How to lock/unlock phone programmatically : Android

江枫思渺然 提交于 2019-11-27 13:46:51
sharath yadhav

on BroadcastReceiver set up the wakelock and in the activity

Do This:

Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

import following

import android.view.Window;
import android.view.WindowManager.LayoutParams;

it solved my problem ,rate it up:-)))

As of API 27, FLAG_SHOW_WHEN_LOCKED and FLAG_TURN_SCREEN_ON were deprecated. Instead use this in the Manifest

<activity
    android:name=".MyActivity"
    android:turnScreenOn="true"
    android:showWhenLocked="true"/>

Or, for general usage, in your activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setTurnScreenOn(true);
        setShowWhenLocked(true);
    } else {
        Window window = getWindow();
        window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
    }
}

Try this...

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
                    setContentView(R.layout.activity_main);
    }

    @Override
        protected void onPause() {
            super.onPause();
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);


        }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!