android 如何获取信号强度,android获取基站信号强度问题?

本文档展示了如何在Android MainActivity中获取手机运营商信息、信号强度及邻区基站信息,包括MCC、MNC、LAC和CellID。重点介绍了使用TelephonyManager监听信号强度变化和CellLocation获取位置信息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值