When I run my app on Android Pie then its work fine, I mean my code for set ringtone and alarm,notification work perfectly but when I run this app on Android 7 (nougat), then alarm and notification is set but ringtone is not. When I click set ringtone then toast message display that ringtone set successfully but when I check it on my phone it still using previous ringtone.
Code for set ringtone:
private void setRingtone() {
AssetFileDescriptor openAssetFileDescriptor;
((AudioManager) getSystemService(AUDIO_SERVICE)).setRingerMode(2);
File file = new File(Environment.getExternalStorageDirectory() + "", this.fNmae);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri parse = Uri.parse(this.fPAth);
ContentResolver contentResolver = getContentResolver();
try {
openAssetFileDescriptor = contentResolver.openAssetFileDescriptor(parse, "r");
} catch (FileNotFoundException e2) {
openAssetFileDescriptor = null;
}
try {
byte[] bArr = new byte[1024];
FileInputStream createInputStream = openAssetFileDescriptor.createInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
for (int read = createInputStream.read(bArr); read != -1; read = createInputStream.read(bArr)) {
fileOutputStream.write(bArr, 0, read);
}
fileOutputStream.close();
} catch (IOException e3) {
e3.printStackTrace();
}
ContentValues contentValues = new ContentValues();
contentValues.put("_data", file.getAbsolutePath());
contentValues.put("title", "nkDroid ringtone");
contentValues.put("mime_type", "audio/mp3");
contentValues.put("_size", Long.valueOf(file.length()));
contentValues.put("artist", Integer.valueOf(R.string.app_name));
contentValues.put("is_ringtone", Boolean.valueOf(true));
contentValues.put("is_notification", Boolean.valueOf(false));
contentValues.put("is_alarm", Boolean.valueOf(false));
contentValues.put("is_music", Boolean.valueOf(false));
try {
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, parse);
Toast.makeText(this, new StringBuilder().append("Ringtone set successfully"), Toast.LENGTH_LONG).show();
} catch (Throwable th) {
Toast.makeText(this, new StringBuilder().append("Ringtone feature is not working"), Toast.LENGTH_LONG).show();
}
}
Code for alarm:
private void setAlarm() {
AssetFileDescriptor openAssetFileDescriptor;
((AudioManager) getSystemService(AUDIO_SERVICE)).setRingerMode(2);
File file = new File(Environment.getExternalStorageDirectory() + "", this.fNmae);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri parse = Uri.parse(this.fPAth);
ContentResolver contentResolver = getContentResolver();
try {
openAssetFileDescriptor = contentResolver.openAssetFileDescriptor(parse, "r");
} catch (FileNotFoundException e2) {
openAssetFileDescriptor = null;
}
try {
byte[] bArr = new byte[1024];
FileInputStream createInputStream = openAssetFileDescriptor.createInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
for (int read = createInputStream.read(bArr); read != -1; read = createInputStream.read(bArr)) {
fileOutputStream.write(bArr, 0, read);
}
fileOutputStream.close();
} catch (IOException e3) {
e3.printStackTrace();
}
ContentValues contentValues = new ContentValues();
contentValues.put("_data", file.getAbsolutePath());
contentValues.put("title", "nkDroid ringtone");
contentValues.put("mime_type", "audio/mp3");
contentValues.put("_size", Long.valueOf(file.length()));
contentValues.put("artist", Integer.valueOf(R.string.app_name));
contentValues.put("is_ringtone", Boolean.valueOf(false));
contentValues.put("is_notification", Boolean.valueOf(false));
contentValues.put("is_alarm", Boolean.valueOf(true));
contentValues.put("is_music", Boolean.valueOf(false));
try {
Toast.makeText(this, new StringBuilder().append("Alarm set successfully"), Toast.LENGTH_LONG).show();
// RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(), RingtoneManager.TYPE_RINGTONE, contentResolver.insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), contentValues));
RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(), RingtoneManager.TYPE_ALARM, parse);
Settings.System.putString(contentResolver, Settings.System.ALARM_ALERT,
parse.toString());
} catch (Throwable th) {
Toast.makeText(this, new StringBuilder().append("Alarm feature is not working"), Toast.LENGTH_LONG).show();
}
}