我从url下载了一些铃声,我正在尝试设置为铃声。
下载方法
public void downloadRingtones(Uri uri, String title) {
request = new DownloadManager.Request(uri);
//String title = URLUtil.guessFileName(String.valueOf(uri), null, null);
request.setTitle(title);
request.setDescription("Downloading File please wait...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_RINGTONES, title);
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
id = downloadManager.enqueue(request);
//String s = downloadManager.getMimeTypeForDownloadedFile(id);
Toast.makeText(this, "Downloading...", Toast.LENGTH_SHORT).show();
}
试验方法和类型测定方法
public int setType(String type, ContentValues values) {
if (type.equals("ringtone")) {
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
return RingtoneManager.TYPE_RINGTONE;
} else if (type.equals("alarm")) {
values.put(MediaStore.Audio.Media.IS_ALARM, true);
return RingtoneManager.TYPE_ALARM;
} else if (type.equals("notif")) {
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
return RingtoneManager.TYPE_NOTIFICATION;
} else if (type.equals("person")) {
setPersonalRingtone(downloaded_uri);
}
return -1;
}
设置方法(如果由于broadcastreceiver下载完成,则启动方法)
public void setRingtone(Uri mUri, String title, String type) {
downloadRingtones(mUri, title);
broadcastReceiver.setDownloadCompletedListener(uri -> {
ContentValues values = new ContentValues();
File file = new File(uri.getPath());
downloaded_uri = uri;
String uri_id = downloaded_uri.getLastPathSegment();
int set_type = setType(type, values);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (!Settings.System.canWrite(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
startActivity(intent);
} else {
values.put(MediaStore.Audio.Media._ID, uri_id);
values.put(MediaStore.Audio.Media.DISPLAY_NAME, title);
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg");
Uri tempUri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
Uri newUri = getBaseContext().getContentResolver()
.insert(tempUri, values);
RingtoneManager.setActualDefaultRingtoneUri(this, set_type, newUri);
Toast.makeText(this, "Ringtone Set", Toast.LENGTH_SHORT).show();
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!Settings.System.canWrite(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
startActivity(intent);
} else {
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, title);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
Uri tempUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
IslamicRingtonesMainActivity.this.getContentResolver()
.delete(tempUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);
Uri newUri = IslamicRingtonesMainActivity.this.getContentResolver()
.insert(tempUri, values);
RingtoneManager.setActualDefaultRingtoneUri(this, set_type, newUri);
Toast.makeText(this, "Ringtone Set", Toast.LENGTH_SHORT).show();
}
} else {
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, title);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
Uri tempUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
IslamicRingtonesMainActivity.this.getContentResolver()
.delete(tempUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);
Uri newUri = IslamicRingtonesMainActivity.this.getContentResolver()
.insert(tempUri, values);
RingtoneManager.setActualDefaultRingtoneUri(this, set_type, newUri);
Toast.makeText(this, "Ringtone Set", Toast.LENGTH_SHORT).show();
}
}, request, downloadManager, id);
}
我的广播接收机
public class DownloadBroadcastReceiver extends BroadcastReceiver {
private downloadCompletedListener completedListener;
DownloadManager downloadManager;
DownloadManager.Request request;
long id;
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
if(id > 0){
completedListener.onDownloaded(downloadManager.getUriForDownloadedFile(id));
}
}
}
public interface downloadCompletedListener {
void onDownloaded(Uri uri);
}
public void setDownloadCompletedListener(downloadCompletedListener listener, DownloadManager.Request request, DownloadManager downloadManager, long id) {
this.completedListener = listener;
this.request = request;
this.downloadManager = downloadManager;
this.id = id;
}
当我进入铃声设置(手机设置)时,我会看到我设置为铃声的歌曲名称,但当有人打电话给我时,它不会播放任何内容。提前感谢你的帮助。
暂无答案!
目前还没有任何答案,快来回答吧!