Android Usage Access for Android 5 Samsung devices - android

As you might know, since Android 5 was launched, accessing the recent tasks (usage stats) of your device requires the user to enable this feature manually (Settings->Security->Usage Access).
My app checks if the device uses Android 5, and if so, then it offers the user the possibility of opening the settings screen for enabling usage access:
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivityForResult(intent, 12345);
The problem comes when I try to do this in a Samsung device running Android 5... I got this error when the line shown above is executed:
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)
It seems that the Settings.ACTION_USAGE_ACCESS_SETTINGS action has not been implemented, which is weird because if you go to Settings the option to enable the app to access this usage stats is there...
Any idea of how to fix this problem for this specific case?

Sorry for the late reply, I hope you have found a good solution, but if not or for other people who have this problem,
this bug seems to exist in some Samsung models, to avoid the crash of your app, maybe you can check if your intent can be handled
PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent);
} else {
Toast.makeText(getBaseContext(), "Please open settings, your model can't be forced
to open it", Toast.LENGTH_LONG).show();
}

Related

How to programmatically determine if an Android app is put to sleep on Samsung Galaxy?

Samsung Galaxy has the feature to put apps to sleep - which to my understanding is not the same as battery optimization for apps.
I need to programmatically check if an app is configured to sleep so I can properly warn the user about this and the fact that background tasks won't work.
I am aware of PowerManager.isIgnoringBatteryOptimizations() which however does not seem to be related to putting an app to sleep or not.
The thing with Samsung sleep feature is that it 'differs' from 'background optimizations'. Their feature is designed to freeze apps(just like freeze in the settings) after 3 days, but depending on user preference, it can be extended to 7 days. Currently, there is no known way to programmatically check if the app has been put to sleep.
What you can do is to inform the user about this and ask to 'whitelist' the app from imminent sleeping.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.samsung.android.sm", "com.samsung.android.sm.ui.battery.BatteryActivity"));
try {
getActivity().startActivity(intent);
} catch (ActivityNotFoundException exception) {
//Manager not installed
}
Code source
This code will try to open BatteryManager, from there, a user can select options to prevent from app sleeping.
Here are the relevant questions:
How do I check that my app enters optimizations Samsung devices?
Samsung “App optimization” feature kills background applications after 3 days
This blog should help you in figuring out the ways to inform the user about the issue

Android Network Operator Settings Intent not working correctly

I am trying to open the Network Operator Settings view with the following code:
startActivity(new Intent(android.provider.Settings.ACTION_NETWORK_OPERATOR_SETTINGS));
It works correctly on all the devices I could test, but on one of them (Alcatel One Touch Pixi, with Android 5.1) the Network Operator Settings view opens and automatically closes after that. I tried to see if the resolveActivity with the packageManager of that Intent returns null, but it does not, it opens the activity of network operator settings and then (for some reason) it automatically finishes.
Anyone can help me to fix this issue that only happens with some specific mobiles?
There's an alternative way to call the Network Settings menu:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.phone", "com.android.phone.MobileNetworkSettings"));
startActivity(intent);
This method works with Samsung devices but not sure about devices that you mentioned (since I'm specifying the name of the package and the activity class name).
I think you can try and if works, you may add the proper conditions to use this code etc.
You cannot fix the issue. The other app has a bug. Only its developers can fix the bug.
wireless settings
startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
Network Operator Settings
startActivity(new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS));

How to check application is white list in MI security permission autostart

I am building an android application where I am using some services. My services get close when application is close in some custom android OS like MI.
Then I figure out we have to push our application in white list of security autostart application.
I got some SOF link's where they are suggesting below answer.
String manufacturer = "xiaomi";
if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}
This above code used to send user to that particular page.
Now I have to check if my application is disable so I can send user to this page and if it is enable then move to other screen. Is there any way to check this?
There is no Android API to check if the the AutoStart is enabled or not. Though you can have your own logic in the App (may be you can use preference to set a Boolean for it) to check this. Also the above way to enabling the AutoStart might not work always. Please have a look here

Android "Screen Overlay Detected" message if user is trying to grant a permission when a notification is showing

