Android apk信息获取管理和手机信息获取管理

本文提供了一种简单的方法来获取Android设备的应用及系统信息,包括版本号、唯一标识符、CPU型号等关键数据。

个人学习总结,如有侵权请留言联系删除,忘海涵。

例一:apkinfo文件

package com.utils.data;

import com.shennongshi.dingdong.MApplication;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class APKInfo {

	public static String getVersionName() {
		try {
			PackageInfo pi = MApplication.getInstance1().getPackageManager().
					getPackageInfo(MApplication.getInstance1().getPackageName(), 0);
			return pi.versionName;
		} catch (NameNotFoundException e) {
		}
		return "";
	}

	public static int getVersionCode() {
		try {
			PackageInfo pi = MApplication.getInstance1().getPackageManager().
					getPackageInfo(MApplication.getInstance1().getPackageName(), 0);
			return pi.versionCode;
		} catch (NameNotFoundException e) {
		}
		return 0;
	}
	
	public static String getMetaData(String key) {
		ApplicationInfo appInfo;
		try {
			appInfo = MApplication.getInstance1().getPackageManager().
					getApplicationInfo(MApplication.getInstance1().getPackageName(), PackageManager.GET_META_DATA);
		} catch (NameNotFoundException e) {
			return "";
		}
		return String.valueOf(appInfo.metaData.get(key));
	}
}



例二:phoneinfo文件


package com.utils.data;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import org.apache.http.conn.util.InetAddressUtils;

import com.shennongshi.dingdong.MApplication;
import com.shennongshi.dingdong.pztools.PreferenceKey;
import com.utils.crypt.MD5Util;
import com.utils.db.ConfigPreferences;
import com.utils.tools.Reflection;
import com.utils.tools.XLogger;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

public class PhoneInfo {

	private static final String TAG = PhoneInfo.class.getSimpleName();
	
	private static Application mApplication;
	private static String UUID = "";
	private static String[] sCpuInfo = null;
	private static int mScreenDensity;
	private static DisplayMetrics mDisplayMetrics;
	
	private PhoneInfo() {}
	
	public static void init(Application application) {
		mApplication = application;
	}
	
	public static String getModel() {
		return Build.MODEL;
	}
	
	public static String getBrand() {
		return Build.BRAND;
	}

	public static String getUUID() {
		if (TextUtils.isEmpty(UUID)) {
			UUID = ConfigPreferences.getString(PreferenceKey.UUIDKEY, "");
			if (!TextUtils.isEmpty(UUID)) return UUID;
			final TelephonyManager tm = (TelephonyManager) MApplication
					.getInstance1().getSystemService(Context.TELEPHONY_SERVICE);
			final String tmDevice, tmSerial, androidId;
			tmDevice = tm.getDeviceId();
			tmSerial = tm.getSimSerialNumber();
			androidId = android.provider.Settings.Secure.getString(
						MApplication.getInstance1().getContentResolver(),
						android.provider.Settings.Secure.ANDROID_ID);
			long aIDTime = androidId.hashCode();
			long leastSigBits = System.currentTimeMillis();
			if (!TextUtils.isEmpty(tmDevice)) leastSigBits = (long) tmDevice.hashCode() << 32;
			if (!TextUtils.isEmpty(tmSerial)) leastSigBits |= tmSerial.hashCode();
			UUID deviceUuid = new UUID(aIDTime, leastSigBits);
			UUID = MD5Util.string2MD5(deviceUuid.toString());
			ConfigPreferences.putString(PreferenceKey.UUIDKEY, UUID);
		}
		return UUID;
	}
	
    /**
     * 获取cpu信息 基本上耗时 几毫秒 0型号 1频率 ARMv7
     * 
     * @return
     */
    public static String[] getCpuInfo() {
        if (sCpuInfo == null) {
            String path = "/proc/cpuinfo";
            String data = "";
            String[] cpuInfo = { "", "" };
            String[] arrayOfString;
            try {
                FileReader file = new FileReader(path);
                BufferedReader localBufferedReader = new BufferedReader(file, 8192);
                data = localBufferedReader.readLine();
                arrayOfString = data.split("\\s+");
                for (int i = 2; i < arrayOfString.length; i++) {
                    cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
                }
                data = localBufferedReader.readLine();
                arrayOfString = data.split("\\s+");
                cpuInfo[1] += arrayOfString[2];
                localBufferedReader.close();

                sCpuInfo = cpuInfo;
            } catch (IOException e) {
            }
        }
        return sCpuInfo;
    }

    public static String getCpuType() {
        String cpu = android.os.Build.CPU_ABI;
        if (TextUtils.isEmpty(cpu)) {
            String[] cpuInfo = getCpuInfo();
            if (cpuInfo != null && !TextUtils.isEmpty(cpuInfo[0])) {
                cpu = cpuInfo[0].toLowerCase();
            }
        } else {
            cpu = cpu.toLowerCase();
        }
        XLogger.d(TAG, "cpu:" + cpu);
        return cpu;
    }

    /**
     * 是否是arm架构cpu arm armv7
     * 
     * @return
     */
    public static boolean isArmCpu() {
        String cpu = getCpuType();
        if (TextUtils.isEmpty(cpu)) {
            return false;
        } else {
            return cpu.contains("arm");
        }
    }

