【问题标题】:NotificationCompat setSound(sound, STREAM_ALARM) not workingNotificationCompat setSound(声音,STREAM_ALARM)不工作
【发布时间】:2017-02-01 08:36:49
【问题描述】:

我正在构建一个 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。是这样吗?即使打开静音模式,还有其他方法可以使notificaiton 发出声音吗? (最好也振动)

我为此目的创建了一个专用的媒体播放器,但我认为不需要这样做。无论如何,我是这样做的:

    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();
    } 

【问题讨论】:

    标签: android audio android-notifications android-permissions


    【解决方案1】:

    使用builder.setSound(alarmSound, AudioManager.STREAM_AUDIO) 正是我需要让我的闹钟继续运行!也许您的问题与您正在使用的 R.raw.ping_sound 声音样本有关。在尝试了一堆糟糕的实现后,我在网上找到了警报通知(这是我找到 Settings.System.DEFAULT_RINGTONE_URI 的地方),我按照 official notification documentation 进行操作,然后使用 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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多