失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【日历】铃声设置为SDcard铃声后卸载SDcard 提示音铃声显示为数字事件响应时无铃声输出

【日历】铃声设置为SDcard铃声后卸载SDcard 提示音铃声显示为数字事件响应时无铃声输出

时间:2023-07-20 08:37:18

相关推荐

【日历】铃声设置为SDcard铃声后卸载SDcard 提示音铃声显示为数字事件响应时无铃声输出

最近做项目时,测试提出的一个问题,在解的过程中遇到过问题,现在已解决,特此记录一下。

思路:设置为SD卡上的铃声之后,保存这个铃声的路径,监听SD卡的插拔事件,当SD卡mount上并扫描完文件之后,再去数据库中查保存起来的路径,根据路径找到当前的uri,也就是新的content://media/external/audio/media/+数字

第一步:既然设置铃声为SDcard铃声,那么首先就需要写一个广播来监听T卡的插拔,也就是SDcard的卸载与安装了,代码如下:

/*** * function: update the calendar ringtone when external sdcard mounted or boot complete*/package com.android.calendar;import java.io.File;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.database.Cursor;import .Uri;import android.os.Handler;import android.os.HandlerThread;import android.os.Looper;import android.os.Message;import android.preference.PreferenceManager;import android.provider.MediaStore;import android.text.TextUtils;import android.util.Log;public class CalendarRingtoneUpdateReceiver extends BroadcastReceiver{private static final String TAG = "CalendarRingtoneExternalReceiver";private Context mContext;private static final int CAL_CHECK = 0;private static final int CAL_QUIT = 1;@Overridepublic void onReceive(Context arg0, Intent arg1) {// TODO Auto-generated method stubString action = arg1.getAction();Log.d(TAG, "[onReceive] action: "+action);if(TextUtils.isEmpty(action)) {Log.w(TAG, "[onReceive] action is empty! just return.");return;}if(action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {mContext = arg0;HandlerThread thread = new HandlerThread("CalendarRingtoneExternalReceiver");thread.start();new CheckExternalHandler(thread.getLooper()).sendEmptyMessage(CAL_CHECK);}}private final class CheckExternalHandler extends Handler {public CheckExternalHandler(Looper looper) {// TODO Auto-generated constructor stubsuper(looper);}@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);switch (msg.what) {case CAL_CHECK:SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);String ringtoneUriForExternal = null;Cursor cursor = null;String soundUri = Utils.getRingTonePreference(mContext);Log.d(TAG, "[onReceive] soundUri : "+soundUri);if(TextUtils.isEmpty(soundUri)|| !soundUri.startsWith(GeneralPreferences.NOTIFICATION_RINTONE_PATH_PREFIX)) {Log.w(TAG, "soundUri is empty, just continue here.");this.sendEmptyMessage(CAL_QUIT);break;}ringtoneUriForExternal = preferences.getString(GeneralPreferences.EXTERNAL_RINGTONE_PATH, null);Log.d(TAG

如果觉得《【日历】铃声设置为SDcard铃声后卸载SDcard 提示音铃声显示为数字事件响应时无铃声输出》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。