以编程方式解锁Android手机?

时间:2012-01-25 14:54:33

标签: android locking screen unlock

我想编写有关如何以编程方式解锁Android手机的代码。

当用户点击接近传感器时,我想锁定或解锁手机。

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*****");                
            }

        };   

    }}

2 个答案:

答案 0 :(得分:4)

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);

另类解决方案...尝试将其解锁屏幕..

答案 1 :(得分:3)

@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"); 

        }