大佬教程收集整理的这篇文章主要介绍了android – NotificationCompat setSound(声音,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(); }
这是我的工作警报通知:
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,请注明来意。