    public static boolean isArmV7Cpu() {
        String cpu = getCpuType();
        if (TextUtils.isEmpty(cpu)) {
            return false;
        } else {
            return cpu.contains("v7");
        }
    }
    
    public static String getImsi() {
        TelephonyManager telephonyManager = (TelephonyManager) MApplication
				.getInstance1().getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = telephonyManager.getSubscriberId();
        return imsi;
    }

    public static String getImei() {
        TelephonyManager telephonyManager = (TelephonyManager) MApplication
				.getInstance1().getSystemService(Context.TELEPHONY_SERVICE);
        String imei = telephonyManager.getDeviceId();
        return imei;
    }

    public static String getWifiMac() {
        WifiManager wifi = (WifiManager) MApplication
				.getInstance1().getSystemService(Context.WIFI_SERVICE);
        WifiInfo mac = wifi.getConnectionInfo();
        String macAdress = (mac != null ? mac.getMacAddress() : "");
        return macAdress;
    }
    
    public static String getWifiBssid() {
        WifiManager wifi = (WifiManager) MApplication
				.getInstance1().getSystemService(Context.WIFI_SERVICE);
        WifiInfo mac = wifi.getConnectionInfo();
        String bssidAdress = (mac != null ? mac.getBSSID() : "");
        return bssidAdress;
    }
    
    /**
     * 获取imsi前5位
     * 
     * @param context
     * @return
     */
    public static String getSimInfo(Context context) {
        String imsi = getImsi();
        String siminfo = "00000";
        if (!TextUtils.isEmpty(imsi) && imsi.length() > 4) {
            siminfo = imsi.substring(0, 5);
        }
        return siminfo;
    }
    
    /**
     * @return  返回当前手机的像素密度,xh 分辨率的是 320,h 的是 240 ,m 的是 160
     */
    public static int  getDefaultDisplayMetricsDensity(){
        if(mScreenDensity < 0){
            mScreenDensity = getDefaultDisplayMetrics().densityDpi;
        }
        return mScreenDensity;
    }
    
    /**
     * 获取手机屏幕宽,高<br>
     * 返回的 DisplayMetricx 不能被 copy ,因为该 DisplayMetricx 会随着手机的变化而变化,比如横竖屏切换时,DisplayMetricx 里面的宽、高的值就会发生对换
     * @return
     */
    public static DisplayMetrics getDefaultDisplayMetrics(){
        if(mDisplayMetrics == null){
            mDisplayMetrics = mApplication.getResources().getDisplayMetrics();
        }
    	return mDisplayMetrics;
    }
    
    public static int getScreenWidth(Activity activity) {
        DisplayMetrics dm = getDisplayMetrics(activity);
        if (dm == null) return 0;
        return dm.widthPixels;
    }

    public static int getScreenHeight(Activity activity) {
        DisplayMetrics dm = getDisplayMetrics(activity);
        if (dm == null) return 0;
        return dm.heightPixels;
    }

    private static DisplayMetrics getDisplayMetrics(Activity activity) {
    	if (activity == null) return null;
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm;
    }
    
    public static float getDimensValue(int resId) {
		return MApplication.getInstance1().getResources().getDimension(resId);
	}
    
	public static int convertDpToPx(int dp) {
		return (int) (getDefaultDisplayMetrics().density * dp + 0.5f);
	}

	public static float convertDpToPx(float dp) {
		return (getDefaultDisplayMetrics().density * dp);
	}

	public static int convertPxToDp(int px) {
		return (int) (px / getDefaultDisplayMetrics().density + 0.5f);
	}

	public static int convertSpToPx(float sp) {
		return (int) (getDefaultDisplayMetrics().scaledDensity * sp);
	}

	public static String getOSVersion() {
		return android.os.Build.VERSION.RELEASE;
	}

	public static int getSDKVersion() {
		return android.os.Build.VERSION.SDK_INT;
	}
    
    //根据IP获取本地Mac
	public static String getLocalMacAddressFromIp() {
		Reflection reflection = new Reflection();

		String mac_s = "";
		try {
			byte[] mac;
			NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
			mac = (byte[]) reflection.invokeMethod(ne, "getHardwareAddress", new Object[] {});
			mac_s = byte2hex(mac);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mac_s;
	}
    
    public static String getLocalIpAddress() {  
	    try {  
	        String ipv4;  
	        List<NetworkInterface>  nilist = Collections.list(NetworkInterface.getNetworkInterfaces());  
	        for (NetworkInterface ni: nilist)   
	        {  
	            List<InetAddress>  ialist = Collections.list(ni.getInetAddresses());  
	            for (InetAddress address: ialist){  
	                if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))   
	                {   
	                    return ipv4;  
	                }  
	            }  
	        }  
	
	    } catch (SocketException ex) {  
	    }  
	    return null;  
	}
    
	public static String byte2hex(byte[] b) {
		StringBuffer hs = new StringBuffer(b.length);
		String stmp = "";
		int len = b.length;
		for (int n = 0; n < len; n++) {
			stmp = Integer.toHexString(b[n] & 0xFF);
			if (stmp.length() == 1)
				hs = hs.append("0").append(stmp);
			else {
				hs = hs.append(stmp);
			}
		}
		return String.valueOf(hs);
	}

}



在使用过程中,将以上两个文件直接导入工程,使用封装好的信息获取函数就可以轻松获取相应信息。

如有纰漏,望指正。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值