【问题标题】:How to get all apps installed on android phone如何在安卓手机上安装所有应用程序
【发布时间】:2011-07-20 14:23:24
【问题描述】:

这是我目前拥有的代码,但我仍然无法获得手机上所有应用程序的列表。有谁看到我做错了什么?

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

抱歉,无法正确格式化代码。

【问题讨论】:

    标签: android android-emulator android-widget android-layout


    【解决方案1】:

    此代码记录所有已安装应用程序的名称和包名称。您可以使用 LogCat 轻松检查日志(如果您使用的是 Eclipse)。

    包名称 - 应用包的名称。

    名称 - 与应用程序关联的文本标签。你不能只使用appInfo.name,因为它会返回null。使用appInfo.loadLabel(packageManager),您将获得实际应用的名称,例如 Speech Recorder,而不是包名称 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));
    } 
    

    【讨论】:

      【解决方案2】:

      这是我用来列出应用程序的代码:

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

      【讨论】:

      • 此代码没有正确剪切和粘贴(字符串 activityName 和 pkg 周围的错误)...仅供参考
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多