我建立一个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()); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。