ActivityNotFoundException in Lollipop when trying to launch activity with intent android.settings.USAGE_ACCESS_SETTINGS
Asked Answered
C

3

5

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.

Continuation answered 6/2, 2015 at 8:35 Comment(9)
Please paste your Manifest.Klein
Just added the manifest file. I have removed the activities and receivers under the application tab - rest all is there.Continuation
I did add the following permission: "<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />"Continuation
Related to #28297133 ?Plagio
Folks, it appears this entire library is missing or hidden in the builds of Android 5 shipped by LG and Samsung. Long live fragmentation. It works fine on the Moto X build of 5, and the build on Nexus devices.Limber
Note: Samsung's OTA update for S4 does have the feature. So far it is anecdotally noted to be absent in the LG G3 and Samsung S5.Limber
The issue also occurs on the LG G Flex 2. The usage stats screen is nowhere to be seen on the any of the settings screens.Rawinsonde
Possible duplicate of android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.USAGE_ACCESS_SETTINGS }Contingent
Voting to close this as duplicate since the other question is slightly older and has a correct accepted answer.Contingent
G
3

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_USAG‌​E_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; 
            } 
        });
Gorden answered 19/2, 2015 at 15:33 Comment(1)
@SunilChaudhary, usage stats was only introduced in Lollipop (21).Contingent
B
0

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

Breadroot answered 16/2, 2015 at 6:15 Comment(0)
L
-5

you need add activity to manifest xml like that

(under application)

<activity
       android:name="com.yournamespace.yourclassname"
       android:label="@string/app_name" >           
</activity>
Latia answered 6/2, 2015 at 8:59 Comment(2)
I have my activities and receivers already added under application tag. I did not show it here (as I mention in my question also) for privacy reasons.Continuation
In any case, <activity> must be inside, not under, <application>Oral

© 2022 - 2024 — McMap. All rights reserved.