Android Notification Builder声音无法运行

时间:2019-09-15 18:00:57

标签: java android

在调试时uri有一个值,但是set.sound不起作用,也不运行声音程序。

private void notification(){
uri = intent.getData();
int notifictionId = 1;
Notification.Builder Builder = new Notification.Builder(this)

    .setSmallIcon(R.drawable.eye_rest)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.eye_rest))
    .setContentTitle("Eye rest")
    .setContentText("It's time to rest the eyes")
    .setAutoCancel(true)
    .setVibrate(new long[] { 600, 600 })
    .setSound(uri);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  String channelId = "YOUR_CHANNEL_ID";
  NotificationChannel channel = new NotificationChannel(channelId,"Channel human readable title",NotificationManager.IMPORTANCE_DEFAULT);
  channel.enableLights(true);
  channel.setLightColor(Color.BLUE);
  channel.setSound(uri,null);
  Builder.setVisibility(Notification.VISIBILITY_PRIVATE);
  notificationManager.createNotificationChannel(channel);
  Builder.setChannelId(channelId);
}
notificationManager.notify(notifictionId, Builder.build());

然后我通过此代码从手机的铃声列表中选择声音:

    intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            uri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_RINGTONE );
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_RINGTONE);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,  "Choose a song for notification");
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            startActivity(intent);

0 个答案:

没有答案