5

I am trying to get a list of currently running apps on my phone. Here is the code I am using:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            UsageStatsManager usm = (UsageStatsManager)this.getSystemService(Context.USAGE_STATS_SERVICE);
            long time = System.currentTimeMillis();
            List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,  time - 10000*10000, time);
            if (appList != null && appList.size() == 0) {
                Log.d("Executed app", "######### NO APP FOUND ##########" );
            }
            if (appList != null && appList.size() > 0) {
                SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
                for (UsageStats usageStats : appList) {
                    Log.d("Executed app", "usage stats executed : " +usageStats.getPackageName() + "\t\t ID: ");
                    mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
                }
                if (mySortedMap != null && !mySortedMap.isEmpty()) {
                    String currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();

                }
            }
        }

Here I have set the permission as well:

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

It does not detect any app usage. I have tried some other things on stackoverflow but nothing seems to work. I have even read somewhere that the ability for apps to monitor other apps running on the phone has been removed completely in the newer phones. Would greatly appreciate any help!

anon_945500
  • 269
  • 3
  • 12
  • Have you tried `ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List runningAppProcessInfo = am.getRunningAppProcesses(); ` – SteelToe Apr 10 '18 at 00:00
  • And then `runningAppProcessInfo.get(i).processName` To get a string representing the app package name for each process – SteelToe Apr 10 '18 at 00:00
  • 1
    @SteelToe I have. That only give me my own app and google search bar whereas there are plenty of other apps running on the phone. – anon_945500 Apr 10 '18 at 00:06
  • Did you make sure to get the proper permissions when trying your code? – SteelToe Apr 10 '18 at 00:17

1 Answers1

1

The problem is that your application does not have the system permission of android:get_usage_stats.

You can use the below code to check if you have the permission:

public static boolean needPermissionForBlocking(Context context){
try {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
    return  (mode != AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
    return true;
}
}

If you do not have the permission then the user must enable this permission by going to Settings -> Security-> Apps with usage access, and then adding your application. Your code should then work fine.

SteelToe
  • 2,477
  • 1
  • 17
  • 28