3

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?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Ciddarth Raaj
  • 55
  • 1
  • 6

3 Answers3

0

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);
}
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
0

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.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
0

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_INenter image description here

Ajay Chauhan
  • 1,471
  • 4
  • 17
  • 37