如何在android中获取已安装的软件包? [英] How to get installed packages in android?

查看:51
本文介绍了如何在android中获取已安装的软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以获取我的代码来获取所有已安装的软件包并将其显示在烤面包中.问题是,我希望我的代码仅显示其中包含字符串的程序包名称.我该怎么办?

I can get my code to get all installed packages and display them in a toast. The problem is, I want my code to only display the package name containing a string in it. How do I do that?

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();

}
 }

推荐答案

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

Here is the code that will give u all installed activities/applications on Android:

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

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

Getting the list of installed non-system apps:

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);
    }
} }

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

OR you can try a well structured example:

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; }

这篇关于如何在android中获取已安装的软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