Skip to content

Instantly share code, notes, and snippets.

@gotoark
Created November 15, 2017 03:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotoark/392af3a3bdd673a06b8a8cb623ae4be0 to your computer and use it in GitHub Desktop.
Save gotoark/392af3a3bdd673a06b8a8cb623ae4be0 to your computer and use it in GitHub Desktop.
android 6.0.1 to ask the WRITE_SETTINGS permission Ref: https://forum.xda-developers.com/s7-edge/help/setactualdefaultringtoneuri-t3387159
private boolean checkMyPermission(boolean isSecondTimeCheck) {
String dialogTitle = "need WRITE_SETTINGS permission";
if (isSecondTimeCheck) dialogTitle = "you deny the permission";
String btnTitle = "ok", if (isSecondTimeCheck) btnTitle = "try again";
if (Build.VERSION.SDK_INT >= 23) {
if (!Settings.System.canWrite(this)) {
new AlertDialog.Builder(mContext).setMessage(dialogTit le).setPositiveButton(btnTitle, new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE_PERMISSION);
}
}).setNegativeButton("cancel", null).create().show();
return false;
}
return true;
} else {
int hasWriteContactsPermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_SETTINGS);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
new AlertDialog.Builder(mContext).setMessage(dialogTit le).setPositiveButton(btnTitle, new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, REQUEST_CODE_PERMISSION);
}
}).setNegativeButton("cancel", null).create().show();
return false;
} else {
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment