利用RingToneManager设置手机铃声(多选)

//需要传入一个文件路径(path),和context	
 public static void setMyRingtone(Context context, String path) { 

File sdfile = new File(path);
String ab_path = sdfile.getAbsolutePath();
ContentValues values = new ContentValues();
Uri uri_query = MediaStore.Audio.Media.getContentUriForPath(ab_path);
// 查询音乐文件在媒体库是否存在
Cursor cursor = context.getContentResolver().query(uri_query, null,
MediaStore.MediaColumns.DATA + "=?", new String[] { ab_path },
null);


if (cursor.moveToFirst() && cursor.getCount() > 0) {
String _id = cursor.getString(0);
Uri newUri = ContentUris.withAppendedId(uri_query,
Long.valueOf(_id));
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE, newUri);
Toast.makeText(context, "设置电话铃声成功1!", Toast.LENGTH_SHORT).show();
} else {


values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, sdfile.length());

values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);

//注意这一行,一定是只设置IS_NOTIFICATION这一个值。同理,如果设置通知音或闹铃也都只设置这一个值,只是在RingtoneManager.setActualDefaultRingtoneUri()这里区别是哪种铃声



Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile
.getAbsolutePath());
Uri newUri = context.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE, newUri);//这里设置不同的TYPE来区别铃声种类
Toast.makeText(context, "设置电话铃声成功2!", Toast.LENGTH_SHORT).show();
System.out.println("setMyRingtone()-----铃声");
}


}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值