How to check permissions runtime in android
Android 6.0 (Marshmallow API - 23) onwards introduced a new runtime permission model. if you target 6.0 you must need to use new permission model.
we have two permission levels normal and dangerous
Normal: Normal permissions are harmless less for user privacy those are NormalPermissions
Dangerous: Dangerous permissions affect the users private information, or could potentially affect his data or the operation of other application. For example, the ability to read the users contact data. Dangerous permissions must be granted by the user at runtime to the app.
Programmatically Check Runtime-permissions:
Step 1: write required permissions on Manifest.
Step 2: Main Activity (activity_main.xml)
Step 3 : mainActivity.java
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
Step 2: Main Activity (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:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/permission_btn_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="Camera"
android:textColor="@android:color/white" />
<Button
android:id="@+id/permission_btn_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="Gallery"
android:textColor="@android:color/white" />
<Button
android:id="@+id/permission_btn_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="Location"
android:textColor="@android:color/white" />
<Button
android:id="@+id/permission_btn_wifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="wifi"
android:textColor="@android:color/white" />
<Button
android:id="@+id/permission_btn_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="Contacts"
android:textColor="@android:color/white" />
<Button
android:id="@+id/permission_btn_call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="Call"
android:textColor="@android:color/white" />
<Button
android:id="@+id/permission_btn_sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="SMS"
android:textColor="@android:color/white" />
<Button
android:id="@+id/permission_btn_internet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="Internet"
android:textColor="@android:color/white" />
<Button
android:id="@+id/permission_btn_calender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="onClick"
android:text="Calender"
android:textColor="@android:color/white" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Step 3 : mainActivity.java
package in.arula.androidpro_permissions;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final int MY_PERMISSIONS_REQUEST = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View view) {
Toast.makeText(this, "Clecked Me..!", Toast.LENGTH_SHORT).show();
if (view.getId() == R.id.permission_btn_camera) {
requestPermission(new String[]{Manifest.permission.CAMERA});
} else if (view.getId() == R.id.permission_btn_gallery) {
requestPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE});
} else if (view.getId() == R.id.permission_btn_location) {
requestPermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION});
} else if (view.getId() == R.id.permission_btn_wifi) {
requestPermission(new String[]{Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE});
} else if (view.getId() == R.id.permission_btn_contact) {
requestPermission(new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS});
} else if (view.getId() == R.id.permission_btn_call) {
requestPermission(new String[]{Manifest.permission.CALL_PHONE});
} else if (view.getId() == R.id.permission_btn_sms) {
requestPermission(new String[]{Manifest.permission.READ_SMS, Manifest.permission.SEND_SMS, Manifest.permission.BROADCAST_SMS, Manifest.permission.RECEIVE_SMS});
} else if (view.getId() == R.id.permission_btn_internet) {
requestPermission(new String[]{Manifest.permission.INTERNET});
} else if (view.getId() == R.id.permission_btn_calender) {
requestPermission(new String[]{Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR});
}
}
public void requestPermission(String[] permission) {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MainActivity.this,
permission[0])
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
permission[0])) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
// Toast.makeText(PermissionChecker.this, "Permission already granted", Toast.LENGTH_SHORT).show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity.this,
permission,
MY_PERMISSIONS_REQUEST);
// MY_PERMISSIONS_REQUEST is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
/**
* This is the call back that is used to grant permission.
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Granted permission...", Toast.LENGTH_SHORT).show();
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
Toast.makeText(MainActivity.this, "Permission denied...", Toast.LENGTH_SHORT).show();
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
No comments