I have Android Marshmallow on a Nexus 6. I am trying to fix the following problem:
If a user is trying to grant permission while a notification is showing, a "Screen overlay detected" message gets displayed and the Request Permission dialog disappears - of course the app does not get the requested permission. (Check screenshot)
I tried to fix the problem by adding "DRAW OVER OTHER APPS" permission - android.permission.SYSTEM_ALERT_WINDOW to the manifest but with no luck.
PS: I am sure the problem is caused by the notification. I do not have any app installed that overlays over other apps, I even turned off all apps with "Draw over other apps" permission in the settings. Did not help..
Anyone knows a solution to that problem?
In the circumstance that I ran across, I was causing the problem myself. It was the result of using a Toast to display information to the user at the same time that I was asking for permission. Both of these actions together cause this type of error.
The other answers might resolve someone else's issue. But I wanted to note that you should be cautious of causing your own overlays errors. Be careful of overlaying something in the view while simultaneously asking for permission.
Uninstall Clean Master app. I uninstalled it and problem solved
This problem appear because of some culprit application like Twilight, cleaner-master, drupe etc..
To solve this problem you have to disable screen overlay for those culprit apps.
i have moto g4 plus, and this is how i solve this problem
Go to Setting --> Select Apps ---> again select setting icon in Apps ---> select draw over other apps ---> and disable culprit apps who trouble for other apps.
what i done is checking each apps by disabling this permission and try to run my app, and i found one app this troubling overlay for other apps, so at the end i disabled only this app.
ScreenShots:
Got insights from multiple answers here and other forums .
Consolidating how I got rid of the issue :
Go to Settings > Apps > (your app which is getting issue)
Press on Power button till window for Power off , reboot , airplane mode comes up
Hold on Power off option
Select reboot in Safe mode
Go to settings > apps > (your app which is getting issue)
Select whichever permissions you want
After Android M update , issues can come up in apps like Messenger , Whatsapp , Prisma etc.
Let me know if any issues .
Note : I am having One plus One mobile.
This popup is caused by the manifest.PERMISSION.SYSTEM_ALERT_WINDOW permission declared by the manifest.
The are 3 categories of permissions, that developer must be aware of :
Normal permission - do nothing with them, just declare in the Manifest
Vulnerable permissions - declare in Manifest and ask for permission at first time. They can be changed through system settings.
Above dangerous permissions: SYSTEM_ALERT_WINDOW and WRITE_SETTINGS belong to this category. They must be granted, but are not visible in system settings. To request for it you don't use a standard way (int checkSelfPermission (String permission)) but you have to check Settings.canDrawOverlays() or Settings.System.canWrite() appropriately and if you not do that you will get exception like
Unable to add window android.view.ViewRootImpl$W#1de28ad -- permission denied for this window type
1-Request this permission by yourself in your code just like given below:
public class MainActivity extends AppCompatActivity {
public final static int REQUEST_CODE = 10000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkDrawOverlayPermission()) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
}
}
public boolean checkDrawOverlayPermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
return false;
} else {
return true;
}
}
#Override
#TargetApi(Build.VERSION_CODES.M)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
}
}
}
I just deleted my app and turned off my Nexus 6P. After turning it back on, I reinstalled the app and no longer got the "screen overlay" dialogs when giving the app permissions.
You must disable the overlay for all the apps you see in the list. Only this way can you modify authorizations in the app you need.
I've done that in safe mode, and it worked.
At the end I rebooted the phone and now it is working fine.
I updated my Sony Xperia Z3 (Dual Sim) to Android 6.0.1 (Marsmallow). I have been having screen overlay issues. For me i do not have Clean Master, Du Speed, or Du Booster(as the solutions i have read).
So i solved mine looking for any screen overlay apps.
A screen overlap app, is an app that you can use to access other apps on your main home screen without leaving your home screen. So for me the Screen Overlay App here in my situation was the OMNI SWIPE. So if you are facing this problem you need to calm down and check which of your app fits the definition of a screen overlay app.
locate the app and uninstall then restart your phone ..
i just finished doing this and am having a good time with the phone
Best of Luck
As long as Android 6.x is buggy on some devices where this "overlay alert" is displayed without any reason (on 2 to 5% of the devices according to my analytics data), the best solution is to avoid the whole permission process by defining the targetSdk to 22.
Take care that you can't downgrade the target sdk for a new version or this will induce a INSTALL_FAILED_PERMISSION_DOWNGRADE error when the user updates requiring an unisntall/install of the app.
solution is
remove Toast messages from onRequestPermissionsResult method
This happens when you have granted overlay permission to malicious apps. Go to overlay settings and disable the overlay feature on all apps that don't belong to google and you will be good to go.
I got this problem when installing a new app.
The way I got around this problem is to manually enable the permissions for the newly installed app (before running the app).
I’m pretty sure this is a problem with Android and Samsung devices in particular.
Hope this helps
Delete the apps which have screen overlay like CM security, Clean Master, etc.
Even delete and try with Messenger (FB app) if needed.

ACTION_OPEN_DOCUMENT not working on MIUI

I want to use ACTION_OPEN_DOCUMENT on my Xiaomi Device. I tried
this google sample, but it also not working. With code below i can normally run on Samsung galaxy s4.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);
Im getting "android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.OPEN_DOCUMENT_TREE }" error.
Xiaomi screwed up, apparently. AFAIK, they're not Google Play certified, so they do not need to pass the CTS. There is nothing you can really do about it, other than to detect this case (e.g., use PackageManager and queryIntentActivities(), or catch the ActivityNotFoundException) and fall back to whatever you do on pre-Android 4.4 devices.
I saw the same error occur on Xiaomi MI 6X with Android 10.
Upon some digging I realized that some devices allow the user to disable the "Files" app from Google Play Services (or might even do that by default). Thus, I would recommend first prompting user for enabling "Files" app and/or updating the Google Play Services. Please note, that on most devices the "Files" app is hidden from the user, thus it will not be present on app list in preferences.

Categories

Resources