2014-01-15 161 views
0

我想按HOME_BUTTON鍵設置屏幕。單擊主頁按鈕設置屏幕

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 

    if (keyCode == KeyEvent.KEYCODE_HOME) { 
     System.out.println("home button"); 
     finish(); 
       WindowManager.LayoutParams params = getWindow().getAttributes(); 
       params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON; 
       params.screenBrightness = 0; 
       getWindow().setAttributes(params); 

     return true; 
    } 

    return super.onKeyDown(keyCode, event); 
} 

任何一個可以幫助我 謝謝

+0

那麼你嘗試過什麼追趕Home鍵旁邊? – WarrenFaith

+0

http://stackoverflow.com/questions/9294917/android-java-turn-screen-off – keshav

+0

@khouloudmejdoub您應該將該代碼添加到您的問題,而不是一個不可讀的評論... – WarrenFaith

回答

0

公共布爾的onkeydown(INT的keyCode,KeyEvent的事件){

if (keyCode == KeyEvent.KEYCODE_HOME) { 
    System.out.println("home button"); 
    DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); 
    mDPM.lockNow(); 
    return true; 
} 
return super.onKeyDown(keyCode, event); 

}

這將鎖定屏幕上的新聞的主頁按鈕。

+0

當我添加此代碼此錯誤出現:導致:java.lang.SecurityException:對於策略#3,沒有由uid 10004擁有的活動管理員 –

1

Finaly我得到的解決方案

@Override 
protected void onResume() { 
    super.onResume(); 
    if (PreferenceHelper.getScreenLock(Recognition.this)) { 
     wakeLock.acquire(); 
    } 
    WindowManager.LayoutParams params = getWindow().getAttributes(); 
    params.screenBrightness = 1; 
    getWindow().setAttributes(params); 

}

@Override 
protected void onPause() { 
    super.onPause(); 
    if (PreferenceHelper.getScreenLock(Recognition.this)) { 
     wakeLock.release(); 
    } 
    WindowManager.LayoutParams params = getWindow().getAttributes(); 
    params.screenBrightness = 0; 
    getWindow().setAttributes(params); 

}