Created
April 10, 2018 03:12
-
-
Save alitamoor65/a00d483b6e21a5bd38ae0563cd6732dc to your computer and use it in GitHub Desktop.
Request and Check RunTime Permissions
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.tutorialscafe.readallsms"> | |
<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" | |
android:windowSoftInputMode="stateHidden"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or 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
@Override | |
public void onRequestPermissionsResult(int requestCode, String permissions[],int[] grantResults) { | |
switch (requestCode) { | |
case 999: { | |
// If request is cancelled, the result arrays are empty. | |
if (grantResults.length > 0 | |
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
Toast.makeText(this, "Permission Granted.", Toast.LENGTH_SHORT).show(); | |
// permission was granted, yay! Do the | |
// contacts-related task you need to do. | |
} else { | |
Toast.makeText(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 | |
} | |
} | |
public void requestPermission(){ | |
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) | |
{ | |
if(!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.READ_CONTACTS)){ | |
}else{ | |
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},999); | |
} | |
}else{ | |
Toast.makeText(this, "Already has permissions.", Toast.LENGTH_SHORT).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment