如何在Android中安装包?

时间:2017-03-21 21:51:19

标签: android

我可以获取我的代码以获取所有已安装的软件包并以吐司形式显示它们。问题是,我希望我的代码只显示包含字符串的包名。我该怎么做?

ActivityManager actvityManager = (ActivityManager)
    getApplicationContext().getSystemService( getApplicationContext().ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

for(int pnum = 0; pnum < procInfos.size(); pnum++)
{
 if((procInfos.get(pnum)).processName.contains("android")||    (procInfos.get(pnum)).processName.contains("system")||(procInfos.get(pnum)).processName.contains("huawei")||(procInfos.get(pnum)).processName.contains("adil")) 
 {
      //Toast.makeText(getApplicationContext(), "system apps", Toast.LENGTH_SHORT).show();
 }
else
{
    actvityManager.killBackgroundProcesses(procInfos.get(pnum).processName);
    Toast.makeText(getApplicationContext(), "killed "+procInfos.get(pnum).processName, Toast.LENGTH_SHORT).show();

}
 }

1 个答案:

答案 0 :(得分:1)

以下代码将为您提供Android上所有已安装的活动/应用程序:

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);

获取已安装的非系统应用程序列表:

public static void installedApps()
{
List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
for (int i=0; i < packList.size(); i++)
{
    PackageInfo packInfo = packList.get(i);
    if (  (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
    {
        String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
        Log.e("App № " + Integer.toString(i), appName);
    }
} }

或者您可以尝试结构良好的示例:

class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
    Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
} }

private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); 
final int max = apps.size();
for (int i=0; i<max; i++) {
    apps.get(i).prettyPrint();
}
return apps; }

private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();        
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
    PackageInfo p = packs.get(i);
    if ((!getSysPackages) && (p.versionName == null)) {
        continue ;
    }
    PInfo newInfo = new PInfo();
    newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
    newInfo.pname = p.packageName;
    newInfo.versionName = p.versionName;
    newInfo.versionCode = p.versionCode;
    newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
    res.add(newInfo);
    }
return res; }