android获取设备信息

最近有人问我说我的博客里面有获取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友交流群



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值