I use the code below to get the launcher activity name belongs to specific package name:
Intent intent = new Intent(); intent.setPackage(aPackageName); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo result = getPackageManager().resolveActivity(intent, 0); I save result.activityInfo.name to shared preference
Later I want to start this activity, but how to get its package name?
or, Is it possible to start this activity without knowing the package name it belongs to?
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(How to get the package name); if(launchIntent != null){ startActivity(launchIntent); } Knowing that the activity(s) name(s) that I save are not mine.
95 Answers
To answer this,
How to get Package name from activity name
If you want to find the package name from the Launcher activity name, please check the following,
String activityName = "TermuxActivity"; PackageManager pm = getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0); Collections.sort(activityList, new ResolveInfo.DisplayNameComparator(pm)); for (ResolveInfo temp : activityList) { if(temp.activityInfo.name.endsWith(activityName)){ Log.i("ActivityCheck", " Activity : " +temp.activityInfo.name+ " package name: " +temp.activityInfo.packageName); } } Output:
ActivityCheck: Activity : com.termux.app.TermuxActivity package name: com.termux Try this :-
public static String PACKAGE_NAME; PACKAGE_NAME = getApplicationContext().getPackageName(); 1You can use this given below code to get package name of application
In Java supported code:
String packageName=this.getPackageName(); // where this represent the context In Kotlin supported code:
var packageName:String=this.packageName // where this represent the context 4Use the following code to get the launcher activity of all packages from your installed apps:
final PackageManager pm = getPackageManager(); 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)); for (ResolveInfo temp : appList) { Log.v("my logs", "package and activity name = " + temp.activityInfo.packageName + " " + temp.activityInfo.name); } The only reliable way was Vladyslav Matviienko suggestion, which is storing package name along with each activity name in a hashmap to be like .. Thank you all for your help.