Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – NotificationCompat setSound(声音,STREAM_ALARM)不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我建立一个ping功能,通过蓝牙找到丢失的手机.我需要手机发出声音,即使它被设置为静音/静音,就像闹钟通常如何工作一样.我以为我可以将我的通知的streamtype放到Au@L_219_3@manager.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 = Au@L_219_3@manager.STREAM_ALARM;

我从Android studio收到一条警告,即不推荐使用audioStreamType.是这样的吗?即使打开静音模式,还有其他方法可以发出通知声音吗? (最好是振动)

我为此目的创建了一个专用的媒体播放器,但我认为不应该这样做.继承人我是怎么做到的:

@H_694_7@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 Au@L_219_3@manager au@L_219_3@manager = (Au@L_219_3@manager) getSystemservice(Context.AUdio_serviCE); if (au@L_219_3@manager.getStreamVolume(Au@L_219_3@manager.STREAM_ALARM) != 0) { mediaPlayer.setAudioStreamType(Au@L_219_3@manager.STREAM_ALARM); mediaPlayer.setLooping(false); try { mediaPlayer.prepare(); } catch (IOException E) { e.printStackTrace(); } mediaPlayer.start(); }

解决方法

使用builder.setSound(alarmSound,Au@L_219_3@manager.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,Au@L_219_3@manager.STREAM_ALARM)
            .setOnlyAlertOnce(true)
            .setVibrate(vibratePattern)
            .setAutoCancel(true);

    // Send Notification
    notificationmanager manager = (notificationmanager) mContext.getSystemservice(NOTIFICATION_serviCE);
    manager.notify(NOTIFICATION_ID,mBuilder.build());
}

大佬总结

以上是大佬教程为你收集整理的android – NotificationCompat setSound(声音,STREAM_ALARM)不起作用全部内容,希望文章能够帮你解决android – NotificationCompat setSound(声音,STREAM_ALARM)不起作用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。