Pada onCreate
cek versi android
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 23) {
// Do something for 14 and above versions cek();
} else {
// do something for phones running an SDK before 14
}
Tambahkan function berikut
private static final int PERMISSION_REQUEST_CODE = 1;
public void cek(){
if (checkPermission()) {
// Snackbar.make(getCurrentFocus(),"Permission already granted.",Snackbar.LENGTH_LONG).show();
} else {
// Snackbar.make(getCurrentFocus(),"Please request permission.",Snackbar.LENGTH_LONG).show(); }
if (!checkPermission()) {
requestPermission();
} else {
Snackbar.make(getCurrentFocus(),"Permission already granted.",Snackbar.LENGTH_LONG).show();
}
}
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
private void requestPermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(getApplicationContext(),"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE);
}
}
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Snackbar.make(getCurrentFocus(),"Permission Granted, Now you can access location data.",Snackbar.LENGTH_LONG).show();
} else {
// Snackbar.make(getCurrentFocus(),"Permission Denied, You cannot access location data.", Snackbar.LENGTH_LONG).show();
}
break;
}
}
Tidak ada komentar:
Posting Komentar