手机基站信息获取

package com.ywcai.firstapp;


import java.util.Date;




import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.telephony.*;
import android.telephony.gsm.GsmCellLocation;
import android.text.method.ScrollingMovementMethod;


public class MainActivity extends Activity {
private int mobiletype = 0, lac = 0, rsp = 0, cid = 0, flagcid = 0;
private String mobileid;
private String nowtime;
private TelephonyManager tm;
private String textString = "";
private TextView cellText, cellText2;
private String errString = "";
private boolean flag=true;
private Handler handler = new Handler();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mobileid = tm.getDeviceId();
cellText = (TextView) findViewById(R.id.StationInfo);
cellText2 = (TextView) findViewById(R.id.StationInfoRs);
cellText2.setMovementMethod(new ScrollingMovementMethod());
handler.post(task);// 立即调用线程
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}


private Runnable task = new Runnable() {
public void run() {
// TODO Auto-generated method stub
handler.postDelayed(this, 1 * 1000);// 设置刷新时间,此处是1秒
dowork();
cellText.setText(String
.format("时间:%s \n\n设备ID:%s | 网络:%d \n\n Lac:%d | Cid:%d | 场强:%d\n\n %s",
nowtime, mobileid, mobiletype, lac, cid, rsp,
errString));
if (flagcid != cid) {
flagcid = cid;
textString = "\n\n【网络:" + mobiletype + " | LAC:" + lac
+ " | Cid:" + cid + " | " + nowtime + "】" + textString;
cellText2.setText(textString);
}
}
};




//采取通过方法获取基站信息,则需要自己重写场强的监听器。3/4G的模式该类无没直接获取场强的方法,需转换。


private PhoneStateListener pStateListener = new PhoneStateListener() {
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// TODO Auto-generated method stub
super.onSignalStrengthsChanged(signalStrength);


if (mobiletype == 1 || mobiletype == 2) {
rsp = signalStrength.getGsmSignalStrength();//2G
} else if (mobiletype == 13) {
String str = signalStrength.toString();
String[] s = str.split("\\ ");
rsp = Integer.parseInt(s[11]);//4G
} else {
String str = signalStrength.toString();
String[] s = str.split("\\ ");
rsp = Integer.parseInt(s[3]);//3G
}
}
};




@SuppressLint("NewApi")
public void dowork() {
mobiletype = tm.getNetworkType();
Date now = new Date();
java.text.DateFormat df = java.text.DateFormat.getTimeInstance();
nowtime = df.format(now);
try {
errString = "use getAllCellInfo() interface";
CellInfo stationinfo = tm.getAllCellInfo().get(0);//这个方式获取信息比较简单,但是有些手机不支持。
if (stationinfo instanceof CellInfoGsm) {
CellInfoGsm cellInfoGsm = (CellInfoGsm) stationinfo;
CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity();
CellSignalStrengthGsm cellrsp = cellInfoGsm
.getCellSignalStrength();
rsp = cellrsp.getDbm();
lac = cellIdentity.getLac();
cid = cellIdentity.getCid();
} else if (stationinfo instanceof CellInfoWcdma) {
CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) stationinfo;
CellIdentityWcdma cellIdentity = cellInfoWcdma
.getCellIdentity();
CellSignalStrengthWcdma cellrsp = cellInfoWcdma.getCellSignalStrength();
rsp = cellrsp.getDbm();
lac = cellIdentity.getLac();
cid = cellIdentity.getCid();
String sss = Integer.toHexString(cid);
sss = sss.substring(sss.length() - 4);
cid = Integer.parseInt(sss, 16);


} else if (stationinfo instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) stationinfo;
CellIdentityLte cellIdentity = cellInfoLte.getCellIdentity();
CellSignalStrengthLte cellrsp = cellInfoLte
.getCellSignalStrength();
rsp = cellrsp.getDbm();
lac = cellIdentity.getTac();
cid = cellIdentity.getCi();
String sss = Integer.toHexString(cid);
sss = sss.substring(sss.length() - 4);
cid = Integer.parseInt(sss, 16);
} else {
}
}

catch (Exception e) {
// TODO: handle exception  有些手机不支持上面的接口,下面的代码可直接获取所有手机的LAC、CID,但是获取场强需要另外加监听器。
try {
errString = "err:cannot use getAllCellInfo() interface";
if(flag)//只加一次
{
tm.listen(pStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
flag=false;
}
GsmCellLocation lc = (GsmCellLocation) tm.getCellLocation();
lac = lc.getLac();
cid = lc.getCid();
String sss = Integer.toHexString(cid);
sss = sss.substring(sss.length() - 4);
cid = Integer.parseInt(sss, 16);
} catch (Exception e2) {
errString = "err:网络切换,无信号";
lac = 0;
cid = 0;
}
}
}
}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值