Created
October 27, 2017 23:04
-
-
Save raveesh-me/20e923bd0935ccaf0a3c9e16206cbce8 to your computer and use it in GitHub Desktop.
A kotlin/anko template for a RequestPermission workflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package `in`.cookytech.testapp | |
import android.Manifest | |
import android.Manifest.permission | |
import android.content.pm.PackageManager | |
import android.os.Build | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import org.jetbrains.anko.* | |
import org.jetbrains.anko.sdk25.coroutines.onClick | |
class MainActivity : AppCompatActivity() { | |
private val PERMISSIONS_REQUEST_CODE: Int = 11 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
relativeLayout { | |
button("call"){ | |
onClick { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ | |
if (checkSelfPermission(permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){ | |
if(shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)){ | |
ctx.alert { | |
title = "We need Permissions" | |
message = "Did you know that we android makes us ask for permissions" + | |
"for us to call your doctor?" | |
yesButton { | |
requestPermissions( | |
arrayOf(Manifest.permission.CALL_PHONE), | |
PERMISSIONS_REQUEST_CODE | |
) | |
} | |
noButton { | |
longToast(":(") | |
} | |
}.show() | |
} | |
else{ | |
requestPermissions( | |
arrayOf(Manifest.permission.CALL_PHONE), | |
PERMISSIONS_REQUEST_CODE | |
) | |
} | |
} | |
else{ | |
makeTheCall() | |
} | |
} | |
else { | |
makeTheCall() | |
} | |
} | |
}.lparams { | |
centerInParent() | |
} | |
} | |
} | |
override fun onRequestPermissionsResult( | |
requestCode: Int, | |
permissions: Array<out String>, | |
grantResults: IntArray | |
) { | |
if (requestCode == PERMISSIONS_REQUEST_CODE){ | |
if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){ | |
makeTheCall() | |
} | |
} | |
} | |
private fun makeTheCall() { | |
makeCall("9883988884") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment