问题
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.settings.USAGE_ACCESS_SETTINGS), the app crashes (stacktrace below)
Observed that another developer noticed this issue in LG G3 phone - thread here.From user logs, I have mostly seen this occur on LG G3 and once on Samsung S5 also.
What is the right intent to launch the window for LG & Samsung S3 phone to get app usage data?
Does someone have either of these phones and can advice if this "Apps using Usage Data" permission option even exists
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.USAGE_ACCESS_SETTINGS }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1801)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1499)
at android.app.Activity.startActivityForResult(Activity.java:3913)
at android.app.Activity.startActivityForResult(Activity.java:3860)
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivi ty.java:849)
at android.support.v4.app.Fragment.startActivity(Fragment.java:880)
at com.mavdev.focusoutfacebook.fragments.addablock.apps.Fragment_appsselect_addbloc k$2.onClick(Fragment_appsselect_addblock.java:182)
at android.view.View.performClick(View.java:5162)
at android.view.View$PerformClick.run(View.java:20873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Here is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hidden"
android:installLocation="internalOnly"
android:versionCode="50"
android:versionName="2.0.5" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:allowClearUserData="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/MyActionBarTheme"
android:vmSafeMode="true" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
I dont have a real LG device (or even Samsung S3) with Lollipop on it to test. I am only testing in the Genymotion Emulator with Android 5.0 (API21) and it works fine.
回答1:
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;
}
});
回答2:
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
回答3:
you need add activity to manifest xml like that
(under application)
<activity
android:name="com.yournamespace.yourclassname"
android:label="@string/app_name" >
</activity>
来源:https://stackoverflow.com/questions/28361629/activitynotfoundexception-in-lollipop-when-trying-to-launch-activity-with-intent