2012-02-16 239 views
2

我正在做一個應用程序來鎖定和解鎖屏幕。我正在使用disableKeygaurd和reeanbleKeygaurd來鎖定和解鎖。下面的代碼我用來解鎖:關於屏幕鎖定和解鎖

if(mKeyguardLock == null){ 
    mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); 
    mKeyguardLock = mKeyGuardManager.newKeyguardLock("****"); 
    mKeyguardLock.disableKeyguard(); 
} 

和用於鎖:

if(mKeyguardLock != null){ 
mKeyguardLock.reenableKeyguard(); 
mKeyguardLock = null; 
} 

它工作正常。但問題是,當用戶通過拖動keygaurd意外解鎖屏幕(不使用我的應用程序),那麼我的應用程序將不會鎖定下一次屏幕..我想通過拖動keygaurd使用解鎖時我的應用程序工作。我該怎麼做? 在此先感謝

+0

我得到了這個問題的解決方案.. – asma 2012-02-16 11:44:43

+1

你介意張貼的解決方案,因此我們其他人能看到? – Peter 2012-06-13 04:28:52

回答

0
Try This: 

private KeyguardManager mKeyguardManager; 
private KeyguardManager.KeyguardLock mKeyguardLock; 
private static final String TAG = "ALERTLock"; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    int flags = getFlagsForVersion();   
     getWindow().addFlags(flags); 
     mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 
} 

private int getFlagsForVersion() { 
     final String possibleFlags[] = new String[] { 
      "FLAG_SHOW_WHEN_LOCKED", 
      "FLAG_DISMISS_KEYGUARD", 
      "FLAG_TURN_SCREEN_ON" 
     }; 

     int flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; 
     for(String flag:possibleFlags) { 
      try { 
       Field field = WindowManager.LayoutParams.class.getField(flag); 
       int value = field.getInt(null); 
       flags |= value; 
      } 
      catch(NoSuchFieldException e) { } 
      catch(IllegalAccessException e) { } 
     } 

     return flags; 
    } 

    private synchronized void enableKeyguard() { 
     if (mKeyguardLock != null) { 
      mKeyguardLock.reenableKeyguard(); 
      mKeyguardLock = null; 
     } 
    } 

    private synchronized void disableKeyguard() { 
     if (mKeyguardLock == null) { 
      mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG); 
      mKeyguardLock.disableKeyguard(); 
     } 
    }  
    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     disableKeyguard(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     disableKeyguard(); 
    } 
+0

我應該在哪裏調用enableKeyguard()..? – asma 2012-02-16 09:45:34

+0

你可以使用它在你想要啓用鍵盤鎖的地方例如@Override \t protected void onDestroy(){ \t super.onDestroy(); \t enableKeyguard(); } – 2012-02-16 10:11:49

+0

@Manish:我的問題是,當用戶通過拖動keygaurd意外地解鎖屏幕時,我的應用下次不會鎖定... – asma 2012-02-16 10:23:26