Unlock Android phone programmatically?

折月煮酒 提交于 2019-12-01 02:25:21

问题


I want to write the code on how to unlock the Android Phone programmatically.

I want lock or unlock the phone when the user taps the proximity sensor.

public class MyActivity extends Activity{   

    private static final String ACTION = "android.intent.action.ACTION_SCREEN_OFF";
    BroadcastReceiver myReceiver;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        context = this;
        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION);

        context.registerReceiver(myReceiver, theFilter);
        System.out.println("inside increate");
        myReceiver = new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub      
                    System.out.println("phone locked*****");                
            }

        };   

    }}

回答1:


@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
         IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
            registerReceiver(mIntentReceiver, filter);
            System.out.println("BROADcast receiver registered****");
    }

     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

                System.out.println("phone locked"); 

        }



回答2:


Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

An Alternative solution... try this to unlock the screen..



来源:https://stackoverflow.com/questions/9004720/unlock-android-phone-programmatically

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