Wednesday, August 9, 2017

android - Check permission programmatically

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    android:background="#82a474"
    >
    <Button
        android:id="@+id/btn_do_task"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check Permission"
        />
</LinearLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private LinearLayout mRootLayout;
    private Button mBtnDoTask;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from xml layout
        mRootLayout = findViewById(R.id.root_layout);
        mBtnDoTask = findViewById(R.id.btn_do_task);

        // Set a click listener for the button
        mBtnDoTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                    /*
                        int checkSelfPermission (Context context, String permission)
                            Determine whether you have been granted a particular permission.

                        Parameters
                            context : Context
                            permission String : The name of the permission being checked.

                        Returns
                            int : PERMISSION_GRANTED if you have the permission, or PERMISSION_DENIED if not.
                    */
                    /*
                        PackageManager
                            Class for retrieving various kinds of information related to the
                            application packages that are currently installed on the device.
                            You can find this class through getPackageManager().
                    */
                    /*
                        int PERMISSION_DENIED
                            Permission check result : this is returned by checkPermission(String, String)
                                                      if the permission has not been granted to the given package.

                        Constant Value : -1 (0xffffffff)
                    */
                    /*
                        int PERMISSION_GRANTED
                            Permission check result: this is returned by checkPermission(String, String)
                                                     if the permission has been granted to the given package.

                        Constant Value: 0 (0x00000000)
                    */
                    if(ContextCompat.checkSelfPermission(mActivity,Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
                        // Camera permission granted

                        Toast.makeText(mContext,"Permission granted.",Toast.LENGTH_SHORT).show();
                    }else {
                        // Camera permission not granted
                        Toast.makeText(mContext,"Permission not granted.",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
AndroidManifest.xml [Permission]

<uses-permission android:name="android.permission.CAMERA"/>