2011-06-03 153 views
0

我使用下面的代码来获取已安装的应用程序的列表并启动它们。它只打开应用程序,如计算器或时钟。当我尝试打开应用程序如联系人或相机时,它不起作用因为它们的启动活动是空的。我如何打开这样的应用程序。启动安装的应用程序与启动活动null

在下面的代码中s [5]指向相机。

 final PackageManager pm = getPackageManager(); 
    List<ApplicationInfo> packages = pm 
     .getInstalledApplications(PackageManager.GET_META_DATA); 


    for (ApplicationInfo packageInfo : packages) 
    { 

     s[i]=packageInfo.packageName; 
     i++; 

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

    } 


} 



    Intent mIntent = getPackageManager().getLaunchIntentForPackage(s[5]); 


     try { 
      startActivity(mIntent); 

     } 
     catch (Exception err) { 
      Toast t = Toast.makeText(getApplicationContext(), 
        "Not Found", Toast.LENGTH_SHORT); 
      t.show(); 
     } 

}

}

回答

1

我终于能够解决下面的代码issue.The启动所有应用程序。

PackageManager pm = this.getPackageManager();

setContentView(R.layout.main); 
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
    List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0); 
    Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm)); 

    TextView c=(TextView)findViewById(R.id.textView1); 
    for(int i1=0; i1<appList.size(); i1++){ 
      c.setText(c.getText() + "\n" + 
        "number: " + i1 + "\n" + 
        "Name: " + appList.get(i1).loadLabel(pm) + "\n" 
        ); 
    } 


    Intent i11 = new Intent(); 
    i11.setAction(Intent.ACTION_MAIN); 
    i11.addCategory(Intent.CATEGORY_LAUNCHER); 
    i11.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
    i11.setComponent(new ComponentName(appList.get (3).activityInfo.applicationInfo.packageName, appList.get(3).activityInfo.name)); 
    startActivity(i11); 
相关问题