AppInfoProvider.java
public class AppInfoProvider {
private CoNtext coNtext;
PackageManager packageManager ;
public AppInfoProvider(CoNtext coNtext){
this.coNtext=coNtext;
packageManager = coNtext.getPackageManager();
}
/**
* 返回當前手機裡面安裝的所有的程式資訊的集合
* @return 應用程式的集合
*/
public List<AppInfo> getAllAppInfo(){
List<AppInfo> myApps= new ArrayList<AppInfo>();
AppInfo myApp = null;
List<PackageInfo> packageInfos =
packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for(PackageInfo packageInfo:packageInfos){
myApp = new AppInfo();
//<manifest package="...">...</manifest>中的package屬性的值
String packageName = packageInfo.packageName;
myApp.setPackageName(packageName);
ApplicationInfo appInfo = packageInfo.applicationInfo;
//設置應用程式名:<application android:label="@string/app_name" >
myApp.setAppName(appInfo.loadLabel(packageManager).toString());
//設置應用程式圖示<application android:icon="@drawable/..." >
myApp.setIcon(appInfo.loadIcon(packageManager));
//判斷是否是系統應用程式
myApp.setSystemApp(!filterApp(appInfo));
myApps.add(myApp);
}
return myApps;
}

/**
* 判斷某個應用程式是 不是三方的應用程式
* @param info
* @return 如果是協力廠商應用程式則返回true,如果是系統程式則返回false
*/
public boolean filterApp(ApplicationInfo info) {
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
return true;
} else if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
return true;
}
return false;
}
}

AppInfo.java

package com.alex.mobilesafe.domain;

import android.graphics.drawable.Drawable;

public class AppInfo {
 private String appName;
 private String packageName;
 private Drawable icon;
 private boolean isSystemApp;
 public String getAppName() {
  return appName;
 }
 public void setAppName(String appName) {
  this.appName = appName;
 }
 public String getPackageName() {
  return packageName;
 }
 public void setPackageName(String packageName) {
  this.packageName = packageName;
 }
 public Drawable getIcon() {
  return icon;
 }
 public void setIcon(Drawable icon) {
  this.icon = icon;
 }
 public boolean isSystemApp() {
  return isSystemApp;
 }
 public void setSystemApp(boolean isSystemApp) {
  this.isSystemApp = isSystemApp;
 }
 @Override
 public String toString() {
  return "AppInfo [appName=" + appName + ", packageName=" + packageName
    + ", icon=" + icon + ", isSystemApp=" + isSystemApp + "]";
 }
 
 

}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()