获取手机的信息:手机IMEI,手机sim卡的IMSI,网络类型(WiFi,LTE(即4G)等),信号强度(蜂窝网络非WiFi),经纬度
app:http://fir.im/dpxu,可以下载看看效果
书接上文:http://blog.csdn.net/i_do_can/article/details/50411727 ,在上面的基础上继续
布局文件只是一个ListView,直接上主要的代码
// 获取当前时间
@SuppressLint("SimpleDateFormat")
public String time() {
long time = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(time);
String strTime = format.format(date);
return strTime;
}
// 获取IMSI
public void getIMSI(Context context) {
TelephonyManager telManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String imsi = telManager.getSubscriberId();
setIMSI(imsi);
}
// 获取IMEI
public void getIMEI(Context context) {
String imei = ((TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
setIMEI(imei);
}
/**
* 通过wifiManager获取mac地址
* @attention Wifi
* @return Mac Address
*/
public void getMacFromWifi(Context context){
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String mac = wifiInfo.getMacAddress();
setMAC(mac);
}
// 在WiFi状态下获取IP的地址
public void getWifi2Ip(Context context) {
// 获取wifi服务
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
// 判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
setIP(ip);
}
// GPRS本地网络时获取的IP地址
public void getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
setIP(inetAddress.getHostAddress().toString());
}
}
}
} catch (SocketException ex) {
}
}
/**
* 获取运营商
*/
public void getOperatorName(Context context) {
String provider = "unknown";
try {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String IMSI = telephonyManager.getSubscriberId();
//Log.v("tag", "getProvider.IMSI:" + IMSI);
if (IMSI == null) {
if (TelephonyManager.SIM_STATE_READY == telephonyManager
.getSimState()) {
String operator = telephonyManager.getSimOperator();
Log.v("tag", "getProvider.operator:" + operator);
if (operator != null) {
if (operator.equals("46000")
|| operator.equals("46002")
|| operator.equals("46007")) {
provider = "中国移动";
} else if (operator.equals("46001")) {
provider = "中国联通";
} else if (operator.equals("46003")) {
provider = "中国电信";
}
}
}
} else {
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")
|| IMSI.startsWith("46007")) {
provider = "中国移动";
} else if (IMSI.startsWith("46001")) {
provider = "中国联通";
} else if (IMSI.startsWith("46003")) {
provider = "中国电信";
}
}
} catch (Exception e) {
e.printStackTrace();
}
setProviter(provider);
}
/**
* 得到当前的手机网络类型
*
* @param context
* @return
*/
public String getCurrentNetType(Context context) {
String type = "unknown";
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null) {
type = "unknown";
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
type = "wifi";
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int subType = info.getSubtype();
if (subType == TelephonyManager.NETWORK_TYPE_CDMA
|| subType == TelephonyManager.NETWORK_TYPE_GPRS
|| subType == TelephonyManager.NETWORK_TYPE_EDGE) {
type = "2g";
} else if (subType == TelephonyManager.NETWORK_TYPE_UMTS
|| subType == TelephonyManager.NETWORK_TYPE_HSDPA
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_A
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_0
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
type = "3g";
} else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡,是3.9G的全球标准
type = "4g";
}
}
setType(type);
return type;
}
/**
* 得到当前的手机蜂窝网络信号强度
* 获取LTE网络和3G/2G网络的信号强度的方式有一点不同,
* LTE网络强度是通过解析字符串获取的,
* 3G/2G网络信号强度是通过API接口函数完成的。
* asu 与 dbm 之间的换算关系是 dbm=-113 + 2*asu
*/
public void getCurrentNetDBM(Context context) {
final TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener mylistener = new PhoneStateListener(){
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
String info = null;
String signalInfo = signalStrength.toString();
String[] params = signalInfo.split(" ");
int Itedbm = Integer.parseInt(params[9]);
int asu = signalStrength.getGsmSignalStrength();
int dbm = -113 + 2*asu;
if(tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE){
setDBM("LTE:" + Itedbm + "dBm,Detail:" +signalInfo );
}else if(tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA ||
tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPA ||
tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSUPA ||
tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS){
setDBM("MCDMA:" + dbm + "dBm,Detail:" +signalInfo );
}else{
setDBM( "GSM:" + dbm + "dBm,Detail:" +signalInfo );
}
}
};
tm.listen(mylistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
public void location(Context context){
//未开启GPS
/*特别奇怪的就是,这个地方,我关闭网络,关闭GPS,但是执行isOPen(context),会自动给我打开无线网 原来是 getWifi2Ip 这个方法给开了 */
if (!isOPen(context)) {
//提示用户开启,这里强制帮开启
openGPS(context);
}
/*检查是否用户允许定位
* 不加这句话虽提示错误但是可以运行
* 加入验证后出现运行时错误,分析是我用的小米系统,可以把应用的GPS权限修改为使用时提示,
* 所以这地方是检查是否允许,这边是不允许的,但是也不应该抛异常呀
* "main@3697" prio=5 waiting
java.lang.Thread.State: WAITING
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:72)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
* */
// if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
// || context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
latitude = locationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER).getLatitude();
longitude = locationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER).getLongitude();
setLatitude(latitude);
setLongitude(longitude);
} else {
LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
}
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
if (location != null) {
setLatitude(location.getLatitude());
setLongitude(location.getLongitude());
}
}
};
locationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 0, locationListener);
Location location2 = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location2 != null) {
latitude = location2.getLatitude(); // 经度
longitude = location2.getLongitude(); // 纬度
setLatitude(latitude);
setLongitude(longitude);
}
}
// }
}
public static boolean isOPen(Context context) {
LocationManager locationManager
= (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快)
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位)
//这里很奇怪,我关了所有的网络,可是 network==true
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
return true;
}
return false;
}
/**
* 强制帮用户打开GPS
* @param context
*/
public static void openGPS(Context context) {
Intent GPSIntent = new Intent();
GPSIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
GPSIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}