问题描述
我的应用程序,您可以指定不同的铃声为不同的输入事件。 EG:来电,来电短信等
My app allows you to specify a different ringtone for different incoming events. EG: incoming call, incoming SMS, etc.
当我收到例如来电,我检查我的应用程序的数据库,如果选择了特定的选项,如果有一个铃声选项设置播放铃声什么,我试图完成的。
What I am trying to accomplish is when I receive for example an incoming call, I check my apps database if a specific option is selected and if there is a ringtone option set play that ringtone.
不过我遇到的问题是我无法覆盖从播放/停止默认手机铃声。
However the problem I am having is I am unable to override / stop the default phone ringtone from playing.
我已经尝试了几种不同的方法,但是从文档大多数这些方法只停止当前的实例,不是全局的方法。
I have tried several different ways, but from the docs most of those methods only stop the current instance and are not global methods.
我无法设置铃声在默认的手机铃声设置,因为它需要根据来电是动态的。
I can't set the ringtone in the default phones ringtone settings as it needs to be dynamic based on the incoming call.
如果有人知道的伎俩还是有办法做到这一点,将是巨大的。我希望是有道理的。
If anyone knows of a trick or a way to accomplish this that would be great.I hope that makes sense.
推荐答案
有一个几个步骤来动态地改变铃声。
There are a several steps to dynamically change the ringtone.
File k = new File("/sdcard/ringtone", "kolyan_.mp3");
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "My Song title");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
values.put(MediaStore.Audio.Media.ARTIST, "Some Artist");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
2。把它插入到数据库
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
// Line below is major because we need to delete old entry
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
mUri = getContentResolver().insert(uri, values);
3。保存当前的默认铃声,并订阅CallListener
// Be careful by calling getActualDefaultRingtoneUri in CallListener, it could return null, better way to save it in OnCreate
mOldUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);
TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new MyCallListener(), PhoneStateListener.LISTEN_CALL_STATE);
4。创建MyCallListener类
class MyCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// On call you replace the ringtone with your own mUri
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_RINGTONE,
mUri
);
break;
case TelephonyManager.CALL_STATE_IDLE:
// Restore the default ringtone
RingtoneManager.setActualDefaultRingtoneUri(
MainActivity.this,
RingtoneManager.TYPE_RINGTONE,
mOldUri
);
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
5。添加权限您的AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
6。完成
主要思想是挂钩的来电,并以自己更换铃声。当然,你应该调用后恢复默认铃声保存的价值。
6. Done
The main idea is hooking for incoming call and replace the ringtone by your own. And of course you should restore default ringtone to saved value after call.
这篇关于来电动态覆盖默认铃声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!