Tuesday, May 14, 2019

Runtime permissions in android

Run time permissions were launched in Android marshmallow  and greater versions of android. The run time permissions are very necessary and must be provided by user to access the critical information of mobile like location, contacts, gallery,etc

The description and code to build run time permission is given below.

1.To check whether  the version of mobile is  greater than marshmallow or not
   
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)

2.To check whether  the permissions are given in manifest file

   if (checkSelfPermission(Manifest.permission.READ_CONTACTS) !=

  PackageManager.PERMISSION_GRANTED)

3.To show the dialog box  to request permission from user

   if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
         requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},1);
        }

4. Now we need to call the override method outside on create and write its content

   @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show();


        }

    }
}
5. Now provide uses permission in manifest for the permission e.g
    
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>


 Here every thing is done for run time permission

Below is the example for permission for reading contacts



Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.apkglobal.blog_runtime_permission">
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //to check mobile permission          
  if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                // to display               
 if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {

                    requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1);
                } else {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
                }
            }
        } else {
        }

    }
 @Override 
   public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(this, "permission granted", Toast.LENGTH_SHORT).show();


            }

        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

No comments:

Alert Dialog Box

Alert Dialog Box Android AlertDialog   can be used to display the dialog message with OK and Cancel buttons. It can be used to inter...