1 public class MainActivity extendsAppCompatActivity {2
3 TelephonyManager telephonyManager;4 MyPhoneStateListener MyListener;5 @Override6 public voidonCreate(Bundle savedInstanceState) {7 super.onCreate(savedInstanceState);8 setContentView(R.layout.activity_main);9 final TextView textView1 =(TextView) findViewById(R.id.text1);10 final TextView textView2 =(TextView) findViewById(R.id.text2);11 Button button=(Button) findViewById(R.id.button1);12 telephonyManager= (TelephonyManager) MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE);13 MyListener = newMyPhoneStateListener();14 button.setOnClickListener(newView.OnClickListener() {15 @Override16 public voidonClick(View v) {17
18 String operator =telephonyManager.getNetworkOperator();19 /**通过operator获取 MCC 和MNC*/
20 int mcc = Integer.parseInt(operator.substring(0, 3));21 int mnc = Integer.parseInt(operator.substring(3));22 GsmCellLocation location =(GsmCellLocation) telephonyManager.getCellLocation();23 /**通过GsmCellLocation获取中国移动和联通 LAC 和cellID*/
24 int lac =location.getLac();25 int cellid =location.getCid();26 System.out.println("**"+mcc+"A"+mnc+"A"+lac+"A"+cellid);27 textView1.setText("国家编号:"+mcc+"运营商编号:"+mnc+"LAC:"+lac+"CellID:"+cellid);28 List infos =telephonyManager.getAllCellInfo();29 //List infos = telephonyManager.getNeighboringCellInfo();
30 StringBuffer sb = new StringBuffer("总数 : " + infos.size() + "\n");31 for (CellInfo info1 : infos) { //根据邻区总数进行循环32 //sb.append(" LAC : " + info1.getLac());//取出当前邻区的LAC33 //sb.append(" CID : " + info1.getCid());//取出当前邻区的CID
34 sb.append(" CID : " + info1.toString()); //取出当前邻区的CID35 //sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n");//获取邻区基站信号强度
36 }37
38 Log.i(TAG, " 获取邻区基站信息:" +sb.toString());39 textView2.setText(sb.toString());40 }41 });42 telephonyManager.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);43 //telephonyManager.listen(celllistener, PhoneStateListener.LISTEN_CELL_LOCATION);//基站位置的变化
44 }45
46
47
48 private class MyPhoneStateListener extendsPhoneStateListener49 {50 /*Get the Signal strength from the provider, each tiome there is an update 从得到的信号强度,每个tiome供应商有更新*/
51 @Override52
53 public voidonSignalStrengthsChanged(SignalStrength signalStrength)54 {55 super.onSignalStrengthsChanged(signalStrength);56 if (signalStrength.getGsmSignalStrength() != 99) {57 Toast.makeText(getApplicationContext(),58 "Go to Firstdroid!!! GSM Cinr = " + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113) + "dbM", Toast.LENGTH_SHORT).show();59
60
61 System.out.println("****" + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113));62 }63 }64 }