可以获得手机中,系统应用,第三方应用。根据包名获得应用信息,判断应用是否是系统应用,打开和卸载应用
package com.zhuoma.packagemanager.utils;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
/**
* @Description: 获取手机安装App工具类
* @ClassName: DensityUtil
* @Author: BiHaidong
* @Version: V1.0
* @Date: 2015-12-3 15:41:24
*/
public class AppInfoProvider {
private PackageManager packageManager;
// 获取一个包管理器
public AppInfoProvider(Context context) {
packageManager = context.getPackageManager();
}
/**
* 获取系统中所有应用信息, 并将应用软件信息保存到list列表中。
*
*/
public List<AppInfo> getApps(String flag) {
List<AppInfo> list = new ArrayList<AppInfo>();
AppInfo myAppInfo;
// 获取到所有安装了的应用程序的信息,包括那些卸载了的,但没有清除数据的应用程序
List<PackageInfo> packageInfos = packageManager
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo info : packageInfos) {
myAppInfo = new AppInfo();
// 拿到包名
String packageName = info.packageName;
// 拿到应用程序的信息
ApplicationInfo appInfo = info.applicationInfo;
// 拿到应用程序的图标
Drawable icon = appInfo.loadIcon(packageManager);
// 拿到应用程序的大小
// long codesize = packageStats.codeSize;
// Log.i("info", "-->"+codesize);
// 拿到应用程序的程序名
String appName = appInfo.loadLabel(packageManager).toString();
myAppInfo.setPackageName(packageName);
myAppInfo.setAppName(appName);
myAppInfo.setAppIcon(icon);
if (filterApp(appInfo)) {
myAppInfo.setSystemApp(false);
} else {
myAppInfo.setSystemApp(true);
}
if ("sys".equals(flag)) {
if (myAppInfo.getSystemApp()) {
list.add(myAppInfo);
}
} else if ("user".equals(flag)) {
if (!myAppInfo.getSystemApp()) {
list.add(myAppInfo);
}
} else if ("all".equals(flag)) {
list.add(myAppInfo);
}
}
return list;
}
/**
* 获取系统中所有系统应用信息。
*
* @author BiHaidong
* @return app信息list
*
*/
public List<AppInfo> getSysApps() {
return getApps("sys");
}
/**
* 获取系统中所有第三方应用信息。
*
* @author BiHaidong
* @return app信息list
*
*/
public List<AppInfo> getuserApps() {
return getApps("user");
}
/**
* 获取系统中所有应用信息。
*
* @author BiHaidong
* @return app信息list
*
*/
public List<AppInfo> getAllApps() {
return getApps("all");
}
/**
* 根据包名获得应用信息
*
* @author BiHaidong
* @param packageName 包名
* @return app信息list
*
*/
public PackageInfo getCurrentPackageInfo(String packageName) {
PackageInfo info = null;
try {
info = packageManager.getPackageInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return info;
}
/**
* 判断某一个应用程序是不是系统的应用程序, 如果是返回true,否则返回false。
*
* @author BiHaidong
* @return app信息list
*/
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;
}
}
App信息类
package com.zhuoma.packagemanager.utils;
import android.graphics.drawable.Drawable;
/**
* 定义一个数据结构,保存应用程序信息(icon,name,packageName,versionName,versionCode)等等。
*
* @ClassName: AppInfo
* @Author: BiHaidong
* @Version: V1.0
* @Date: 2015-12-3 15:41:24
*/
public class AppInfo {
public String appName = "";
public String packageName = "";
public String versionName = "";
public int versionCode = 0;
public Drawable appIcon = null;
public Boolean systemApp = false;
public Boolean getSystemApp() {
return systemApp;
}
public void setSystemApp(Boolean systemApp) {
this.systemApp = systemApp;
}
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 String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public int getVersionCode() {
return versionCode;
}
public void setVersionCode(int versionCode) {
this.versionCode = versionCode;
}
public Drawable getAppIcon() {
return appIcon;
}
public void setAppIcon(Drawable appIcon) {
this.appIcon = appIcon;
}
}
打开和卸载应用
package com.zhuoma.packagemanager.utils;
import java.util.List;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.widget.Toast;
public class CustomUtils {
public static void open(String packageName,Context context) {
// open app
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageName);
List<ResolveInfo> resolveInfoList = context
.getPackageManager()
.queryIntentActivities(resolveIntent, 0);
if (resolveInfoList != null && resolveInfoList.size() > 0) {
ResolveInfo resolveInfo = resolveInfoList.get(0);
String activityPackageName = resolveInfo.activityInfo.packageName;
String className = resolveInfo.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName componentName = new ComponentName(
activityPackageName, className);
intent.setComponent(componentName);
context.startActivity(intent);
} else {
Toast.makeText(context, "此应用没有提供前台界面,不能打开!",
Toast.LENGTH_SHORT).show();
}
}
public static void delete(String packageName,Context context) {
// delete app
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
packageURI);
context.startActivity(uninstallIntent);
}
}