检查设备上安装的应用列表,找到我的应用的包名?

时间:2012-08-15 16:24:32

标签: android

我试图通过提供应用名称来查找我的应用程序的包名称。下面的方法在Droid3上花了很长时间。我怎样才能优化这个或者我错过了什么?

 String                     sPackageName        = null;

   PackageManager           pm                  = m_Context.getPackageManager();

 //get a list of installed apps.
  List<ApplicationInfo>     packages            = pm.getInstalledApplications(PackageManager.GET_META_DATA);

  ApplicationInfo   ap  = new ApplicationInfo();
  ap.loadLabel(pm);



   for (ApplicationInfo packageInfo : packages) {

        String          sApp                = packageInfo.loadLabel( pm ).toString();

        //Log.d(TAG, "Installed AppName = " + appName + "  ,package :" + packageInfo.packageName );
        //Log.d(TAG,   "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 

        if( sApp.compareTo( sAppName ) == 0 ){

            sPackageName    = packageInfo.packageName;
            break;
        }

   }// the getLaunchIntentForPackage  

   return sPackageName;

谢谢, DK

1 个答案:

答案 0 :(得分:0)

您无需致电loadLabel()查找包名称,只需直接从packageNameApplicationInfo获取ResolveInfo并进行比较。

使用loadLabel()loadIcon()非常耗时。