Android get current running application package name from service

January 28, 2021, at 11:10 PM

I want to get the package name of current running application from service. To achieve this I have service which has timer task. Based on research I have ended up with below methods to get package name. But this is not giving the running app package name.

Approach 1 :

ActivityManager mActivityManager =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
                String packageName;
                if (Build.VERSION.SDK_INT > 20) {
                    packageName = mActivityManager.getRunningAppProcesses().get(0).processName;
                } else {
                    packageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();

Approach 2 :

ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
    Log.d("My Service : ", "runningProcesses size::" + runningProcesses.size());
    for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
        Log.d("My Service : ", "processInfo.importance::" + processInfo.importance);
        if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            for (String activeProcess : processInfo.pkgList) {
                Log.d("My Service : ", "package name::" + activeProcess);

How can I achive this. Any help would be appriciable.

Answer 1

Firstly add these 2 permissions in your Android Manifest file

<uses-permission android:name="android.permission.GET_TASKS" /> 
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />

Then for getting the current package name -

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        UsageStatsManager usm = (UsageStatsManager) getSystemService("usagestats");
        long time = System.currentTimeMillis();
        List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
                        time - 1000 * 1000, time);
                if (appList != null && appList.size() > 0) {
                    SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
                    for (UsageStats usageStats : appList) {
                    if (mySortedMap != null && !mySortedMap.isEmpty()) {
                        currentApp = mySortedMap.get(
    } else {
            ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
            currentApp = am.getRunningTasks(1).get(0).topActivity .getPackageName(); 
Github action with MySQL test database

Github action with MySQL test database

I have a CodeIgniter Web app connected with Mysql that is developed in the dockerI would like to do some unit test in the GitHub action fo ci/cd pipeline

JSF H:commandButton &#39;type=reset&#39; does not call action, &#39;type=button&#39; acts as submit

JSF H:commandButton 'type=reset' does not call action, 'type=button' acts as submit

I'm trying to implement a Reset button that clears all fields back to empty or to some database related default valueEven though my solution actually works, the problem is that this button also intercepts the ENTER key, which is wrong as that should only go to the actual...

JButton wont change size or position on JPanel

JButton wont change size or position on JPanel

I added a button into JPanel and tried to change the size and the position of the buttonI've tried different lines of codes but they wont work