PackageManager: Not granting permission - android

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

Related

What happens when a permission is disabled at runtime?

I couldn't find any information about what happens when the user disables an app's permission while the app is running.
Is the application re-initilized?
I saw that in some apps if a Dialog or BottomSheet is open while I disable the permission, the dialog is no longer displayed when I return to the app.
Can anyone explain what happens in-detail when a permission is denied at runtime? Or does anyone have some useful links for me?
I would be especially interested in which lifecycle events are called when returning to the app.
When a previously granted permission is revoked through settings, the app is force stopped. You can see this by watching your app in the debugger. The app process is marked DEAD as soon as the permission is revoked.
Returning to the app will launch it from the main activity. I've never really looked into why this happens, but I assume it's because when a granted permission is revoked, the user could be deep into the app at a place where it is assumed the permission is granted. When the permission is revoked, there's no way to know if the screen they are currently in is even valid anymore.
Upon returning to the app, the app's state is restored and your current activity will be restarted, similar to a configuration change. If the activity you are in assumes a certain permission is granted, you should probably check that permission again in onCreate() to make sure you have it.
Simply put, That depends on what the app is trying to do when it needs permission.
For example: If we live in a country that requires you to be an adult to watch any video on YouTube, nothing will work with Location permissions denied
Another example: If you want to take photos using your phone via an app, the Camera permission should be permitted.
Under some circumstances, just part functions of app can not be used, but at an Extreme case, app would throw Security Exception and crash.
According to your point :
I saw that in some apps if a Dialog or BottomSheet is open while I
disable the permission, the dialog is no longer displayed when I
return to the app.
There is no lifecycle callback about what you do once permission is denied, but there's method on ActivityCompat which gives you flag if you want to show your own Dialog/BottomSheet
So, you can call shouldShowRequestPermissionRationale() method from ActivityCompat & make your own logic work when it's true.
shouldShowRequestPermissionRationale :
Gets whether you should show UI with rationale for requesting a permission. You should do this only if you do not have the permission and the context in which the permission is requested does not clearly communicate to the user what would be the benefit from granting this permission.
For example,
if you write a camera app, requesting the camera permission would be expected by the user and no rationale for why it is requested is needed.
If however, the app needs location for tagging photos then a non-tech savvy user may wonder how location is related to taking photos. In this case you may choose to show UI with rationale of requesting this permission.
While disabling permission for first time will give you callback in onRequestPermissionResult() method.

(ANDROID) "requires appop SYSTEM_ALERT_WINDOW" even though already granted

I have an Android app that uses the SYSTEM_ALERT_WINDOW permission. I have a runtime check to make sure the permission is granted. When I sideload the app (for dev), it prompts me for the permission as usual. However, when a user installs it from the Play Store, the permission is (allegedly) automatically granted, but when they try to use it, an error is logged saying: Appop Denial: Accessing service ComponentInfo{<package>} from pid=<pid>, uid=<uid> requires appop SYSTEM_ALERT_WINDOW (basically saying the permission isn't granted). The weird part is, if the user goes to the app's settings and revokes and then permits the permission, it works. Any ideas why this is happening??
I have seen this same problem a number of times and found various answers to the problem. One solution that I recall had to do with the fact that when you request permission for SYSTEM_ALERT_WINDOW and then prepare a Toast after the result of the permissions, it would throw this same error. It has to do with the fact that the Android system perceives the toast as a secondary 'alert' and throws the above exception when it happens in succession.
If you are creating any type of Toast after the result check of the permission (usually in the onRequestPermissionsResult method), try removing it and see if you have the same issue.

Android handling permission disabling from system settings

From app I gave the required permission for my app. While my app is running, I went to system settings page and revoked the permission. The app is crashing. Will we be able to handle this?.
Android 6.0 (Marshmallow, API 23) switched from an install-time permission model to a runtime permission model. Now instead of the user granting all permissions at runtime, you the developer are responsible for requesting permissions at runtime and responding appropriately.
You should begin by reading the Requesting Permissions at Run Time documentation. So that you can properly request permissions on devices running Marshmallow.
To prevent your app from crashing, you need to call ContextCompat.checkSelfPermission() to see if you have a permission before attempting to call a method that requires a permission. However, this is only half the equation since you still need to request the permission if you don't already have it.

Android M Permission list not showing for manual install

Android M not showing normal permission like Internet, WIFI in the permission list. Its just saying No special permission to display. Is that how Android M will display permission, it will never show permission prior to install. Can any one experienced this before.
attached screenshot,please check it.
Please help me to finding this answer.
Thanks.
Quoting the documentation:
When the user installs or updates the app, the system grants the app all permissions listed in the manifest that fall under PROTECTION_NORMAL. For example, alarm clock and internet permissions fall under PROTECTION_NORMAL, so they are automatically granted at install time. For more information about how normal permissions are handled, see Normal Permissions.
The system may also grant the app signature permissions, as described in System components and signature permissions. The user is not prompted to grant any permissions at install time.
(emphasis added)

Android set Service state

I'm trying to turn off the android radio programmatically. I'm trying to use the ServiceState class, but it wont work. I added the permission CHANGE_NETWORK_STATE and MODIFY_PHONE_STATE. Still no luck. any ideas?
Permission MODIFY_PHONE_STATE is a system permission, thus, you cannot use it in your application.
EDIT:
Ok. I'll try to explain. The service method to turn on and turn off radio programatically is protected with MODIFY_PHONE_STATE permission. This permission has signature type, thus, only applications that have the same signature can invoke this method. Thus, so as your application is not signed with the system certificate you cannot use this permission in your application. But without this permission you also cannot change the state of the radio.

Categories

Resources