Continue vibration even after the screen goes to sleep mode in Android

限于喜欢 提交于 2019-12-05 19:58:28
Anu

The correct answer to the question is as follows

Before doing this, don't forget to add the permission "android.permission.VIBRATE" to your app manifest file.

public BroadcastReceiver vibrateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            vibe.vibrate(pattern, 0);
        }
    }
};

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(vibrateReceiver, filter);

wakelock will not work here, because the receiver will receive the intent only after the screen goes off. Though we can acquire the wakelock after the screen goes to off mode the vibration stops, because it happens with the ACTION_SCREEN_OFF. So it can be done by starting the vibration again after receiving the broadcast.

Try this it might help you. First make broadcast receiver for this such that when mobile light screen off then write logic of vibrate mobile.

 public BroadcastReceiver wakeLockReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            //WRITE LOGIC OF VIBRATION.
        }
    }
};

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(wakeLockReceiver, filter);

Add permission in AndroidManifest.xml

<uses-permission android:name="android.permission.VIBRATE"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!