1、Android 獲取手機中已安裝apk文件信息(PackageInfo、ResolveInfo)(應用圖片、應用名、包名等) 一、經過PackageManager可獲取手機端已安裝的apk文件的信息,具體代碼以下:app
經過上述方法,可獲得手機中安裝的全部應用程序,包括手動安裝的apk包的信息、、系統預裝的應用軟件的信息,要區分這兩類軟件可以使用如下方法: (a) 從packageInfoList獲取的packageInfo,再經過packageInfo.applicationInfo獲取applicationInfo。 (b) 判斷(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,該值大於0時,表示獲取的應用爲系統預裝的應用,反之則爲手動安裝的應用。 (1)獲取應用的代碼:spa
public static List<PackageInfo> getAllApps(Context context) { 3d
List<PackageInfo> apps = new ArrayList<PackageInfo>(); orm
PackageManager pManager = context.getPackageManager(); 對象
// 獲取手機內全部應用 圖片
List<PackageInfo> paklist = pManager.getInstalledPackages(0); ip
for (int i = 0; i < paklist.size(); i++) { 內存
PackageInfo pak = (PackageInfo) paklist.get(i);
// 判斷是否爲非系統預裝的應用程序
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
apps.add(pak);
}
}
return apps;
}
(2)、獲取圖片、應用名、包名(ShareItemInfo 類是本身寫的,忽視吧):
PackageManager pManager = MessageSendActivity.this.getPackageManager();
List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this);
for(int i=0;i<appList.size();i++) {
PackageInfo pinfo = appList.get(i);
shareItem = new ShareItemInfo();
shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));
shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString());
shareItem.setPackageName(pinfo.applicationInfo.packageName);
}
(3)獲取支持分享的應用的代碼:
public static List<ResolveInfo> getShareApps(Context context){
List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
Intent intent=new Intent(Intent.ACTION_SEND,null);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("text/plain");
PackageManager pManager = context.getPackageManager();
mApps = pManager.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
return mApps;
}
因爲該方法,返回的並非PackageInfo 對象。而是ResolveInfo。所以獲取圖片、應用名、包名的方法不同,以下:
PackageManager pManager = MessageSendActivity.this.getPackageManager();
List<ResolveInfo> resolveList = Utils.getShareApps(MessageSendActivity.this);
for(int i=0;i<resolveList.size();i++) {
ResolveInfo resolve = resolveList.get(i);
ShareItemInfo shareItem = new ShareItemInfo();
//set Icon
shareItem.setIcon(resolve.loadIcon(pManager));
//set Application Name
shareItem.setLabel(resolve.loadLabel(pManager).toString());
//set Package Name
shareItem.setPackageName(resolve.activityInfo.packageName);
}
總結: 經過 PackageInfo 獲取具體信息方法: 包名獲取方法:packageInfo.packageName icon獲取獲取方法:packageManager.getApplicationIcon(applicationInfo) 應用名稱獲取方法:packageManager.getApplicationLabel(applicationInfo) 使用權限獲取方法:packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS) .requestedPermissions 經過 ResolveInfo 獲取具體信息方法: 包名獲取方法:resolve.activityInfo.packageName icon獲取獲取方法:resolve.loadIcon(packageManager) 應用名稱獲取方法:resolve.loadLabel(packageManager).toString()
2、獲取手機可用內存和總內存(手機的內存信息主要在/proc/meminfo文件中,其中第一行是總內存,而剩餘內存可經過ActivityManager.MemoryInfo獲得):
private String[] getTotalMemory() {
String[] result = {"",""}; //1-total 2-avail
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
mActivityManager.getMemoryInfo(mi);
long mTotalMem = 0;
long mAvailMem = mi.availMem;
String str1 = "/proc/meminfo";
String str2;
String[] arrayOfString;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
localBufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
result[0] = Formatter.formatFileSize(this, mTotalMem);
result[1] = Formatter.formatFileSize(this, mAvailMem);
Log.i(TAG, "meminfo total:" + result[0] + " used:" + result[1]);
return result;
}
3、獲取手機CPU信息(和內存信息同理,cpu信息可經過讀取/proc/cpuinfo文件來獲得,其中第一行爲cpu型號,第二行爲cpu頻率):
private String[] getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2 = "";
String[] cpuInfo = {"", ""}; //1-cpu型號 //2-cpu頻率
String[] arrayOfString;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 2; i < arrayOfString.length; i++) {
cpuInfo[0] = cpuInfo[0] + arrayOfString + " ";
}
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
cpuInfo[1] += arrayOfString[2];
localBufferedReader.close();
} catch (IOException e) {
}
Log.i(TAG, "cpuinfo:" + cpuInfo[0] + " " + cpuInfo[1]);
return cpuInfo;
}
4、獲取手機MAC地址:
private String getMacAddress(){
String result = "";
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
result = wifiInfo.getMacAddress();
Log.i(TAG, "macAdd:" + result);
return result;
}
5、獲取屏幕密度三種方法:
// 獲取屏幕密度(方法1)
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕寬(像素,如:480px)
int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p)
Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// 獲取屏幕密度(方法2)
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
float xdpi = dm.xdpi;
float ydpi = dm.ydpi;
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
screenWidth = dm.widthPixels; // 屏幕寬(像素,如:480px)
screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px)
Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// 獲取屏幕密度(方法3)
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
xdpi = dm.xdpi;
ydpi = dm.ydpi;
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
int screenWidthDip = dm.widthPixels; // 屏幕寬(dip,如:320dip)
int screenHeightDip = dm.heightPixels; // 屏幕寬(dip,如:533dip)
Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
screenWidth = (int)(dm.widthPixels * density + 0.5f); // 屏幕寬(px,如:480px)
screenHeight = (int)(dm.heightPixels * density + 0.5f); // 屏幕高(px,如:800px)
Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
|