- zeero0 / MainActivity.java
- Android Multiple Runtime Permissions Tutorial Example Programmatically
- What is Runtime Permission In Android?
- Three Protection levels
- Normal Permissions
- Signature Permissions
- Dangerous Permissions
- Special Permissions
- Understanding Runtime Permissions Architecture
- Step 1: Creating WelcomeActivity.java class:
- Step 2: Creating MainActivity.java class:
- Step 3: Description of MainActivity
- Defining permission codes
- Preparing ArrayList
- Hashmap Creation
- Update Package name
- Define required permissions in AndroidManifest.xml
- Other Scenario to handle
- To handle this situation you just need to change a number of permissions in following snippets.
- Kotlin Version
- Other charming tutorials you will definitely like
- Download Source Code For Android Multiple Runtime Permissions
- 10 thoughts on “Android Multiple Runtime Permissions Tutorial Example Programmatically”
zeero0 / MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.zeeroapps.easy_permissions ; |
import android.Manifest ; |
import android.os.Build ; |
import android.support.annotation.NonNull ; |
import android.support.v7.app.AppCompatActivity ; |
import android.os.Bundle ; |
import android.util.Log ; |
import android.view.View ; |
import android.widget.Toast ; |
public class MainActivity extends AppCompatActivity implements PermissionUtil . PermissionsCallBack < |
private static final String TAG = » MainActivity » ; |
@Override |
protected void onCreate ( Bundle savedInstanceState ) < |
super . onCreate(savedInstanceState); |
setContentView( R . layout . activity_main); |
> |
public void requestPermissions ( View view ) < |
if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . M ) < |
if ( PermissionUtil . checkAndRequestPermissions( this , |
Manifest . permission . ACCESS_FINE_LOCATION , |
Manifest . permission . SEND_SMS )) < |
Log . i( TAG , » Permissions are granted. Good to go! » ); |
> |
> |
> |
@Override |
public void onRequestPermissionsResult ( int requestCode , @NonNull String [] permissions , @NonNull int [] grantResults ) < |
super . onRequestPermissionsResult(requestCode, permissions, grantResults); |
PermissionUtil . onRequestPermissionsResult( this , requestCode, permissions, grantResults, this ); |
> |
@Override |
public void permissionsGranted () < |
Toast . makeText( this , » Permissions granted! » , Toast . LENGTH_SHORT ) . show(); |
> |
@Override |
public void permissionsDenied () < |
Toast . makeText( this , » Permissions Denied! » , Toast . LENGTH_SHORT ) . show(); |
> |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.zeeroapps.easy_permissions ; |
import android.annotation.TargetApi ; |
import android.app.Activity ; |
import android.app.AlertDialog ; |
import android.content.Context ; |
import android.content.DialogInterface ; |
import android.content.pm.PackageManager ; |
import android.os.Build ; |
import android.support.annotation.NonNull ; |
import android.support.annotation.RequiresApi ; |
import android.support.v4.app.ActivityCompat ; |
import java.util.ArrayList ; |
import java.util.List ; |
public class PermissionUtil < |
public static final int REQUEST_CODE_PERMISSIONS = 100 ; |
/** |
* Check if multiple permissions are granted, if not request them. |
* |
* @param activity calling activity which needs permissions. |
* @param permissions one or more permissions, such as < @link android.Manifest.permission#CAMERA>. |
* @return true if all permissions are granted, false if at least one is not granted yet. |
*/ |
@RequiresApi ( api = Build . VERSION_CODES . M ) |
public static boolean checkAndRequestPermissions ( Activity activity , String . permissions ) < |
List String > permissionsList = new ArrayList<> (); |
for ( String permission : permissions) < |
int permissionState = activity . checkSelfPermission(permission); |
if (permissionState == PackageManager . PERMISSION_DENIED ) < |
permissionsList . add(permission); |
> |
> |
if ( ! permissionsList . isEmpty()) < |
ActivityCompat . requestPermissions(activity, |
permissionsList . toArray( new String [permissionsList . size()]), |
REQUEST_CODE_PERMISSIONS ); |
return false ; |
> |
return true ; |
> |
/** |
* Handle the result of permission request, should be called from the calling < @link Activity>‘s |
* |
* |
* @param activity calling activity which needs permissions. |
* @param requestCode code used for requesting permission. |
* @param permissions permissions which were requested. |
* @param grantResults results of request. |
* @param callBack Callback interface to receive the result of permission request. |
*/ |
public static void onRequestPermissionsResult ( final Activity activity , int requestCode , @NonNull String [] permissions , @NonNull int [] grantResults , final PermissionsCallBack callBack ) < |
if (requestCode == PermissionUtil . REQUEST_CODE_PERMISSIONS && grantResults . length > 0 ) < |
final List String > permissionsList = new ArrayList<> (); |
for ( int i = 0 ; i permissions . length; i ++ ) < |
if (grantResults[i] == PackageManager . PERMISSION_DENIED ) < |
permissionsList . add(permissions[i]); |
> |
> |
if (permissionsList . isEmpty() && callBack != null ) < |
callBack . permissionsGranted(); |
> else < |
boolean showRationale = false ; |
for ( String permission : permissionsList) < |
if ( ActivityCompat . shouldShowRequestPermissionRationale(activity, permission)) < |
showRationale = true ; |
break ; |
> |
> |
if (showRationale) < |
showAlertDialog(activity, new DialogInterface . OnClickListener () < |
@TargetApi ( Build . VERSION_CODES . M ) |
@Override |
public void onClick ( DialogInterface dialogInterface , int i ) < |
checkAndRequestPermissions(activity, permissionsList . toArray( new String [permissionsList . size()])); |
> |
>, new DialogInterface . OnClickListener () < |
@Override |
public void onClick ( DialogInterface dialogInterface , int i ) < |
if (callBack != null ) < |
callBack . permissionsDenied(); |
> |
> |
>); |
> |
> |
> |
> |
/** |
* Show alert if any permission is denied and ask again for it. |
* |
* @param context |
* @param okListener |
* @param cancelListener |
*/ |
private static void showAlertDialog ( Context context , |
DialogInterface . OnClickListener okListener , |
DialogInterface . OnClickListener cancelListener ) < |
new AlertDialog . Builder (context) |
.setMessage( » Some permissions are not granted. Application may not work as expected. Do you want to grant them? » ) |
.setPositiveButton( » OK » , okListener) |
.setNegativeButton( » Cancel » , cancelListener) |
.create() |
.show(); |
> |
interface PermissionsCallBack < |
void permissionsGranted (); |
void permissionsDenied (); |
> |
> |
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Источник
Android Multiple Runtime Permissions Tutorial Example Programmatically
Hi, Guys. Welcome to check android multiple runtime permissions example.
In this tutorial, We will learn how to programmatically request multiple marshmallow runtime permissions in android studio.
You will learn how to request multiple runtime permissions at once or single request.
From android’s Marshmallow version, the developer needs to programmatically check and request multiple runtime permissions to the user.
What is Runtime Permission In Android?
Runtime Permissions means that permissions required by the android app for it’s several operations.
For example, if your app wants to capture images, you need to use camera of the android device. You need to have permission for using the camera of the device.
The primary purpose of the runtime permission is to protect the user’s data and personal information such as contact list and SMS.
Runtime permissions provides extra security to the user.
Earlier, developer do not need to ask for these permissions.
App was allowed to use device resources without user’s permission which was not an efficient thing in terms of user’s private security.
You need user agreement while working with dangerous Permissions only. Normal, Signature and Special are the other types of the permissions. Other permissions can be used without runtime environment.
Using this example, you can ask for multiple or single runtime permissions Android as per your requirements.
Three Protection levels
Permissions are divided in the three levels of security.
- Normal Permissions
- Signature Permissions
- Dangerous Permissions
Normal Permissions
When the app requires the data or resource which falls outside the app’s sandbox, there is very low risk to the user’s security or the operations of the other app.
These permissions are known as Normal Permissions.
Normal permissions are not much harmful to the privacy of the user. Hence, no need to ask them at runtime to the user.
For example, app needs to use the internet. Here, accessing internet via app is not a big task and it will not fetch user’s personal data. So INTERNET permission is a normal permission.
Signature Permissions
The system grants these app permissions at install time, but only when the app that attempts to use a permission is signed by the same certificate as the app that defines the permission.
For example, CLEAR_APP_CACHE permission.
Dangerous Permissions
These are the permissions which are sensitive to the use’s personal data and security. These permissions can also alter the device’s data storage or the operation of any other apps.
For example, you want to save the picture on device’s internal storage (WRITE_EXTERNAL_STORAGE).
User has to personally grant these permissions.
If user does not grant these permissions, your app can not provide the functionalities which require dangerous permissions.
Special Permissions
There are two permissions which are not included in the normal or dangerous permissions.
SYSTEM_ALERT_WINDOW and WRITE_SETTINGS are special permissions.
You can find some more details and list of all the permissions here on an official document.
Understanding Runtime Permissions Architecture
Let us understand how whole architecture of Runtime Permission works.
- As you have seen in the video, first of all, a dialog is created for asking each and every permission.
- If the user denies specific permission, compiler will again ask for that particular permission when the user opens an Android app. This process continuous until user grants that permission.
- Once user allows, it will never ask for the same permission in the future.
- There is also one checkbox saying “Never ask again,” in the permission dialog. If the user checks this checkbox and then denies, then Android will never ask for that permission.
Now how to ask for that permission?
- For handling above situation, We need to open another dialog which allows the user to go to setting of our app and from there, he can turn on and off various permissions.
Following is the output:
Following are the required code snippets.
Step 1: Creating WelcomeActivity.java class:
Create new Activity named WelcomeActivity.java and add below code
User will only enter in this activity when he has allowed all the runtime permissions. Otherwise compiler will close the app.
Update activity_welcome.xml as below
Step 2: Creating MainActivity.java class:
Step 3: Description of MainActivity
Defining permission codes
In checkAndRequestPermissions() method, create permission code for each permission like below.
These integer variables will represent the specific type of permissions in the whole example.
Preparing ArrayList
Add each permission in ArrayList and ask user by below source code
listPermissionsNeeded will contain all the permissions which we are going to ask from the user.
Hashmap Creation
In onRequestPermissionResult() method, put all the permission in one Hashmap.
Now check if user have granted all the required permissions or not.
If user have granted all the required permissions then open WelcomeActivity or continue to the normal flow of your app.
If not granted, then ask again with your message.
Above source code will check if user gave granted all the permissions or not.
If any of the permissions is missing then again this code will request for runtime permissions.
Update Package name
When the user have denied any permission and he has also checked the “”Never ask again,” checkbox then the compiler will call explain() method.
In explain() method you need to update package name in below line
Define required permissions in AndroidManifest.xml
Other Scenario to handle
In the above code, we ask check multiple marshmallow runtime permissions in splash screen.
But sometimes, you want to ask for specific permission in specific activity.
You may want to ask for a different number of permissions in different activities.
To handle this situation you just need to change a number of permissions in following snippets.
- When creating permissions codes
- Create ArrayList of permissions with below one
- While creating HashMap
- In following two if conditions
Following are two more examples on runtime permissions.
Kotlin Version
Other charming tutorials you will definitely like
- Google Login Android Implementation
- Facebook Login Android Implementation
- Circular Progress Bar
- Listview with checkbox
That’s all for check request multiple marshmallow runtime permissions android studio tutorial.
If you have any question about implementing check request multiple marshmallow runtime permissions android, feel free to ask in the comment section.
Cheers and happy coding!
Download Source Code For Android Multiple Runtime Permissions
10 thoughts on “Android Multiple Runtime Permissions Tutorial Example Programmatically”
Nice one example
I need to draw/mark a path on google maps… I was thinking of sending my location every 30-60seconds and drawing my path like that, my doubt is, how can i mark the path…
Using volley to send data to mysql and then retrieving my points to a map …
But how can i mark the path im going (like a hicking app) and draw it on google maps? any tips ?
Thank you
We will post google map related tutorial for that in near future.
Great work, Thankful to you for this work that helped me as beginner….
Источник