2016-04-24 88 views
1

在Android 6.0項目上,我嘗試使用接近傳感器解鎖手機。disableKeyguard在應用程序上調用凍結手機

這是我對OnSensorChanged事件:

public void onSensorChanged(SensorEvent event) { 
      proxValue = (event.values[0]); //sensor value stored in proxValue 
      keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
      lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 
      screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
    /*when service is running and phone is locked 
    and proximity sensor value is less than 2.0 centimeters*/ 
      if(isMyServiceRunning(StartStopService.class) && lockStatus == true && proxValue < 2){ 
       Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
       v.vibrate(500); // vibrate for 500 ms 
       screenLock.acquire(); //to wake the phone up 
       lock.disableKeyguard(); //to unlock the phone 
       keyguardDisabled = true; // boolean to check keyguard status 

      } 
     } 

在我ScreenReceiver我:

public class ScreenReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
      lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 
      screenLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(
        PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
      if((intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) || !(intent.getAction().equals(Intent.ACTION_USER_PRESENT))) 
      { 
       lockStatus = true; 
       if(keyguardDisabled){ 
        lock.reenableKeyguard(); 
        screenLock.release(); 
        keyguardDisabled = false; 

       } 
      } 
      else 
      { 
       lockStatus = false; 
      } 
     } 
    } 

當我打電話lock.disableKeyguard()時出現問題。它會解鎖併成功喚醒手機,但當按下主頁按鈕時手機會凍結。我知道我需要在某個時間點重新啓用鍵盤鎖,但是在哪裏?什麼可能是解決方案或有任何其他方式來解鎖手機編程?

回答

0

如果有人遇到同樣的問題,我解決它通過創建活動,並在onCreate方法我說:

//StartStopActivity 
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | 
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | 
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON); 

在我的服務,我開始喜歡這個活動:

//StartStopService 
Intent dialogIntent = new Intent(this, StartStopActivity.class); 
       dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(dialogIntent); 

舊方法不適用於棉花糖,並已棄用。