I am trying to get the permission to access app usage data using this permission. THis is only being done for Lollipop and when I start activity with this intent (android.se
Does someone know how to listen to changes in that setting, with code similar or analogous to
Context context = configuration.getContext();
ContentResolver resolver = context.getContentResolver();
resolver.registerContentObserver(Settings.System.CONTENT_URI, true, observer = new TimeoutSettingContentObserver(context, new Handler()));
Here is the code:
PackageManager packageManager = getPackageManager();
final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
appOpsManager.startWatchingMode(AppOpsManager.OPSTR_GET_USAGE_STATS,
applicationInfo.packageName,
new AppOpsManager.OnOpChangedListener(){
@Override public void onOpChanged(String op, String packageName) {
int mode = appOpsManager.checkOpNoThrow(op, applicationInfo.uid, packageName);
boolean enabled = mode == AppOpsManager.MODE_ALLOWED;
}
});
I checked the same code on google nexus 5.0 and emulator , it works fine .It didnot show any error .Intent works fine and Apps usage acess actvity is also launched though it didnot list any apps under this .
Under settings -->security --> APPS usage -- first check your device list any apps .
Try the below code and check whether your application has usage acess enable
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
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 false;
}
And also you can refer to this link this link
you need add activity to manifest xml like that
(under application)
<activity
android:name="com.yournamespace.yourclassname"
android:label="@string/app_name" >
</activity>