1 package myapplication.com.myjizhansj;
 2 
 3 import android.content.Context;
 4 import android.graphics.Color;
 5 import android.provider.SyncStateContract;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.os.Bundle;
 8 import android.telephony.CellInfo;
 9 import android.telephony.PhoneStateListener;
10 import android.telephony.SignalStrength;
11 import android.telephony.TelephonyManager;
12 import android.telephony.gsm.GsmCellLocation;
13 import android.util.Log;
14 import android.view.View;
15 import android.widget.Button;
16 import android.widget.TextView;
17 import android.widget.Toast;
18 
19 import java.lang.reflect.Method;
20 import java.util.List;
21 
22 public class MainActivity extends AppCompatActivity {
23 
24     TelephonyManager telephonyManager;
25     MyPhoneStateListener MyListener;
26     TextView textView3;
27     Button button;
28 
29     @Override
30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.activity_main);
33         final TextView textView1 = (TextView) findViewById(R.id.text1);
34         final TextView textView2 = (TextView) findViewById(R.id.text2);
35         textView3 = (TextView) findViewById(R.id.text3);
36         button = (Button) findViewById(R.id.button1);
37         telephonyManager = (TelephonyManager) MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
38         MyListener = new MyPhoneStateListener();
39         button.setOnClickListener(new View.OnClickListener() {
40             @Override
41             public void onClick(View v) {
42 
43                 String operator = telephonyManager.getNetworkOperator();
44                 /**通过operator获取 MCC 和MNC */
45                 int mcc = Integer.parseInt(operator.substring(0, 3));
46                 int mnc = Integer.parseInt(operator.substring(3));
47                 GsmCellLocation location = (GsmCellLocation) telephonyManager.getCellLocation();
48                 /**通过GsmCellLocation获取中国移动和联通 LAC 和cellID */
49                 int lac = location.getLac();
50                 int cellid = location.getCid();
51                 textView1.setText("国家编号:" + mcc + "运营商编号:" + mnc + "LAC:" + lac + "CellID:" + cellid);
52                 List<CellInfo> infos = null;
53                 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
54                     infos = telephonyManager.getAllCellInfo();
55                 }
56                 // List<NeighboringCellInfo> infos = telephonyManager.getNeighboringCellInfo();
57                 //这个telephonyManager.getNeighboringCellInfo()方法在使用时,size一直为0,是为什么?
58                 StringBuffer sb = new StringBuffer("总数 : " + infos.size() + "\n");
59                 for (CellInfo info1 : infos) { // 根据邻区总数进行循环
60                     //  sb.append(" LAC : " + info1.getLac()); // 取出当前邻区的LAC
61                     //  sb.append(" CID : " + info1.getCid()); // 取出当前邻区的CID
62                     sb.append(" CID : " + info1.toString()); // 取出当前邻区的CID
63                     //  sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 获取邻区基站信号强度
64                 }
65 
66                 Log.i("TT", " 获取邻区基站信息:" + sb.toString());
67                 textView2.setText(sb.toString());
68                 System.out.println("BBBBB"+sb.toString());
69 3
70             }
71         });
72         telephonyManager.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
73         // telephonyManager.listen(celllistener, PhoneStateListener.LISTEN_CELL_LOCATION); // 基站位置的变化
74     }
75 
76     private class MyPhoneStateListener extends PhoneStateListener {
77         /* Get the Signal strength from the provider, each tiome there is an update  从得到的信号强度,每个tiome供应商有更新*/
78         @Override
79 
80         public void onSignalStrengthsChanged(SignalStrength signalStrength) {
81             super.onSignalStrengthsChanged(signalStrength);
82             //if (signalStrength.getGsmSignalStrength() != 99) {
83             Toast.makeText(getApplicationContext(),
84                     "Go to Firstdroid!!! GSM Cinr = " + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113) + "dbM", Toast.LENGTH_SHORT).show();
85             //这里toast的数据是基站信号强度吗?
86             //手机信号强度和基站信号强度一样吗?
87             textView3.setText(signalStrength.toString());
88             textView3.setTextColor(Color.RED);
89             System.out.println("AAAA"+signalStrength.toString());
90             System.out.println("****" + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113));
91             //  }
92         }
93     }
94 
95 
96 
97 }
View Code

 

相关文章:

  • 2022-12-23
  • 2021-12-22
  • 2021-12-25
  • 2021-05-28
  • 2021-08-21
  • 2022-12-23
  • 2021-11-16
猜你喜欢
  • 2021-12-18
  • 2021-06-02
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
  • 2021-11-16
  • 2021-07-13
相关资源
相似解决方案