Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday 29 April 2016

Marshmallow Permissions


Hello friends, 
Welcome to this blog. Today I will show you Android Marshmallow Permissions Sample. One of the major changes in Android Marshmallow is the new permission system. In earlier versions we were declaring the permission in the AndroidManifest.xml file. But with Android Marshmallow we need to ask the permission at run time.  In this post I will show you a simple how to give runtime permissions. So lets begin.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.samset.marshmallowpermissionssample.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samset.marshmallowpermissionssample">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!--Need READ_PHONE_STATE runtime permissions-->
   
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!--Need READ_EXTERNAL_STORAGE runtime permissions-->
   
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

ActivityMain.java

 package com.samset.marshmallowpermissionssample;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_READ_PHONE_STATE_PERMISSION = 225;
    private static final int REQUEST_READ_EXTERNALSTORAGE_PERMISSION = 226;
    private static final int REQUEST_WRITE_EXTERNALSTORAGE_PERMISSION = 227;
   
    private String TAG=MainActivity.class.getSimpleName();
    private MainActivity ctx;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ctx=this;
        checkPermission();
    }

    public void checkPermission() {

        //get all required elements
       
int READ_PHONE_PERMISSION=ContextCompat.checkSelfPermission(ctx, Manifest.permission.READ_PHONE_STATE);
        int READ_EXTERNALSTORAGE_PERMISSION=ContextCompat.checkSelfPermission(ctx, Manifest.permission.READ_EXTERNAL_STORAGE);
        int WRITE_EXTERNALSTORAGE_PERMISSION=ContextCompat.checkSelfPermission(ctx, Manifest.permission.WRITE_EXTERNAL_STORAGE);


        //Check permissions if permission already granted run if blog if not granted then run else blog
       
if (READ_PHONE_PERMISSION == PackageManager.PERMISSION_GRANTED && READ_EXTERNALSTORAGE_PERMISSION == PackageManager.PERMISSION_GRANTED && WRITE_EXTERNALSTORAGE_PERMISSION == PackageManager.PERMISSION_GRANTED) {
            Log.e(TAG, " Permission Already given ");
            telephonePermission();
           
        } else {
            Log.d(TAG, "Current app does not have permission");
            ActivityCompat.requestPermissions(ctx,new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.READ_PHONE_STATE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_READ_PHONE_STATE_PERMISSION);

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Log.e(TAG, "Permission request" + requestCode);

        switch (requestCode) {

            case REQUEST_READ_PHONE_STATE_PERMISSION: {
                // If request is cancelled, the result arrays are empty.
               
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.e(TAG, "Permission Granted");
                    //Proceed to next steps
                   
telephonePermission();
                   
                }
                else {
                    Log.e(TAG, "Permission Denied");
                }
                return;
            }
            case REQUEST_READ_EXTERNALSTORAGE_PERMISSION:
            {
                if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    Log.e(TAG, "Permission Granted");
                    //Proceed to next steps
                   
telephonePermission();
                   

                }
                else {
                    Log.e(TAG, "Permission Denied");
                }
                return;
            }
            case REQUEST_WRITE_EXTERNALSTORAGE_PERMISSION:
            {
                if (grantResults.length > 0 && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
                    Log.e(TAG, "Permission Granted");
                    //Proceed to next steps
                   
telephonePermission();
                 
  }
                else {
                    Log.e(TAG, "Permission Denied");
                }
                return;
            }

            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private void telephonePermission() {
        TelephonyManager TM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String androidId = android.provider.Settings.Secure.getString(this.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);


    }

}

I hope this blog very helps you

Full SourceCode Marshmallowpermissionssample


LiveSample







No comments:

Post a Comment