问题描述
当我在Android Pie上运行我的应用程序时,它工作正常,是指我设置铃声和警报,通知的代码运行正常,但是当我在Android 7(牛轧糖)上运行此应用程序时,则设置了警报和通知,但铃声不是。当我单击“设置铃声”时,举杯消息成功显示该铃声设置,但是当我在手机上检查该铃声时,它仍使用以前的铃声。
设置的铃声代码
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,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();
}
}
警报代码:
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,read);
}
fileOutputStream.close();
} catch (IOException e3) {
e3.printstacktrace();
}
ContentValues contentValues = new ContentValues();
contentValues.put("_data",Boolean.valueOf(false));
contentValues.put("is_notification",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(),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();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)