由于Android MARSHMELLOW的,你需要使用运行权限,其目的是更安全,或者使用许可时,需要在这里documenatation
对于写入设置文档在这里
在清单中添加
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
在你班上
private boolean checkSystemWritePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(Settings.System.canWrite(context))
return true;
else
openAndroidPermissionsMenu();
}
return false;
}
private void openAndroidPermissionsMenu() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
context.startActivity(intent);
}
}
像这样使用
try {
if (checkSystemWritePermission()) {
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
Toast.makeText(context, "Set as ringtoon successfully ", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Allow modify system settings ==> ON ", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.i("ringtoon",e.toString());
Toast.makeText(context, "unable to set as Ringtoon ", Toast.LENGTH_SHORT).show();
}