ForegroundApp Checker returns null after an hour of running through service :

18 views Asked by At

I am using the below code which works fine till an hour afterwards it returns foreground app package name null

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

To check the runtime permissions :

void requestUsageStatsPermission() {if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP&& !hasUsageStatsPermission(this)) {startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));}}
@TargetApi(Build.VERSION_CODES.KITKAT)boolean hasUsageStatsPermission(Context context) {AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);int mode = appOps.checkOpNoThrow("android:get_usage_stats",android.os.Process.myUid(), context.getPackageName());boolean granted = mode == AppOpsManager.MODE_ALLOWED;return granted;}


To check the foreground app package name :

AppChecker appChecker = new AppChecker();String packageName = appChecker.getForegroundApp(context);

after an exact hour packageName returns null.

for the library reference : https://github.com/ricvalerio/foregroundappchecker

any help will be appreciated thanks!

I have checked and service is running continuously and doesn't stop anytime.

0

There are 0 answers