2011-07-31 194 views
1

在我的應用程序中,我需要捕獲電源按鈕按一個活動,然後顯示另一個活動。我已經設法抓住電源按鈕並在按下時顯示新的活動。 我使用BroadcastReceiver來掛斷電源按下事件。我添加下列標誌到我的活動,以防止屏幕當按下電源按鈕被鎖定:電源按鈕按下 - 保持屏幕

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);//prevent phone from being locked  
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //I am not sure it works.  
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);//turns the screen on 

雖然我能趕上電源按下並顯示沒有被鎖定在活動中,屏幕關閉約1秒鐘,並自動再次打開,這不是我真正想要的。 無論如何,當用戶按下電源按鈕時,我可以保持屏幕?

我不能用dispatchkeyevent趕上權力,但我可以通過這個抓住它:

screenoff = new BroadcastReceiver() 
public static final String Screenoff = "android.intent.action.SCREEN_OFF"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (!intent.getAction().equals(Screenoff)) { 
      //code goes here return; 

     return; 
     } 
    }; 
    IntentFilter offfilter = new IntentFilter (Intent.ACTION_SCREEN_OFF); 
    registerReceiver(screenoff, offfilter); 

的問題是,在屏幕關閉約1秒鐘。 爲什麼會發生這種情況,我該如何解決這個問題?

感謝, 侯賽因

回答

0

您應該使用

boolean  dispatchKeyEvent(KeyEvent event) 
Called to process key events. 

在Activity類捕捉到關鍵事件來代替。

不知道它是否抓住了電源按鈕。

+0

我coud已經通過廣播接收器趕上事件,但屏幕關閉1秒左右,我想要阻止。即使用戶按下電源,是否有任何方法可以保持屏幕亮起?我也嘗試了'getWindow()。addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);'但我也沒有幫助我。 – Hossein

相關問題