Wednesday 1 March 2017

Andorid marshmallow custom permission code

As we all know android has put some restrictions for permission check for security reasons from android M (6.0) Marshmallow.

Here we gonna share a simple code snippets to customize permission check and put it where you find suitable in your code.

 

Android developer training guide link:  Marshmallow permission


1) Declare permission 

 

public static final String PHONE_PERMISSION = "android.permission.READ_PHONE_STATE";


public static final String CAMERA_PERMISSION = "android.permission.CAMERA";

 

2) xml layout for custom alert dialog (custom_permission_alert.xml)


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainmenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/background_color"
        android:clickable="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center|left"
            android:text="@string/message"
            android:padding="10dp"
            android:textColor="@color/white_color"
            android:textSize="@dimen/header_text_size" />


        <TextView
            android:id="@+id/message_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="center|left"
            android:textColor="@color/white_color"
            android:textSize="@dimen/content_text_size" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/action_no_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/action_yes_tv"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:text="@string/no"
                android:padding="10dp"
                android:visibility="gone"
                android:textColor="@color/white_color"
                android:textSize="@dimen/content_text_size" />

            <TextView
                android:id="@+id/action_yes_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:text="@string/ok"
                android:padding="10dp"
                android:textColor="@color/white_color"
                android:textSize="@dimen/content_text_size" />


        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

 

3) Now create an alert dialog using above xml layout

 

/**
     * This method shows alert dialog to prompt user to enable phone permissions.
     *
     * @param title   the title
     * @param message the message
     */

    public static void showPermissionAlertDialog(final String title, String message,
                                                 final Activity context) {
        try {
            final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            dialog.setContentView(R.layout.app_custom_dialog);
   
            dialog.setCancelable(false);

            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

            // set the custom dialog components - text, image and button

            TextView titleTv = (TextView) dialog.findViewById(R.id.title_tv);
            titleTv.setText(title);

            TextView actionYesTv = (TextView) dialog.findViewById(R.id.action_yes_tv);
            actionYesTv.setText(context.getResources().getString(R.string.allow));

            TextView actionNoTv = (TextView) dialog.findViewById(R.id.action_no_tv);
            actionNoTv.setText(context.getResources().getString(R.string.deny));
            actionNoTv.setVisibility(View.VISIBLE);

            TextView messageTv = (TextView) dialog.findViewById(R.id.message_tv);
            messageTv.setText(message);


            actionYesTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    // Open app settings screen to allow required permissions.
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", context.getPackageName(), null);
                    intent.setData(uri);
                    context.startActivity(intent);
                    dialog.dismiss();
                }
            });

            actionNoTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            if (dialog != null && !dialog.isShowing()) {
                dialog.show();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4) Steps to call the above functions in real scenario 

 

/** 

* Check if device sdk version is greater than or equal to android M

*/

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!Util.checkDevicePermissionRuntime(this, 

Util.CAMERA_PERMISSION)) {
                        Util.showPermissionAlertDialog(getResources().getString(R.string.alert),
                                getResources().getString(R.string.allow_app_to_access_device_feature), this);

       }                   

}

 

Here we done with custom permission check implementation for Marshmallow and above versions.

 

Thanks!

Andorid marshmallow custom permission code

As we all know android has put some restrictions for permission check for security reasons from android M (6.0) Marshmallow. Here we gon...