I want to know how to get the package name when I open an app, for instance when I open Facebook I should get its package name and when I open another app I should get its package name how do I do this?
-
aapt dump badging
| grep package:\ name – Jon Goodwin Jul 13 '17 at 17:38 -
connect using adb, you can launch adb shell and execute pm list packages -f, which shows the package name for each installed apk. – Jon Goodwin Jul 13 '17 at 17:39
-
@JonGoodwin sorry to not mention this earlier I am creating an android application. – Ciddarth Raaj Jul 14 '17 at 13:43
-
You check this I gave complete detail about this, [Getting running app in background](https://gist.github.com/rmkrishna/7e133bcf1a918a02f2c882c1d7011b18). – Muthukrishnan Rajendran Jul 14 '17 at 13:51
-
hey did you found solution bro..please update answer bro – Gowthaman M Dec 18 '17 at 11:01
3 Answers
I does not show you the package name when you open the specific app, but you can use this code to list the package names of all apps installed on your device.
I don't think an Android app can have the privilege to "listen" for the launch of another app. You could only achieve this with a custom ROM.
You can do this with the PackageManager
.
final PackageManager manager = getPackageManager();
List<ApplicationInfo> packages = manager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo info : packages) {
Log.i("Info", "Installed package:" + info.packageName);
}

- 27,862
- 20
- 113
- 121
-
I know how to find all the package name.But I want to make a app locker so what is the other way to do this? – Ciddarth Raaj Jul 14 '17 at 13:54
For get the package name for the running app in Background you need to run one service and call the below method every 100 or 300 milliseconds, it will give you the current running package name. for more detail you can see this link, In this method AndroidUtils
you can find in the link.
public String getRecentApps(Context context) {
String topPackageName = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 30, System.currentTimeMillis() + (10 * 1000));
UsageEvents.Event event = new UsageEvents.Event();
while (usageEvents.hasNextEvent()) {
usageEvents.getNextEvent(event);
}
if (event != null && !TextUtils.isEmpty(event.getPackageName()) && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
if (AndroidUtils.isRecentActivity(event.getClassName())) {
return event.getClassName();
}
return event.getPackageName();
} else {
topPackageName = "";
}
} else {
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
// If the current running activity it will return not the package name it will return the activity refernce.
if (AndroidUtils.isRecentActivity(componentInfo.getClassName())) {
return componentInfo.getClassName();
}
topPackageName = componentInfo.getPackageName();
}
return topPackageName;
}
If your app is AppLocker you should call this method more frequent may be every 100ms then only you can lock other apps.

- 11,122
- 3
- 31
- 41
There is one third party app in playstore ,which shows 'Package Name' along with Activity's name .Find it over here:
https://play.google.com/store/apps/details?id=com.willme.topactivity&hl=en_IN

- 1,471
- 4
- 17
- 37