문제

Prior to Android 4.2.1 the following code worked just fine

notification.audioStreamType = AudioManager.STREAM_RING;
notification.flags = notification.flags | Notification.FLAG_INSISTENT
        | Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(ID_NOTIFICATION_SOUND, notification);

Vibration is done separately, depending on the state of a preference setting.

vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(vibr_pattern1, 0);

Now with Android 4.2.1+ there is always vibration even if the phone is set to silent (no vibration) and set to vibrate mode, even if the app settings for vibration are set to off. Interestingly there is no vibration when the phone is in normal mode.

As mentioned, everything worked fine before 4.2.1.

Can someone help? What did google change here? Is it related to this bug report?

Regards,
Andreas

도움이 되었습니까?

해결책

I fixed it like this:

//vibration workaround for android 4.2.1+
notification.vibrate = new long[]{0,0};
//end workaround

I manually add "no vibration" and if the user chooses vibration it's done with an Vibrator object. If not, the code above keeps everything quiet. As mentioned before, this was not necessary before 4.2.1+

다른 팁

I know this is old. But for some one who is looking for answer. You can try this.

AudioManager audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
     Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
    switch( audio.getRingerMode() ){
    case AudioManager.RINGER_MODE_NORMAL:
        try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
            r.play();
            v.vibrate(500);
        } catch (Exception e) {
            e.printStackTrace();
        }
       break;
    case AudioManager.RINGER_MODE_SILENT:
       break;
    case AudioManager.RINGER_MODE_VIBRATE:
         v.vibrate(500);
       break;
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top