public class MainActivity extends AppCompatActivity {
private static final String TAG = "GSMCellLocationActivity";
TelephonyManager telephonyManager;
MyPhoneStateListener MyListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView1 = (TextView) findViewById(R.id.text1);
final TextView textView2 = (TextView) findViewById(R.id.text2);
Button button= (Button) findViewById(R.id.button1);
telephonyManager= (TelephonyManager) MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
MyListener = new MyPhoneStateListener();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String operator = telephonyManager.getNetworkOperator();
/**通过operator获取 MCC 和MNC */
int mcc = Integer.parseInt(operator.substring(0, 3));
int mnc = Integer.parseInt(operator.substring(3));
GsmCellLocation location = (GsmCellLocation) telephonyManager.getCellLocation();
/**通过GsmCellLocation获取中国移动和联通 LAC 和cellID */
int lac = location.getLac();
int cellid = location.getCid();
System.out.println("**"+mcc+"A"+mnc+"A"+lac+"A"+cellid);
textView1.setText("国家编号:"+mcc+"运营商编号:"+mnc+"LAC:"+lac+"CellID:"+cellid);
List<CellInfo> infos = telephonyManager.getAllCellInfo();
**// List<NeighboringCellInfo> infos = telephonyManager.getNeighboringCellInfo();
//这个方法在使用时,size一直为0,是为什么?**
StringBuffer sb = new StringBuffer("总数 : " + infos.size() + "\n");
for (CellInfo info1 : infos) { // 根据邻区总数进行循环
// sb.append(" LAC : " + info1.getLac()); // 取出当前邻区的LAC
// sb.append(" CID : " + info1.getCid()); // 取出当前邻区的CID
sb.append(" CID : " + info1.toString()); // 取出当前邻区的CID
// sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 获取邻区基站信号强度
}
Log.i(TAG, " 获取邻区基站信息:" + sb.toString());
textView2.setText(sb.toString());
}
});
telephonyManager.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
// telephonyManager.listen(celllistener, PhoneStateListener.LISTEN_CELL_LOCATION); // 基站位置的变化
}
private class MyPhoneStateListener extends PhoneStateListener
{
/* Get the Signal strength from the provider, each tiome there is an update 从得到的信号强度,每个tiome供应商有更新*/
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
**if (signalStrength.getGsmSignalStrength() != 99) {
Toast.makeText(getApplicationContext(),
"Go to Firstdroid!!! GSM Cinr = " + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113) + "dbM", Toast.LENGTH_SHORT).show();
//这里toast的数据是基站信号强度吗?
//手机信号强度和基站信号强度一样吗?
System.out.println("****" + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113));**
}
}
}
}