我正在建立ping功能,通过蓝牙找到丢失的手机。我需要手机发出声音,即使它被设置为静音/静音,就像闹钟通常如何工作一样。我以为我可以将streamtype
notification
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;
如果我尝试:
notificaiton
我从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();
}
声音吗? (最好是振动)
我为此目的创建了一个专用的媒体播放器,但我认为不应该这样做。继承人我是怎么做到的:
InstanceContextMode = InstanceContextMode.PerSession
答案 0 :(得分:0)
使用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());
}