[英]How to get all apps installed on android phone
這是我目前的代碼,但我仍然無法獲得手機上所有應用的列表。 有誰看到我做錯了什么?
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 ...
}
}}
抱歉,無法正確格式化代碼。
此代碼記錄所有已安裝應用程序的名稱和包名稱。 您可以使用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));
}
這是我用來列出應用程序的代碼:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.