简体   繁体   中英

Android: How to check application is Running or not

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;
    }

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM