如何以编程方式更改 Android 中的铃声?

发布于 2024-11-10 13:49:05 字数 533 浏览 4 评论 0原文

我正在尝试编写一个应用程序(除其他外)将根据用户的位置更改用户的铃声。

但是,我在应用程序中设置手机铃声时遇到困难。我已经能够显示手机铃声列表,并且一直在使用以下代码来尝试设置铃声:

RingtoneManager.setActualDefaultRingtoneUri(applicationContext, 
      RingtoneManager.TYPE_RINGTONE,
      MediaStore.Audio.Media.getContentUriForPath(settings.getRingtoneURI()));

Settings.System.putString(c.getContentResolver(), Settings.System.RINGTONE, 
      settings.getRingtoneURI());

其中 settings.getRingtoneURI() 返回一个包含所需铃声 URI 的字符串。

当我运行它时,我没有收到任何错误,但铃声没有改变。

有什么建议吗?

I'm trying to write an app that (among other things) will change the user's ringtone based on their location.

However, I'm having difficulty setting the ringtone of my phone from within my app. I've been able to display a list of the phone's ringtones, and have been using the following code to try and set the ringtone:

RingtoneManager.setActualDefaultRingtoneUri(applicationContext, 
      RingtoneManager.TYPE_RINGTONE,
      MediaStore.Audio.Media.getContentUriForPath(settings.getRingtoneURI()));

Settings.System.putString(c.getContentResolver(), Settings.System.RINGTONE, 
      settings.getRingtoneURI());

where settings.getRingtoneURI() returns a string with the URI of the desired ringtone.

When I run this, I receive no errors but the ringtone does not change.

Any advice?

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

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

发布评论

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

评论(1

滥情空心 2024-11-17 13:49:05

下面的代码选择来自手机的任何随机铃声作为来电。

            RingtoneManager rm = new RingtoneManager(context);
    Random random = new Random();

    int i = rm.getRingtonePosition(RingtoneManager
            .getActualDefaultRingtoneUri(context,
                    RingtoneManager.TYPE_RINGTONE));
    MyApplication.APPLICATION_SHARED_PREFERENCE.edit()
            .putInt(MyConstants.PHONE_RINGTONE_NUMBER, i).commit();
    int chanegToneNumber;
    Cursor cursor = rm.getCursor();

    while (true) {
        chanegToneNumber = random.nextInt(cursor.getCount());
        if (chanegToneNumber != i)
            break;
    }

    Log.d(TAG, "Tone: " + i);

    Log.d(TAG, "Tone total: " + cursor.getCount());

    while (cursor.moveToNext()) {

        if (i == cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID))) {
            RingtoneManager.setActualDefaultRingtoneUri(context,
                    RingtoneManager.TYPE_RINGTONE,
                    rm.getRingtoneUri(chanegToneNumber));
            break;
        }
    }

The below code choose any random tone from the mobile for Incoming call.

            RingtoneManager rm = new RingtoneManager(context);
    Random random = new Random();

    int i = rm.getRingtonePosition(RingtoneManager
            .getActualDefaultRingtoneUri(context,
                    RingtoneManager.TYPE_RINGTONE));
    MyApplication.APPLICATION_SHARED_PREFERENCE.edit()
            .putInt(MyConstants.PHONE_RINGTONE_NUMBER, i).commit();
    int chanegToneNumber;
    Cursor cursor = rm.getCursor();

    while (true) {
        chanegToneNumber = random.nextInt(cursor.getCount());
        if (chanegToneNumber != i)
            break;
    }

    Log.d(TAG, "Tone: " + i);

    Log.d(TAG, "Tone total: " + cursor.getCount());

    while (cursor.moveToNext()) {

        if (i == cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID))) {
            RingtoneManager.setActualDefaultRingtoneUri(context,
                    RingtoneManager.TYPE_RINGTONE,
                    rm.getRingtoneUri(chanegToneNumber));
            break;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文