Is there any way to ask permission programmatically in android ? I don't want to add all permission to AndroidManifest.xml. So is there any dialog that asks for permission at runtime?
问题:
回答1:
No. The user needs to be informed about the permissions while installing the application. Asking the user at runtime would be a security risk.
回答2:
Applications statically declare the permissions they require, and the Android system prompts the user for consent at the time the application is installed. Android has no mechanism for granting permissions dynamically (at run-time) because it complicates the user experience to the detriment of security.
- Android Developer site - System Permissions
回答3:
Not until now, but yes.
According to Google's new permission model introduced in Android M:
If an app running on the M Preview supports the new permissions model, the user does not have to grant any permissions when they install or upgrade the app. Instead, the app requests permissions as it needs them, and the system shows a dialog to the user asking for the permission.
Here's a summary of the key components of this new model:
Declaring Permissions: The app declares all the permissions it needs in the manifest, as in earlier Android platforms.
Permission Groups: Permissions are divided into permission groups, based on their functionality. For example, the
CONTACTS
permission group contains permissions to read and write the user's contacts and profile information.- Limited Permissions Granted at Install Time: 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 underPROTECTION_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. User Grants Permissions at Run-Time: When the app requests a permission, the system shows a dialog to the user, then calls the app's callback function to notify it whether the user granted the permission. This permission model changes the way your app behaves for features that require permissions. Here's a summary of the development practices you should follow to adjust to this model:
Always Check for Permissions: When the app needs to perform any action that requires a permission, it should first check whether it has that permission already. If it does not, it requests to be granted that permission. You do not need to check for permissions that fall under
PROTECTION_NORMAL
.Handle Lack of Permissions Gracefully: If the app is not granted an appropriate permission, it should handle the failure cleanly. For example, if the permission is just needed for an added feature, the app can disable that feature. If the permission is essential for the app to function, the app might disable all its functionality and inform the user that they need to grant that permission.
Permissions are Revocable: Users can revoke an app's permissions at any time. If a user turns off an app's permissions, the app is not notified. Once again, your app should verify that it has needed permissions before performing any restricted actions.
Source: https://developer.android.com/preview/features/runtime-permissions.html
回答4:
No.
Answer here: get Android permission dynamiclly
See the "Uses Permissions" section here: http://developer.android.com/guide/topics/security/security.html
回答5:
Android M introduced Runtime permissions, which everyone has been waiting for. Also the permissions are now categorized into NORMAL and DANGEROUS, where NORMAL permissions are granted by default and DANGEROUS permissions are requested when they are needed. Also DANGEROUS permissions can be revoked by the user at any time from the device's Settings menu.
回答6:
If I combine the answers from 'Piskvor' and from 'Hanno Binder', your app can check if the helper app is available (try to invoke it with an Intent), and if it is not there (the invocation fails), prompt the user to install it.
Look at the following, for example.
how to download adobe reader programatically if not exists