android – NotificationCompat setSound(声音,STREAM_ALARM)不起作用

我建立一个ping功能,通过蓝牙找到丢失的手机.我需要手机发出声音,即使它被设置为静音/静音,就像闹钟通常如何工作一样.我以为我可以将我的通知的streamtype放到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 = 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();
    }

解决方法

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...