-2

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.

Jack
  • 693
  • 1
  • 7
  • 25
  • 3
    https://stackoverflow.com/questions/6589797/how-to-get-package-name-from-anywhere?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – IntelliJ Amiya May 02 '18 at 11:33
  • 1
    store a package name as well – Vladyslav Matviienko May 02 '18 at 11:37
  • The activity I want to start doesn't belong to me. – Jack May 02 '18 at 11:37
  • it doesn't have to. You have package name already in `aPackageName` – Vladyslav Matviienko May 02 '18 at 11:37
  • As I mentioned above, I only save the activity name, and when I retrieve it, I want to start it, but all methods I found needs the package name, which I don't have it. – Jack May 02 '18 at 11:39
  • Vlad is right you should store package name as well ... now the only solution is to iterate all packages and all Activites in package to find out where you stored activitybelongs – Selvin May 02 '18 at 11:41
  • The activity name I retrieve / save is full path that includes the package it belongs to, so there's not a method that returns me the package this activity belongs to. – Jack May 02 '18 at 11:44
  • `I only save the activity name` then save a package name as well, not only the activity name. – Vladyslav Matviienko May 02 '18 at 11:58
  • @VladyslavMatviienko Thank you, I already have this in mind as a last resort if there's no public method to achieve what I want. – Jack May 02 '18 at 12:00

4 Answers4

1

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
Hardian
  • 1,922
  • 22
  • 23
0

Try this :-

public static String PACKAGE_NAME;
PACKAGE_NAME = getApplicationContext().getPackageName();
anoopknr
  • 3,177
  • 2
  • 23
  • 33
0

You 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
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
  • The application doesn't belong to me to use getPackageName() – Jack May 02 '18 at 16:05
  • Please clarify what you want to do? – Hemant N. Karmur May 03 '18 at 04:41
  • I receive a package name when a new application gets installed, and I want to find the name of the main / launcher activity belongs to this package, so later I can build a resolveInfo, which requires both package name and activity name. – Jack May 03 '18 at 07:46
  • Means you want all launcher activity names from app apps which you have installed on your device. Right ? – Hemant N. Karmur May 04 '18 at 05:25
0

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.

Jack
  • 693
  • 1
  • 7
  • 25