代码之家  ›  专栏  ›  技术社区  ›  Carter Chen

当用户在通话时,如何播放通知声音?

  •  0
  • Carter Chen  · 技术社区  · 8 年前

    我正在开发一个应用程序,可以连续检测特定信标的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());
    

    问题来了:

    当用户在打电话时,他或她将收到通知以及振动。但是 无法接收通知声音 ,我想知道是否有办法实现此功能。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Carter Chen    8 年前

    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);
            }
        };
    }
    

    iUserspeakingonphone 确定是否需要启动 IntentService 哪个叫 PlayToneService

    if (isUserSpeakingOnPhone) {
            Intent playTone = new Intent(this, PlayToneService.class);
            startService(playTone);
    
    }
    

    PlayToneService。类别:

    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();
            }
        }
    }