Android Working with Marshmallow Runtime Permission (READ PHONE STATE)

0
2590

Android is a benefit isolated working framework, i.e. every application in android is isolated from another through an unmistakable id, and every application document/information is private to that application as it were. Every Android application is begun in its own procedure in this way are disconnected from every other application (even from framework/default applications). Thus an Android application can’t get to any document or information outside its degree until and unless the record or information is imparted to the application.

In this way, the conclusion is that if an application needs anything outside its degree, at that point it should ask for consent to the client. Android accompanies an arrangement of predefined consents (System authorizations) for specific assignments. Each application can ask for required authorizations. For instance, an application may proclaim that it requires arrange get to. It can likewise characterize new authorizations.

 

Creating Android Project

1. Create a new project in Android Studio File ⇒ New Project and fill the project details.

2. Add the phone state permission to your AndroidManifest.xml just before the application tag.

<uses-permission android:name=”android.permission.READ_PHONE_STATE” />
3.Create a New Class and Name It : AbsRuntimePermission and copy the from code below and paste it.

/**
 * Created by Aonik on 23/10/2017.
 */
public abstract class AbsRuntimePermission extends Activity {
    private SparseIntArray mErrorString;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mErrorString = new SparseIntArray();
 
    }
 
    public abstract void onPermissionsGranted(int requestCode);
 
    public void requestAppPermissions(final String[]requestedPermissions, final int stringId, final int requestCode) {
        mErrorString.put(requestCode, stringId);
 
        int permissionCheck = PackageManager.PERMISSION_GRANTED;
        boolean showRequestPermissions = false;
        for(String permission: requestedPermissions) {
            permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
            showRequestPermissions = showRequestPermissions || ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
        }
 
        if (permissionCheck!=PackageManager.PERMISSION_GRANTED) {
            if(showRequestPermissions) {
                Snackbar.make(findViewById(android.R.id.content), stringId, Snackbar.LENGTH_INDEFINITE).setAction("GRANT", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityCompat.requestPermissions(AbsRuntimePermission.this, requestedPermissions, requestCode);
                    }
                }).show();
            } else {
                ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
            }
        } else {
            onPermissionsGranted(requestCode);
        }
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        int permissionCheck = PackageManager.PERMISSION_GRANTED;
        for(int permisson : grantResults) {
            permissionCheck = permissionCheck + permisson;
        }
 
        if( (grantResults.length &gt; 0) &amp;&amp; PackageManager.PERMISSION_GRANTED == permissionCheck) {
            onPermissionsGranted(requestCode);
        } else {
            //Display message when contain some Dangerous permisson not accept
            Snackbar.make(findViewById(android.R.id.content), mErrorString.get(requestCode),
                    Snackbar.LENGTH_INDEFINITE).setAction("ENABLE", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent();
                    i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    i.setData(Uri.parse("package:" + getPackageName()));
                    i.addCategory(Intent.CATEGORY_DEFAULT);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                    startActivity(i);
                }
            }).show();
        }
    }
}
  1. extends MainActivity With AbsRuntimePermission And Import a method onPermissionsGranted . Copy the code from bellow and paste it .
/**
 * Created by Aonik on 23/10/2017.
 */
public class MainActivity extends AbsRuntimePermission {
    private static final int REQUEST_PERMISSION = 12;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestAppPermissions(new String[]{
                        android.Manifest.permission.READ_PHONE_STATE},
 
                R.string.msg,REQUEST_PERMISSION);
 
    }
 
    @Override
    public void onPermissionsGranted(int requestCode) {
        //Do anything when permisson granted
        Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_LONG).show();
    }
}
  1. Add The Code in string.xml

<resources>
<string name=”app_name”>RuntimePermission</string>
<string name=”msg”>Please enable the permission to continue </string>
</resources>

LEAVE A REPLY

Please enter your comment!
Please enter your name here