For the application I write crash stability is quite important. I found that if app doesn't have autostart permission it does not receive properly onTaskRemoved callback in Service . At least this happens on my Xiaomi smartphone. When I turn this permission on manually callback started to be called.
But!! while I turned this permission on manually I found that there are applications that received this permission without help. I.e. Facebook, AliExpress, Moovit etc.
How did they received this permission? I tried to receive RECEIVE_BOOT_COMPLETED. This doesn't help. What is the trick?
Related
Recently my app keeps crashing when registering a contentObserver for the Android CallLog (in onCreate of a Service). This is the way I register it:
getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, new MyObserver(new Handler()));
Part of the stacktrace:
java.lang.RuntimeException: Unable to create service
nl.xelion.restandroid.service.CallLogChanged:
java.lang.SecurityException: Permission Denial: opening provider
com.android.providers.contacts.CallLogProvider from
ProcessRecord{f73a1ed 7949:nl.my.app.debug/u0a119}
(pid=7949, uid=10119) requires android.permission.READ_CALL_LOG or
android.permission.WRITE_CALL_LOG
...
Caused by: java.lang.SecurityException: Permission Denial: opening provider...etc
I used this tutorial, and it DID work for about a year:
http://www.adityathakker.com/android-content-observer-react-on-content-change
Approximately since Android O the crashes started to appear. The javadoc of registerContentObserver does say something along these lines: Starting in O, all content notifications must be backed by a valid ContentProvider. So I was wondering if this would be the problem, and if so, how to solve it?
Anyway, why is it complaining about permissions for contacts, and why didn't this happen before? This doesn't make sense to me.
ps. I do use code which requires permission for contacts, which is called in the onChange of the ContentObserver, but even when I comment this piece of code the crash still occurs.
The problem lies with the update to Android O. Permissions are grouped together and since Android O permission behaviour was changed because of incorrect handling of permission requests.
In my example I had added the permissions READ_CALL_LOG/WRITE_CALL_LOG and CALL_PHONE to the manifest, but requested only CALL_PHONE at first startup of the app. This caused the permission group PHONE to be accepted, but neither of the call-log permissions. So when the registerContentObserver method was called, Android implicitly accepted it to be used because of accepting the group PHONE.
When my user upgraded from N to O, and registerContentObserver was called again, it would crash (justly). Solution: check for the READ_CALL_LOG/WRITE_CALL_LOG permisions before calling registerContentObserver.
Workaround for users on Android O who're still using the old - crashing- code: Manually turn off & on the specific permission in the Android settings of the app (so the PHONE permission group in this case). This causes all permissions in this group to be accepted (or at least those permissions in that group that are required by the app)
I am implementing a service that uses LocationManager to get and utilize the tablet location. This service is start and stop from an activity.
The latest Android requires that permissions are requested on runtime. Now I have managed to do this on an activity by using requestPermission in onCreate , checkSelfPermission everytime I use some Location manager function, and adding the requestPermission function and overriding the onRequestPermissionResult.
It works great.
Now for my service I need to do the same, but these functions seems to work only for activities. How can I activate permissions in a Service?
just in case, I have already asked for permissions in the activity that starts and stops the services
How can I activate permissions in a Service?
You don't. You activate (i.e., request) permissions from an activity. That is not negotiable.
Ideally, you request permissions before the activity starts the service or does something that will eventually cause the service to start (e.g., schedules the job with JobScheduler).
If you determine that your service no longer has the necessary permissions — perhaps the user revoked them from Settings — you could raise a Notification that leads the user to an activity where you re-request the permissions.
It is technically possible for a service to start an activity which requests the permissions. Usually, this is not a good idea, as you may not know what the user is doing at that moment, and the user may be unhappy to have you interrupt them with this permission request.
How can I activate permissions in a Service?
You can't request for permissions from services. Permissions should be asked explicitly which should be visible to the user in UI. However you can ask permission from activity and, if succeed, you can access those resources until user again turned off permission for your app.
how can you "transfer" these permissions to the service?
Permission is assigned for the entire app, so you don't need to transfer it from one activity to another or from one activity to service. Once you get a permission in an Activity, that permission is assigned to the entire app and your services can access the resources then after. cheers :)
I know, with Android Marshmallow, we have to ask permissions every time we need a functionality that require a permission.
I do this for new App, but for old BIG App is more complicated.
For old BIG App, I ask all permissions in "MainActivity".
So, my app is started and shows a Map Fragment.
I ask for Location Permission with no problem. The user agrees.
The user puts it in background, goes in the App Settings and denies permission.
Then he puts the App in foreground: App crashes!!
I know, there is a problem using Map Fragment.
But the App is too BIG to analyze every single permission.
I need the FAST solution: PERMISSION CHANGE ---> RESTART APP!!!
And this is what GoogleMaps does!
I started GoogleMaps, and put it in background.
GoogleMaps App permissions, the first difference:
in my App, if I denied a permission, I haven't this message showing for Google Maps:
Permission denied
After that, if I put GoogleMaps in foreground, the App restart with no problem:
GoogleMaps restart
I need this for my app. Can someone help me?!?!?!?
Thanks a lot!!
When permissions are revoked, your app is killed and restarted. Look on the logcat when you revoke permission with your application in background. You will see :
I/ActivityManager(572): Killing 13896:com.your.app/uxxxx (): permissions revoked
So you need to only check permissions in your class "myApplication extends Application" in the onCreate() function.
I followed all the guidance found on SO to autostart app at boot time.
In my manifest, I have set the right permission (RECEIVE_BOOT_COMPLETED) and also declared my broadcast receiver: .BootReceiver picking up Android.Intent.Action.BOOT_COMPLETED.
I also launch my service in BootReceiver, this is very straightforward stuff.
The thing is, my app starts at boot time on certain devices (I hope most devices) but not on some of them. I have a Xiaomi phone that gives me the following error at boot time:
"Unable to launch app com..example/10120 for broadcast Intent {act=android.intent.action.BOOT_COMPLETE flg=0x8000010 (has extras) }: process is not permitted to autostart."
I am surprised to see this message, because I can see that the list of permissions includes running at startup.
There must be a way, because Whatsapp for instance is launched at boot time.
Any clue would be highly appreciated.
Same here. I tried to delete the file in the SD card ( but the system direct to internal storage ) by using the permission WRITE_EXTERNAL_STORAGE, and get the similar message from log-cat. Maybe it can be fixed by rooting the xiao-mi mobile, that's what I searched from the Internet.
Im trying to force my phone to go to sleep as soon as i turn my screen off, but whenever i install with adb, packagemanager informs me that it wont grant the permission i need, so my service throws an exception. I was able to install another app called power save mode toggle which has the same permission, and it seems to work, so i should be able to get this to work, right?
Without knowing which permissions you did grant, or the AndroidManifest.xml to ensure you placed them in the correct area, try granting these:
DEVICE_POWER
MODIFY_PHONE_STATE