android原生中,如果通话期间设定的闹钟时间到了,为了不错过闹钟同时又尽量减小对当前通话的影响,对用户设置的闹钟提醒方式进行了修改:

  1. mMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);设置铃声音量为最大音量的0.125倍
        // Volume suggested by media team for in-call alarms.
        private static final float IN_CALL_VOLUME = 0.125f;
  2. alarmNoise = getInCallRingtoneUri(context); 播放特定的铃声(嘟嘟–嘟嘟–的声音)

那如果我们想要修改原生的提醒方式该到哪修改呢?

Android L

packages/apps/DeskClock/src/com/android/deskclock/alarms/AlarmKlaxon.java

try {
    // Check if we are in a call. If we are, use the in-call alarm
    // resource at a low volume to not disrupt the call.
    if (inTelephoneCall) {
        LogUtils.v("Using the in-call alarm");
        sMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
        setDataSourceFromResource(context, sMediaPlayer, R.raw.in_call_alarm);
    } else {
        sMediaPlayer.setDataSource(context, alarmNoise);
    }
    startAlarm(context, sMediaPlayer);
}

修改成通话中取消震动

        if (instance.mVibrate) {
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            // Check if we are in a call. If we are, cancel the vibrator.
            try {
                if (inTelephoneCall){
                    vibrator.cancel();
                }else{
                    vibrator.vibrate(sVibratePattern, 0, VIBRATION_ATTRIBUTES);
                }
            }catch (Exception ex){
            }
//            vibrator.vibrate(sVibratePattern, 0, VIBRATION_ATTRIBUTES);
        }

Android N

7.0 上代码进行了调整,震动还是在这个类里面,铃声的设置移到了另一个类。

packages/apps/DeskClock/src/com/android/deskclock/alarms/AlarmKlaxon.java

    public static void start(Context context, AlarmInstance instance) {
        // Make sure we are stopped before starting
        stop(context);
        LogUtils.v("AlarmKlaxon.start()");
        if (!AlarmInstance.NO_RINGTONE_URI.equals(instance.mRingtone)) {
            getAsyncRingtonePlayer(context).play(instance.mRingtone);
        }
        if (instance.mVibrate) {
            final Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            if (Utils.isLOrLater()) {
                vibrateLOrLater(vibrator);
            } else {
                vibrator.vibrate(sVibratePattern, 0);
            }
        }
        sStarted = true;
    }

若要取消震动,在上面的代码段修改。

packages/apps/DeskClock/src/com/android/deskclock/AsyncRingtonePlayer.java

        /**
         * Starts the actual playback of the ringtone. Executes on ringtone-thread.
         */
        @Override
        public boolean play(final Context context, Uri ringtoneUri) {
            checkAsyncRingtonePlayerThread();
            LogUtils.i(TAG, "Play ringtone via android.media.MediaPlayer.");
...
            boolean scheduleVolumeAdjustment = false;
            try {
                // Check if we are in a call. If we are, use the in-call alarm resource at a
                // low volume to not disrupt the call.
                if (isInTelephoneCall(context)) {//如果在电话中
                    LogUtils.v("Using the in-call alarm");
                    mMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);    //设置音量为0.125f
                    alarmNoise = getInCallRingtoneUri(context);    //设置铃声为InCall专用铃声
                } else if (isCrescendoEnabled(context)) {    //如果开启“声音渐强”
                    mMediaPlayer.setVolume(0, 0);    //先设为最小生硬(无声)
                    // Compute the time at which the crescendo will stop.
                    mCrescendoDuration = getCrescendoDurationMillis(context);
                    mCrescendoStopTime = System.currentTimeMillis() + mCrescendoDuration;
                    scheduleVolumeAdjustment = true;
                }
                // If alarmNoise is a custom ringtone on the sd card the app must be granted
                // android.permission.READ_EXTERNAL_STORAGE. Pre-M this is ensured at app
                // installation time. M+, this permission can be revoked by the user any time.
                mMediaPlayer.setDataSource(context, alarmNoise);
                startAlarm(mMediaPlayer);
                scheduleVolumeAdjustment = true;
            } catch (Throwable t) {
                ...
            }
            return scheduleVolumeAdjustment;
        }
    }

若要定制铃声,在上面的代码段修改。