来电动态覆盖默认铃声

发布于 2024-09-12 19:52:24 字数 281 浏览 5 评论 0原文

我的应用程序允许您为不同的传入事件指定不同的铃声。 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的奇迹 2024-09-19 19:52:24

动态更改铃声有几个步骤。

1. 准备铃声

    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. 完成

主要思想是挂钩来电并替换自己的铃声。当然,您应该在通话后将默认铃声恢复为保存的值。

There are a several steps to dynamically change the ringtone.

1. Prepare 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. Insert it into the database

    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. Save the current default ringtone and subscribe to 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. Create MyCallListener class

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. Add permissions to you AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

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.

孤君无依 2024-09-19 19:52:24

从 API 5 (Android 2.x) 开始,联系人数据库有一个 CUSTOM_RINGTONE 字段,请参阅此页面:

http://developer.android.com/reference/android/provider/ContactsContract.ContactOptionsColumns.html#CUSTOM_RINGTONE

此字段的值必须是 content:// 媒体文件的 URI。您可以从 MedicaStore 内容提供程序获取一个:

http://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html

这可能足以让您开始。这都是标准内容提供商的东西;这两项任务都有大量现有材料。

Starting with API 5 (Android 2.x), the Contacts database has a CUSTOM_RINGTONE field, see this page:

http://developer.android.com/reference/android/provider/ContactsContract.ContactOptionsColumns.html#CUSTOM_RINGTONE

The value for this field must be a content:// URI to a media file. You can obtain one from the MedciaStore content provider:

http://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html

That may be enough to get you started. This is all standard content provider stuff; lots of existing material for both tasks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文