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() + " "); 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()) + " "); // 获取邻区基站信号强度 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 }
1.signalStrength.toString()方法:
@Override public String toString() { return ("SignalStrength:" + " " + mGsmSignalStrength + " " + mGsmBitErrorRate + " " + mCdmaDbm + " " + mCdmaEcio + " " + mEvdoDbm + " " + mEvdoEcio + " " + mEvdoSnr + " " + mLteSignalStrength + " " + mLteRsrp + " " + mLteRsrq + " " + mLteRssnr + " " + mLteCqi + " " + (isGsm ? "gsm|lte" : "cdma")); }
/** * Get LTE as dBm * * @hide */ public int getLteDbm() { return mLteRsrp; }
2. 华为荣耀4A:
4GLTE 信号下打印出的signalStrength.toString()为:
SignalStrength:GW:99 -1 -1 255 0,CDMA:-1 255,EVDO:-1 255 255,LTE:24 -92 -7 286 2147483647,TD:2147483647;gsm|lte
3G HSDPA 信号下 打印出的signalStrength.toString()为:
SignalStrength:GW:99 -1 -1 255 0,CDMA:-1 255,EVDO:-1 255 255,LTE:99 2147483647 2147483647 2147483647 2147483647,TD:-82;gsm|lte
2G信号下打印出的signalStrength.toString()为:
SignalStrength:GW:17 -80 -1 255 0,CDMA:-1 255,EVDO:-1 255 255,LTE:99 2147483647 2147483647 2147483647 2147483647,TD:2147483647;gsm|lte
3. Meizu M2 note
4G LTE信号下打印出的signalStrength.toString()为:
SignalStrength: 25 45 -120 -160 -7 0 -1 99 -95 -7 2147483647 2147483647 gsm|lte 1 1 1
3G HSDPA信号下 打印出的signalStrength.toString()为:
SignalStrength: 16 49 -120 -160 -4 0 -1 99 2147483647 2147483647 2147483647 2147483647 gsm|lte 1 -304 0
2G EDGE信号下打印出的signalStrength.toString()为:
SignalStrength: 18 0 -120 -160 -3 0 -1 99 2147483647 2147483647 2147483647 2147483647 gsm|lte -305 1 1
4. (1)toString重写后打印结果还是不一样