修改通话期间闹钟的提醒方式

版权声明: https://blog.csdn.net/aaa111/article/details/70310251
android原生中,如果通话期间设定的闹钟时间到了,为了不错过闹钟同时又尽量减小对当前通话的影响,对用户设置的闹钟提醒方式进行了修改:
1. mMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);设置铃声音量为最大音量的0.125倍
2. alarmNoise = getInCallRingtoneUri(context); 播放特定的铃声(嘟嘟--嘟嘟--的声音)

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

Android L

packages/apps/DeskClock/src/com/android/deskclock/alarms/AlarmKlaxon.java
  1.            try {
  2. // Check if we are in a call. If we are, use the in-call alarm
  3. // resource at a low volume to not disrupt the call.
  4. if (inTelephoneCall) {
  5. LogUtils.v("Using the in-call alarm");
  6. sMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
  7. setDataSourceFromResource(context, sMediaPlayer, R.raw.in_call_alarm);
  8. } else {
  9. sMediaPlayer.setDataSource(context, alarmNoise);
  10. }
  11. startAlarm(context, sMediaPlayer);
  12. }

修改成通话中取消震动
  1. if (instance.mVibrate) {
  2. Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  3. // Check if we are in a call. If we are, cancel the vibrator.
  4. try {
  5. if (inTelephoneCall){
  6. vibrator.cancel();
  7. }else{
  8. vibrator.vibrate(sVibratePattern, 0, VIBRATION_ATTRIBUTES);
  9. }
  10. }catch (Exception ex){
  11. }
  12. // vibrator.vibrate(sVibratePattern, 0, VIBRATION_ATTRIBUTES);
  13. }

Android N

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

packages/apps/DeskClock/src/com/android/deskclock/alarms/AlarmKlaxon.java
  1. public static void start(Context context, AlarmInstance instance) {
  2. // Make sure we are stopped before starting
  3. stop(context);
  4. LogUtils.v("AlarmKlaxon.start()");
  5. if (!AlarmInstance.NO_RINGTONE_URI.equals(instance.mRingtone)) {
  6. getAsyncRingtonePlayer(context).play(instance.mRingtone);
  7. }
  8. if (instance.mVibrate) {
  9. final Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  10. if (Utils.isLOrLater()) {
  11. vibrateLOrLater(vibrator);
  12. } else {
  13. vibrator.vibrate(sVibratePattern, 0);
  14. }
  15. }
  16. sStarted = true;
  17. }
若要取消震动,在上面的代码段修改。

packages/apps/DeskClock/src/com/android/deskclock/AsyncRingtonePlayer.java
  1. /**
  2. * Starts the actual playback of the ringtone. Executes on ringtone-thread.
  3. */
  4. @Override
  5. public boolean play(final Context context, Uri ringtoneUri) {
  6. checkAsyncRingtonePlayerThread();
  7. LogUtils.i(TAG, "Play ringtone via android.media.MediaPlayer.");
  8. ...
  9. boolean scheduleVolumeAdjustment = false;
  10. try {
  11. // Check if we are in a call. If we are, use the in-call alarm resource at a
  12. // low volume to not disrupt the call.
  13. if (isInTelephoneCall(context)) {//如果在电话中
  14. LogUtils.v("Using the in-call alarm");
  15. mMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);    //设置音量为0.125f
  16. alarmNoise = getInCallRingtoneUri(context);    //设置铃声为InCall专用铃声
  17. } else if (isCrescendoEnabled(context)) {    //如果开启“声音渐强”
  18. mMediaPlayer.setVolume(0, 0);    //先设为最小生硬(无声)
  19. // Compute the time at which the crescendo will stop.
  20. mCrescendoDuration = getCrescendoDurationMillis(context);
  21. mCrescendoStopTime = System.currentTimeMillis() + mCrescendoDuration;
  22. scheduleVolumeAdjustment = true;
  23. }
  24. // If alarmNoise is a custom ringtone on the sd card the app must be granted
  25. // android.permission.READ_EXTERNAL_STORAGE. Pre-M this is ensured at app
  26. // installation time. M+, this permission can be revoked by the user any time.
  27. mMediaPlayer.setDataSource(context, alarmNoise);
  28. startAlarm(mMediaPlayer);
  29. scheduleVolumeAdjustment = true;
  30. } catch (Throwable t) {
  31. ...
  32. }
  33. return scheduleVolumeAdjustment;
  34. }
若要定制铃声,在上面的代码段修改。

展开阅读全文

没有更多推荐了,返回首页