最近有人问我说我的博客里面有获取IOS设备信息的博文,但是缺少获取Android设备信息的,我回复说这个百度一搜一大堆,但是鉴于有人有这个需求我就把自己做的一个封装放在这里的,好久之前封装的,要是有不足的地方欢迎指正。
下面是我的封装代码
package com.loopgame.sdk.utils;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.TelephonyManager;
public class DeviceInfoUtils {
private Context context = null;
private TelephonyManager tm = null;
public void initDeviceInfoUtils(Context con)
{
context = con;
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
}
public String getAllInfo()
{
String res = null;
if(context != null)
{
JSONObject js = new JSONObject();
try
{
js.put("appName", getAppName());
js.put("deviceName", getDeviceName());
js.put("deviceCreator", getDeviceCreator());
js.put("imie", getDeviceIMEI());
js.put("softVersion", getDeviceSoftVer());
js.put("imsi", getDeviceImsi());
js.put("phoneNumber", getNativePhoneNumber());
js.put("netCountryIso", getNetCountryIso());
js.put("netOpetator", getNetOpetator());
js.put("netOpetatorName", getNetOpetatorName());
js.put("netType", getNetType());
js.put("phoneType", getPhoneType());
js.put("sim", getDeviceSim());
js.put("simState", getSimState());
js.put("simCountryIso", getSimCountryIso());
js.put("simOperator", getSimOperator());
js.put("simOperatorName", getSimOperatorName());
js.put("voiceMailNumber", getVoiceMailNumber());
js.put("phoneNumProvider", getPhoneNumProviderName());
}
catch(JSONException e)
{
e.printStackTrace();
}
res = js.toString();
}
return res;
}
public String getAppName()
{
String res = null;
if(context == null)
return null;
try
{
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
res = packageManager.getApplicationLabel(appInfo).toString();
}
catch(PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
return res;
}
public String getDeviceName()
{
return Build.MODEL;
}
public String getDeviceCreator()
{
return Build.BRAND;
}
public String getDeviceIMEI()
{
if(context == null)
return null;
return tm.getDeviceId();
}
public String getDeviceSoftVer()
{
if(context == null)
return null;
return tm.getDeviceSoftwareVersion();
}
public String getDeviceImsi()
{
if(context == null)
return null;
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSubscriberId();
}
public String getNativePhoneNumber()
{
if(context == null)
return null;
return tm.getLine1Number();
}
public String getNetCountryIso()
{
if(context == null)
return null;
return tm.getNetworkCountryIso();
}
public String getNetOpetator()
{
if(context == null)
return null;
return tm.getNetworkOperator();
}
public String getNetOpetatorName()
{
if(context == null)
return null;
return tm.getNetworkOperatorName();
}
public String getNetType()
{
if(context == null)
return null;
return String.valueOf(tm.getNetworkType());
}
public String getPhoneType()
{
if(context == null)
return null;
return String.valueOf(tm.getPhoneType());
}
public String getDeviceSim()
{
if(context == null)
return null;
tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSimSerialNumber();
}
public String getSimState()
{
if(context == null)
{
return null;
}
return String.valueOf(tm.getSimState());
}
public String getSimCountryIso()
{
if(context == null)
return null;
return String.valueOf(tm.getSimCountryIso());
}
public String getSimOperator()
{
if(context == null)
return null;
return String.valueOf(tm.getSimOperator());
}
public String getSimOperatorName()
{
if(context == null)
return null;
return String.valueOf(tm.getSimOperatorName());
}
public String getVoiceMailNumber()
{
if(context == null)
{
return null;
}
return tm.getVoiceMailNumber();
}
public String getPhoneNumProviderName() {
String ProvidersName = "N/A";
try{
String IMSI = tm.getSubscriberId();
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
System.out.println(IMSI);
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中国移动";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中国联通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中国电信";
}
}catch(Exception e){
e.printStackTrace();
}
return ProvidersName;
}
}
我希望自己的博文能够为 已经在探索或者即将开始探索Android的彤彤们提供帮助, 喜欢我这种博文方式的或者感觉对自己有帮助的彤彤们千万要关注或者收藏哟,当然博主也欢迎彤彤们有问题留言或者QQ群咨 询,我将尽量为你们做解答。关于 qq群的信息请参考我的博文:Android探秘——q友交流群