1. 程式人生 > >android4.4 手機通話中收到簡訊,未有響鈴或震動提示

android4.4 手機通話中收到簡訊,未有響鈴或震動提示

原來的程式碼

// If we're not supposed to beep, vibrate, etc. then don't.
if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS)
     == 0 || (smsRingtone && mInCall))
     && (!(old != null
         && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
     && (r.getUserId() == UserHandle.USER_ALL ||
         (r.getUserId() == userId && r.getUserId() == currentUser))
     && canInterrupt
     && mSystemReady) {

 final AudioManager audioManager = (AudioManager) mContext
 .getSystemService(Context.AUDIO_SERVICE);

 // sound

 // should we use the default notification sound? (indicated either by
 // DEFAULT_SOUND or because notification.sound is pointing at
 // Settings.System.NOTIFICATION_SOUND)
 final boolean useDefaultSound =
        (notification.defaults & Notification.DEFAULT_SOUND) != 0 ||
                Settings.System.DEFAULT_NOTIFICATION_URI
                        .equals(notification.sound);

 Uri soundUri = null;
 boolean hasValidSound = false;

 if (useDefaultSound) {
     soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;

     // check to see if the default notification sound is silent
     ContentResolver resolver = mContext.getContentResolver();
     hasValidSound = Settings.System.getString(resolver,
            Settings.System.NOTIFICATION_SOUND) != null;
 } else if (notification.sound != null) {
     soundUri = notification.sound;
     hasValidSound = (soundUri != null);
 }

 if (hasValidSound) {
     boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
     int audioStreamType;
     if (notification.audioStreamType >= 0) {
         audioStreamType = notification.audioStreamType;
     } else {
         audioStreamType = DEFAULT_STREAM_TYPE;
     }
     mSoundNotification = r;
     // do not play notifications if stream volume is 0 (typically because
     // ringer mode is silent) or if there is a user of exclusive audio focus
     if ((audioManager.getStreamVolume(audioStreamType) != 0)
             && !audioManager.isAudioFocusExclusive()) {
         final long identity = Binder.clearCallingIdentity();
         try {
             final IRingtonePlayer player = mAudioService.getRingtonePlayer();
             if (player != null) {
                 player.playAsync(soundUri, user, looping, audioStreamType);
             }
         } catch (RemoteException e) {
         } finally {
             Binder.restoreCallingIdentity(identity);
         }
     }
 }

修改後的程式碼
// If we're not supposed to beep, vibrate, etc. then don't.
if ((!(old != null
    && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
&& (r.getUserId() == UserHandle.USER_ALL ||
    (r.getUserId() == userId && r.getUserId() == currentUser))
&& canInterrupt
&& mSystemReady) {
final AudioManager audioManager = (AudioManager) mContext
        .getSystemService(Context.AUDIO_SERVICE);
boolean hasValidSound = false;
if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS)
        == 0 || (smsRingtone && mInCall)) ) {

    // sound

    // should we use the default notification sound? (indicated either by
    // DEFAULT_SOUND or because notification.sound is pointing at
    // Settings.System.NOTIFICATION_SOUND)
    final boolean useDefaultSound =
           (notification.defaults & Notification.DEFAULT_SOUND) != 0 ||
                   Settings.System.DEFAULT_NOTIFICATION_URI
                           .equals(notification.sound);

    Uri soundUri = null;

    if (useDefaultSound) {
        soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;

        // check to see if the default notification sound is silent
        ContentResolver resolver = mContext.getContentResolver();
        hasValidSound = Settings.System.getString(resolver,
               Settings.System.NOTIFICATION_SOUND) != null;
    } else if (notification.sound != null) {
        soundUri = notification.sound;
        hasValidSound = (soundUri != null);
    }

    if (hasValidSound) {
        boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
        int audioStreamType;
        if (notification.audioStreamType >= 0) {
            audioStreamType = notification.audioStreamType;
        } else {
            audioStreamType = DEFAULT_STREAM_TYPE;
        }
        mSoundNotification = r;
        // do not play notifications if stream volume is 0 (typically because
        // ringer mode is silent) or if there is a user of exclusive audio focus
        if ((audioManager.getStreamVolume(audioStreamType) != 0)
                && !audioManager.isAudioFocusExclusive()) {
            final long identity = Binder.clearCallingIdentity();
            try {
                final IRingtonePlayer player = mAudioService.getRingtonePlayer();
                if (player != null) {
                    player.playAsync(soundUri, user, looping, audioStreamType);
                }
            } catch (RemoteException e) {
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
        }
    }

}

if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0
        || mInCall) {
    // vibrate
    // Does the notification want to specify its own vibration?
    final boolean hasCustomVibrate = notification.vibrate != null;

    // new in 4.2: if there was supposed to be a sound and we're in vibrate
    // mode, and no other vibration is specified, we fall back to vibration
    final boolean convertSoundToVibration =
               !hasCustomVibrate
            && hasValidSound
            && (audioManager.getRingerMode()
                       == AudioManager.RINGER_MODE_VIBRATE);

    // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
    final boolean useDefaultVibrate =
            (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;

    if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
            && !(audioManager.getRingerMode()
                    == AudioManager.RINGER_MODE_SILENT)) {
        mVibrateNotification = r;

        if (useDefaultVibrate || convertSoundToVibration) {
            // Escalate privileges so we can use the vibrator even if the
            // notifying app does not have the VIBRATE permission.
            long identity = Binder.clearCallingIdentity();
            try {
                mVibrator.vibrate(r.sbn.getUid(), r.sbn.getBasePkg(),
                    useDefaultVibrate ? mDefaultVibrationPattern
                        : mFallbackVibrationPattern,
                    ((notification.flags & Notification.FLAG_INSISTENT) != 0)
                            ? 0: -1);
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
        } else if (notification.vibrate.length > 1) {
            // If you want your own vibration pattern, you need the VIBRATE
            // permission
            mVibrator.vibrate(r.sbn.getUid(), r.sbn.getBasePkg(),
                    notification.vibrate,
                ((notification.flags & Notification.FLAG_INSISTENT) != 0)
                        ? 0: -1);
        }
    }
}
}