0

I wan to start Vibrate When Locked Screen. My Screen is open/working when wake lock. My Question is that Vibrate is not working when Screen Locked Mode. if Device Locked is open then working.

onCreate()

KeyguardManager myKM = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
        if( myKM.inKeyguardRestrictedInputMode()) {
            //it is locked
            Log.e("Incomimg","Device Locked");
            onStartVibrate();

        } else {
            //it is not locked
            Log.e("Incomimg","Device Not Locked");
        }

See Below Methods are for Vibrate on/off

 public void onStartVibrate()
    {
        long[] pattern = {500, 250, 0 ,100,0, 250, 500};
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        vibrator.vibrate(pattern,0);
    }

    public void onStopVibrate(){

        try{

            vibrator.cancel();

        }catch (Exception ex)
        {

       }
    }
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
Android Devs
  • 125
  • 13

2 Answers2

1

According to the Vibrator Documentation - it says that:

If your process exits, any vibration you started will stop.

As it has its own SystemService - i think there is no "clean" way to solve this issue

Maxi
  • 979
  • 10
  • 16
0

try using notifications:

 try {

            long[] pattern = {500, 250, 0 ,100,0, 250, 500};
            Notification noti = new Notification.Builder(this)
                    .setVibrate(pattern)
                    .build();
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.notify(1, noti);
        }catch(Exception vv)
        {
            Log.i("not vibrating",vv.getLocalizedMessage());
        }
henrybbosa
  • 1,139
  • 13
  • 28
  • sorry, its not working. I am Implementing incoming call screen vibrate is working but device locked then not work, what is this issue ? – Android Devs Jul 26 '16 at 07:35
  • tried it as well and you need to add a `setSmallIcon` to get it running. Then it still does terminate vibration, when locking device – Maxi Feb 27 '17 at 15:10