屏幕锁定从接收器

时间:2013-03-05 06:34:56

标签: android

我正在尝试锁定并解锁屏幕,

我正在做的事情如下  一个Broadcast receiver检查屏幕是否为OFF,如果屏幕是ON,它将锁定屏幕,如果OFF,它将解锁屏幕。

我在Broadcast receiver中使用的代码是

public void onReceive(Context context, Intent intent) {

        System.out.println("Entered Broadcaste Reciever........");
       context1 = context;
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            // DO WHATEVER YOU NEED TO DO HERE
             mShaker = new ShakeListener(context);
                mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
                  public void onShake()
                  {
                     PowerManager TempPowerManager = (PowerManager) context1.getSystemService(Context.POWER_SERVICE);
                    PowerManager.WakeLock TempWakeLock = TempPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE, "TempWakeLock");
                    TempWakeLock.acquire();
                    final Vibrator vibe = (Vibrator)context1.getSystemService(Context.VIBRATOR_SERVICE);
                    vibe.vibrate(100);
            System.out.println("LISTENING SHAKE");
                  }
                });

        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // AND DO WHATEVER YOU NEED TO DO HERE

             mShaker = new ShakeListener(context);
                mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
                  public void onShake()
                  {

                        mDPM = (DevicePolicyManager)context1.getSystemService(Context.DEVICE_POLICY_SERVICE);



                    ComponentName mAdminName = new ComponentName(context1,LockActivity.class);
                    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
                    filter.addAction(Intent.ACTION_SCREEN_OFF);
                    BroadcastReceiver mReceiver = new ScreenReceiver();
                    context1.registerReceiver(mReceiver, filter);
                    System.out.println("The Device  device admin enabled");
                    Intent  intent = new             Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
                                           intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"onEnabled");
                                   mDPM.lockNow();
                                   mDPM.setMaximumTimeToLock(mAdminName,0);
                                   intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
   //                                           startActivityForResult(intent, 1); 
                  }
                });
        }

问题是它正在正常启动并且最多5次正常工作,之后它将进入无限状态并且设备被卡住。有人帮我找到解决方案

1 个答案:

答案 0 :(得分:1)

现在,需要记住的是,系统屏幕关闭前事件的顺序是:

  

ExampleActivity.onPause() - > ScreenReceiver.onReceive()

这有点不直观,因为你认为接收器会先受到攻击 - 所以当你玩设置布尔等时,要注意这个小事实,同样当屏幕打开事件的顺序时是:

  

ExampleActivity.onResume() - > ScreenReceiver.onReceive()

 @Override
    protected void onPause() {
        // when the screen is about to turn off
        if (ScreenReceiver.wasScreenOn) {
            // this is the case when onPause() is called by the system due to a screen state change
            System.out.println("SCREEN TURNED OFF");
        } else {
            // this is when onPause() is called when the screen state has not changed
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        // only when screen turns on
        if (!ScreenReceiver.wasScreenOn) {
            // this is when onResume() is called due to a screen state change
            System.out.println("SCREEN TURNED ON");
        } else {
            // this is when onResume() is called when the screen state has not changed
        }
        super.onResume();
    }

Reference from here