Firing an Intent, but I can't seem to find out how to get the package name
Here is my current code:
public ListAdapter createAdapter() {
appNames = new String[] { "" };
appNamesList = new ArrayList<String>();
final PackageManager pm = getPackageMana开发者_高级运维ger();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
packs = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(packs, new ResolveInfo.DisplayNameComparator(pm));
for (int i = 0; i < packs.size(); i++) {
appNamesList.add(packs.get(i).loadLabel(pm).toString());
}
appNames = maker(appNamesList);
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, appNames);
return adapter;
}
I currently have this code updating a ListView for the user to select an app from the list. Now, on my Listener, I need to get the package name. Is there an easy way to get the package name from the code I already have? Or will I need to create another List of package names?
I feel dumb for asking this next question, but is there a way you can start an intent from just the app name? I already have that and it would be a lot easier.
Thanks.
Ok to achieve what you want you need to make some changes in your code.
Make package name part of your appNamesList like
appNameList = ArrayList<HashMap<String,String>>();
so when you will get the item in your listener using
listView.getAdapter().getItem(i);
you will get the package name as a part of this item.
now you can use it as you want.
If you are still confused or have any other doubt, let me know.
Are you able to call getPackageName() from the context of an Activity?
精彩评论