Friday, April 29, 2016

Check permission in Android 6 (Marshmallow)

activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/sample_main_layout" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical">


    <Button
        android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Get deviceId" 
 android:id="@+id/btn_deviceId" 
 android:layout_gravity="center_horizontal" />

    <TextView 
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content" 
 android:text="deviceId" 
 android:id="@+id/tv_deviceId" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

Android_Manifest
 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="map.conghuy.com.myapplication">

    <!-- BEGIN_INCLUDE(manifest) -->
    <!-- Note that all required permissions are declared here in the Android manifest. 
 On Android M and above, use of these permissions is only requested at run time. --> 
 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <!-- END_INCLUDE(manifest) --> 
 <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" 
 android:label="@string/app_name" 
 android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
 
MainActivity
 
public class MainActivity extends SampleActivityBase
        implements ActivityCompat.OnRequestPermissionsResultCallback {
    public static final String TAG = "MainActivity";
    private final int MY_PERMISSIONS_REQUEST_CODE = 1;
    Button btn_deviceId;
    TextView tv_deviceId;
    TelephonyManager telephonyManager;

    private boolean checkPermissions() {
        if (ActivityCompat.checkSelfPermission(this,
 Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
        return true;
    }

    @Override 
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode != MY_PERMISSIONS_REQUEST_CODE) {
            return;
        }
        boolean isGranted = true;
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                isGranted = false;
                break;
            }
        }

        if (isGranted) {
            startApplication();
        } else {
            Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();

        }
    }

    private void setPermissions() {
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.READ_PHONE_STATE        }, MY_PERMISSIONS_REQUEST_CODE);
    }

    public void startApplication() {
        btn_deviceId = (Button) findViewById(R.id.btn_deviceId);
        tv_deviceId = (TextView) findViewById(R.id.tv_deviceId);
        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId();
        tv_deviceId.setText(deviceId);
    }

    @Override 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (checkPermissions()) {
            startApplication();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("msg");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override 
 public void onClick(DialogInterface dialog, int which) {
                    setPermissions();
                }
            });
            builder.show();
        }

    }
} 

No comments:

Post a Comment