0

I need to check by packageName - running application or not. Until v.21 I simply get list of running applications and dance. But now it not working - returns only one my app. I know that I need use "Statistics Usage", but can't find completely example for it.

I don't need all this analytics, I need one simple function

bool isRunning(String packageName)

How to implement it on new Android (>21) without dancing arround it?

upd. Answer of code4rox the best. All is OK, thanks!

I use this code for check rights:

import android.content.pm.PackageManager;
import android.content.pm.ApplicationInfo;
import android.app.AppOpsManager;
import android.util.Log;
import  android.provider.Settings;

try {
            PackageManager packageManager = getContext().getPackageManager();
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getContext().getPackageName(), 0);
            AppOpsManager appOpsManager = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE);
            int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
            if (!(mode == AppOpsManager.MODE_ALLOWED)) {
                Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
                startActivity(intent);
            }
        } catch (PackageManager.NameNotFoundException e) {}

And this for check app:

import android.util.Log;
import android.app.usage.UsageStatsManager;
import android.app.usage.UsageStats;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.ArrayList;

public boolean isRunning(Context context, long timeMills, String app) {
        boolean result = false;
        //timeMils = 1000
        UsageStatsManager usm = (UsageStatsManager)context.getSystemService(Context.USAGE_STATS_SERVICE);
        long time = System.currentTimeMillis();
        List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,  time - timeMills, time);
        if (appList != null && appList.size() > 0) {
            for (UsageStats usageStats : appList) {
                String appname = usageStats.getPackageName();
                Log.i("DOM_LAUNCHER", "Running app: " + appname);
                if (app.equals(appname)) {
                    result = true;
                }
            }
        }
        return result;
    }
Borisov
  • 55
  • 1
  • 7

2 Answers2

2

There is no any way to get the all running apps in API level (>21) for the security reasons.

BUT

You can access to app usage history and statistics with the time intervals: days, weeks, months, and years with UsageStatsManager

Here is official Docs Link

The Other Apps like clean master , ccleaner use this technic to get running apps.

Here is the Example to get apps list using UsageStatsManager

Note: You must give Usage Access Permission before use UsageStatsManager

code4rox
  • 941
  • 9
  • 34
0

You can visit this documentation. Scroll down to the section of methods. Some of the methods need permission and some of them are only intended to be used only for debugging. Hope that helps.

  • 1
    These methods deprecated and not working many years. I need like this https://stackoverflow.com/questions/30619349/android-5-1-1-and-above-getrunningappprocesses-returns-my-application-packag But it returns list apps that was active some mills ago, but I need a RUNNING apps – Borisov Feb 11 '19 at 09:23
  • In what android version are you looking to implement the feature? – Hasanuzzaman Mamun Feb 11 '19 at 09:32
  • for sdk >=25 version . – Borisov Feb 11 '19 at 09:52