Video Demo:
In Marshmallow (Api level 23 or higher), Google had changed the way of asking permissions. If we want to access the camera, gallery etc of the marshmallow user, then we have to asked these permission during runtime otherwise over app should crash. There are two type of permission Normal Permission and Dangerous Permission
NORMAL PERMISSION- Normal permission in marshmallow is directly granted while installation of application. Examples of normal permission is given below:
DANGEROUS PERMISSION- Dangerous Permissions which provide access the personal data of user such as gallery, location, camera etc. Examples of dangerous permission is given below:In Marshmallow (Api level 23 or higher), Google had changed the way of asking permissions. If we want to access the camera, gallery etc of the marshmallow user, then we have to asked these permission during runtime otherwise over app should crash. There are two type of permission Normal Permission and Dangerous Permission
NORMAL PERMISSION- Normal permission in marshmallow is directly granted while installation of application. Examples of normal permission is given below:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_SMS"/>
In this tutorial I will cover the runtime permission in Marshmallow in android kotlin. DOWNLOAD SOURCE CODE FROM BELOW.
Add this dependency in your gradle:
compile 'io.vrinda.kotlinpermissions:kotlinpermissions:1.1'
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="deepshikha.com.runtimepermission">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:id="@+id/rl_camera"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="100dp"
android:id="@+id/iv_camera"
android:layout_marginTop="50dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:src="@drawable/camera" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_camera"
android:layout_centerHorizontal="true"
android:text="Take Picture"
android:textColor="#000000"
android:textSize="18dp"
android:textStyle="bold"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#CDCDCD"></View>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_gallery"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="100dp"
android:id="@+id/iv_gallery"
android:layout_marginTop="50dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:src="@drawable/gallery" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_gallery"
android:layout_centerHorizontal="true"
android:text="Gallery Picture"
android:textColor="#000000"
android:textSize="18dp"
android:textStyle="bold"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#CDCDCD"> </View>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#EAEAEA"></View>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#EAEAEA"></View>
</RelativeLayout>
<!---->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:id="@+id/rl_call"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="100dp"
android:id="@+id/iv_call"
android:layout_marginTop="50dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:src="@drawable/call" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_call"
android:layout_centerHorizontal="true"
android:text="Call"
android:textColor="#000000"
android:textSize="18dp"
android:textStyle="bold"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#CDCDCD"></View>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_contacts"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="100dp"
android:id="@+id/iv_contact"
android:layout_marginTop="50dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:src="@drawable/contactlist" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_contact"
android:layout_centerHorizontal="true"
android:text="Contact"
android:textColor="#000000"
android:textSize="18dp"
android:textStyle="bold"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#CDCDCD"></View>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#EAEAEA"></View>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#EAEAEA"></View>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
MainActivity.kt:package deepshikha.com.runtimepermission
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract
import android.provider.MediaStore
import android.util.Log
import io.vrinda.kotlinpermissions.PermissionCallBack
import io.vrinda.kotlinpermissions.PermissionsActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : PermissionsActivity() {
var CAMERAMODE:Int=1
var GALLERYMODE:Int=2
var CALLMODE:Int=3
var CONATACTMODE:Int=4
var boolean_camera:Boolean=false
var boolean_gallery:Boolean=false
var boolean_call:Boolean=false
var boolean_contact:Boolean=false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rl_camera.setOnClickListener {
if (boolean_camera) {
takePhoto()
}else {
fn_permission(Manifest.permission.CAMERA,CAMERAMODE)
}
}
rl_gallery.setOnClickListener {
if (boolean_gallery) {
takegallerypicture()
}else {
fn_permission(Manifest.permission.READ_EXTERNAL_STORAGE,GALLERYMODE)
}
}
rl_call.setOnClickListener {
if (boolean_call) {
phonecall()
}else {
fn_permission(Manifest.permission.CALL_PHONE,CALLMODE)
}
}
rl_contacts.setOnClickListener {
if (boolean_contact){
readcontact()
}else{
fn_permission(Manifest.permission.READ_CONTACTS,CONATACTMODE)
}
}
}
fun fn_permission(permission:String,mode:Int){
requestPermissions(permission, object : PermissionCallBack {
override fun permissionGranted() {
super.permissionGranted()
Log.v("Call permissions", "Granted")
if (mode==CAMERAMODE){
takePhoto()
boolean_camera=true
}else if (mode==GALLERYMODE){
takegallerypicture()
boolean_gallery=true
}else if (mode==CALLMODE){
phonecall()
boolean_call=true
}else if (mode==CONATACTMODE){
readcontact()
boolean_contact=true
}
}
override fun permissionDenied() {
super.permissionDenied()
Log.v("Call permissions", "Denied")
if (mode==CAMERAMODE){
boolean_camera=false
}else if (mode==GALLERYMODE){
boolean_camera=false
}else if (mode==CALLMODE){
boolean_call=false
}else if (mode==CONATACTMODE){
boolean_contact=false
}
}
})
}
fun takePhoto() {
val intent1 = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (intent1.resolveActivity(packageManager) != null) {
startActivityForResult(intent1, CAMERAMODE)
}
}
fun takegallerypicture() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
if (intent.resolveActivity(packageManager) != null) {
startActivityForResult(intent, GALLERYMODE)
}
}
@SuppressLint("MissingPermission")
fun phonecall() {
val intent = Intent(Intent.ACTION_CALL);
intent.data = Uri.parse("tel:1234567890s")
startActivity(intent)
}
fun readcontact(){
val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
startActivity(intent)
}
}