How to get the name of the main activity in the application?
How do I get the main activity in third-party applications of the system?
PackageManager pm= getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);
for (PackageInfo pi : packs) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("icon", pi.applicationInfo.loadIcon(pm));
map.put("appName", pi.applicationInfo.loadLabel(pm));
map.put("packageName", pi.packageName);
}
I know how to get the package name. But don't know how to get the name of the main activity.
I am using pi.activities [0] .name. It still gets a null pointer.
Anyones idea is greatly appreciated.
+3
source to share
2 answers
using
pm.getLaunchIntentForPackage(pi.packageName);
This will give you the launch intent, if you print it you will see the launch intent and the main action.
If you only want to show the activity that can be launched from the device launcher, you need to put a filter for the Launcher category .
+2
source to share
You can use the following
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "unknown)");
+1
source to share