NotificationCompat的setSound(sound, STREAM_ALARM)方法无法使用

5

我正在构建一个通过蓝牙查找丢失手机的ping功能。我需要让手机发出声音,即使它已设置为静音模式,就像闹钟通常工作的方式一样。我认为可以将我的notificationstreamtype设置为AudioManager.STREAM_ALARM,但这并没有起作用。只有当手机声音开启时才会发出声音。这是我的设置:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_spenwallet)
        .setContentTitle("Ping")
        .setContentText("Device is trying to find your phone.")
        .setAutoCancel(false)
        .setSound(sound, STREAM_ALARM)
        .setVibrate(vibratePattern)
        .addAction(cancelAction);

如果我尝试:

    Notification notification = builder.build();
    notification.audioStreamType = AudioManager.STREAM_ALARM;

我在Android Studio中收到了有关audioStreamType已被弃用的警告。这是真的吗?即使静音模式开启,还有其他方法可以使通知声音响起吗?(最好也有震动)

我通过创建专门的mediaplayer来实现它,但我认为这不应该是必需的。以下是我如何做到的:

    MediaPlayer mediaPlayer = new MediaPlayer();
    final String packageName = getApplicationContext().getPackageName();
    Uri sound = Uri.parse("android.resource://" + packageName + "/" + R.raw.ping_sound);

    try {
        mediaPlayer.setDataSource(this, sound);
    } catch (IOException e) {
        e.printStackTrace();
    }

    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mediaPlayer.setLooping(false);
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
    } 
1个回答

1

使用builder.setSound(alarmSound, AudioManager.STREAM_AUDIO)正是我需要的来保持我的闹钟响起!也许你的问题在于你正在使用的R.raw.ping_sound声音样本。在尝试了一堆糟糕的在线实现闹钟通知(在那里我找到了Settings.System.DEFAULT_RINGTONE_URI)之后,我遵循了官方通知文档,然后使用了NotificationCompat.Builder文档进行自定义。

这是我的可工作的闹钟通知:

private void showNotification(){
    // Setup Intent for when Notification clicked
    Intent intent = new Intent(mContext, MedsActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // See https://developer.android.com/training/notify-user/navigation for better navigation
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

    // Setup Ringtone & Vibrate
    Uri alarmSound = Settings.System.DEFAULT_RINGTONE_URI;
    long[] vibratePattern = { 0, 100, 200, 300 };

    // Setup Notification
    String channelID = mContext.getResources().getString(R.string.channel_id_alarms);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, channelID)
            .setContentText(notificationMessage)
            .setContentTitle(notificationTitle)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(pendingIntent)
            .setSound(alarmSound, AudioManager.STREAM_ALARM)
            .setOnlyAlertOnce(true)
            .setVibrate(vibratePattern)
            .setAutoCancel(true);

    // Send Notification
    NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, mBuilder.build());
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接