2011-03-22 57 views
5

這是我目前的代碼,但我仍無法獲取手機上所有應用程序的列表。有沒有人看到我在做什麼錯了?如何獲取安裝在Android手機上的所有應用程序

public class GetAppList extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     try { 
      List<PackageInfo> appListInfo1 = this.getPackageManager() 
      .getInstalledPackages(0); 
      JSONArray ja = new JSONArray(); 
      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       Object sendDataUrl = null; 
       HttpPost request = new HttpPost(sendDataUrl.toString()); 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       ContextWrapper context = null; 
       PackageManager pm = context.getPackageManager(); 
       List<PackageInfo> appListInfo = pm.getInstalledPackages(0); 
       for (PackageInfo p : appListInfo) { 
        if (p.applicationInfo.uid > 10000) { 
         JSONObject jo = new JSONObject(); 
         jo.put("label", p.applicationInfo.loadLabel(pm).toString()); 
         jo.put("packageName", p.applicationInfo.packageName); 
         ja.put(jo); 
        } 
        System.out.print(ja); 
       }   
     } catch (Exception e) { 
      // TODO: handle exception 
     } 
     finally { 
      // ... cleanup that will execute whether or not an error occurred ... 
     } 



}catch (Exception e) { 
    // TODO: handle exception 
} 
finally { 
    // ... cleanup that will execute whether or not an error occurred ... 
} 
    }} 

抱歉無法得到這個來正確格式化代碼。

回答

3

這是我使用清單應用程序的代碼:如果你以後要運行的應用程序

PackageManager pm = this.getPackageManager(); 
Intent intent = new Intent(Intent.ACTION_MAIN, null); 

String activityName = rInfo.activityInfo.name; 
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED); 

for (ResolveInfo rInfo : list) { 
    pkg = rInfo.activityInfo.applicationInfo.packageName; 
    if (pm.getLaunchIntentForPackage(pkg) == null) { 
     continue; 
    } 
    String label = rInfo.activityInfo.applicationInfo.loadLabel(pm).toString(); 
    arrayList.add(new AppEntry(label, activityName, pkg, null)); 
} 

只知道PKG和activityName你可以這樣做:

Intent intent = new Intent(Intent.ACTION_MAIN, null); 
intent.addCategory(Intent.CATEGORY_LAUNCHER); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setClassName(pkg, activityname); 
ctx.startActivity(intent); 
+1

此代碼不會剪切和粘貼爲正確的(String activityName和pkg周圍的錯誤)...只是FYI – easycheese 2012-05-06 16:02:02

5

此代碼登錄名和所有已安裝應用程序的軟件包名稱。您可以使用LogCat輕鬆檢查日誌(如果您使用的是Eclipse)。

程序包名稱 - 應用程序包的名稱。

名稱 - 與應用程序關聯的文本標籤。您只能使用appInfo.name,因爲它將返回null。使用appInfo.loadLabel(packageManager)您可以獲得實際應用的名稱,如語音錄音機而不是包名com.android.speechrecorder

final PackageManager packageManager = getPackageManager(); 
List<ApplicationInfo> installedApplications = 
    packageManager.getInstalledApplications(PackageManager.GET_META_DATA); 

for (ApplicationInfo appInfo : installedApplications) 
{ 
    Log.d("OUTPUT", "Package name : " + appInfo.packageName); 
    Log.d("OUTPUT", "Name: " + appInfo.loadLabel(packageManager)); 
} 
相關問題