如何在android中获取通知音uri

如何在android中获取通知音uri,android,Android,我想从我的android设备获取通知音URI 如何获取我设置为通知音调的特定音调的uri?我猜您想在应用程序中播放的铃声是什么 如果希望通知播放自定义声音,有两个选项: 使用直接的Uri(!=Uri) 使用internal MediaStore的内容提供程序(如果您希望用户选择自己的曲调,可以使用此功能) 可以找到一些例子 因此,假设您的叮当声位于您的应用程序res/raw-文件夹中,您将看到以下内容(请参见此问题:): 如果您的文件位于手机SD卡上,您可以执行以下操作: Uri jingle

我想从我的android设备获取通知音URI


如何获取我设置为通知音调的特定音调的uri?

我猜您想在应用程序中播放的铃声是什么

如果希望通知播放自定义声音,有两个选项:

  • 使用直接的
    Uri
    !=Uri
  • 使用internal MediaStore的
    内容提供程序
    (如果您希望用户选择自己的曲调,可以使用此功能)
  • 可以找到一些例子

    因此,假设您的叮当声位于您的应用程序
    res/raw
    -文件夹中,您将看到以下内容(请参见此问题:):

    如果您的文件位于手机SD卡上,您可以执行以下操作:

    Uri jingle = Uri.parse("file:///sdcard/jingle.mp3");
    

    只需从我的一个应用程序中复制/粘贴一些代码,即可满足您的需求

    这在标记为“设置铃声”或类似按钮的onClick处理程序中:

    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
    this.startActivityForResult(intent, 5);
    
    此代码捕获用户所做的选择:

     @Override
     protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
     {
         if (resultCode == Activity.RESULT_OK && requestCode == 5)
         {
              Uri uri = Intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    
              if (uri != null)
              {
                  this.chosenRingtone = uri.toString();
              }
              else
              {
                  this.chosenRingtone = null;
              }
          }            
      }
    

    此外,我建议我的用户从Android Market安装“Rings Extended”应用程序。然后,无论何时在他们的设备上打开此对话框,例如从我的应用程序或手机的“设置”菜单,用户都可以选择存储在他们设备上的任何MP3,而不仅仅是内置铃声。

    我使用了下面的格式,它对我很有效。我在我的资源原始文件夹中有音调。它有消息和通知图标,但音调是特定的。试试这个,我想它会帮你的

    Notification notification = new NotificationCompat.Builder(getApplicationContext())
                    .setContentTitle("Your Title")
                    .setContentText(Html.fromHtml(String.format("New message", messageThreadArrayList.size() > 1 ? "s" : "")))
                    .setContentIntent(messagePendingIntent)
                    .setLargeIcon(largeIcon)
                    .setAutoCancel(true)
                    .setSound(Uri.parse(String.format(Locale.ENGLISH, "android.resource://%s/%d", getPackageName(), R.raw.notification_tone)))
                    .setSmallIcon(R.mipmap.ic_olla_logo_action_bar)
                    .setStyle(inboxStyle.setBigContentTitle("Your Title"))
                    .addAction(R.mipmap.notification_icon, "Reply", messagePendingIntent)
                    .build();
    
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notificationManager.notify(Configuration.NOTIFICATION_ID2, notification);
        }
    
    这说明了如何在代码中并利用首选项文件来实现这一点。
    Notification notification = new NotificationCompat.Builder(getApplicationContext())
                    .setContentTitle("Your Title")
                    .setContentText(Html.fromHtml(String.format("New message", messageThreadArrayList.size() > 1 ? "s" : "")))
                    .setContentIntent(messagePendingIntent)
                    .setLargeIcon(largeIcon)
                    .setAutoCancel(true)
                    .setSound(Uri.parse(String.format(Locale.ENGLISH, "android.resource://%s/%d", getPackageName(), R.raw.notification_tone)))
                    .setSmallIcon(R.mipmap.ic_olla_logo_action_bar)
                    .setStyle(inboxStyle.setBigContentTitle("Your Title"))
                    .addAction(R.mipmap.notification_icon, "Reply", messagePendingIntent)
                    .build();
    
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notificationManager.notify(Configuration.NOTIFICATION_ID2, notification);
        }