如何在用户通话时播放通知声音?
问题描述:
我正在开发一个应用程序,它可以持续检测来自特定信标的BLE信号。如果这些信标的电池即将耗尽,信号的内容将会改变。因此,我可以提示用户哪个信标即将死亡,他或她可能需要更换电池或为其充电。如何在用户通话时播放通知声音?
我把检测服务,在一般情况下,它只是完美的作品。无论应用程序处于前台还是后台,只要应用程序检测到异常信号,应用程序就会通过振动和声音向用户发送通知。以下是Notification
在我的代码设置:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(contentIntent)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setSound(sound)
.setVibrate(new long[]{INTERVAL_VIBRATE, INTERVAL_VIBRATE});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setPriority(Notification.PRIORITY_DEFAULT);
builder.setFullScreenIntent(contentIntent, true);
}
if (!TextUtils.isEmpty(message)) {
builder.setContentText(message);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
这里谈到的问题:
当用户在他或她将收到通知,以及振动煲电话粥。但无法收到通知声音,我想知道是否有办法完成此功能。
答
我终于想出了一个替代解决方案:使用ToneGenerator
来播放声音。
在这种情况下,当我仅使用Notification
时,即使振动仍然有效,通知的声音在手机上消失了,但我想要找到提示用户他或她的高级方法需要提防某事。
所以,在我的服务,我想补充PhoneStateListener
监控我的手机状态
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = createPhoneStateListener();
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
我createPhoneStateListener()方法:
private PhoneStateListener createPhoneStateListener() {
return new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_IDLE :
isUserSpeakingOnPhone = false;
break;
case TelephonyManager.CALL_STATE_RINGING :
isUserSpeakingOnPhone = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK :
isUserSpeakingOnPhone = true;
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
}
我用一个布尔isUserSpeakingOnPhone,以确定我是否需要开始IntentService
,其中调用PlayToneService
来产生音调,并在需要推送通知时同时播放:
if (isUserSpeakingOnPhone) {
Intent playTone = new Intent(this, PlayToneService.class);
startService(playTone);
}
PlayToneService.class:
public class PlayToneService extends IntentService {
public PlayToneService() {
super(PlayToneService.class.getName());
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
// play tone
try {
ToneGenerator toneGenerator= new ToneGenerator(AudioManager.STREAM_SYSTEM, ToneGenerator.MAX_VOLUME);
toneGenerator.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
Thread.sleep(200);
toneGenerator.stopTone();
toneGenerator.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
然后,当用户正在接听电话和收到的通知,而不是notfication声音,他/她会听到从设备的扬声器“哔”声。当用户不在电话上发言时,通知声音将照常工作。
我怀疑有人想在通话时收到通知声音。只是说。 –
@ H.Brooks这款应用程序用于监控由安装在房间或车辆中的信标生成的紧急事件信号。如果发生了什么事情,用户需要得到通知。 –
你试过看优先领域吗?另一件事是尝试查看抬头通知可能会介绍一些见解。 – JoxTraex