如何了解一个特定的包是否我的Andr​​oid设备上的存在?我的、存在、设备、oid

2023-09-11 20:20:46 作者:庸俗且无趣

如何才能了解是否一个特定的包或应用程序 - 比如,com.android.abc - 存在我的Andr​​oid设备上。

How can I learn whether a particular package or application - say, "com.android.abc" - exists on my Android device?

推荐答案

调用任何下面的方法与包名。

Call any of the below method with the package name.

import com.android.content.pm.PackageManager;

// ...

    public boolean isPackageExisted(String targetPackage){
        List<ApplicationInfo> packages;
        PackageManager pm;

        pm = getPackageManager();        
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if(packageInfo.packageName.equals(targetPackage))
                return true;
        }
        return false;
    }

 import com.android.content.pm.PackageManager;

 public boolean isPackageExisted(String targetPackage){
   PackageManager pm=getPackageManager();
   try {
     PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
   } catch (PackageManager.NameNotFoundException e) {
     return false;
   }  
   return true;
 }