我有一個單一的活動計時器應用程序,其中我已覆蓋onPause()方法來暫停計時器,當用戶按下home或back按鈕時。不過,我希望計時器在用戶手動關閉屏幕的情況下繼續移動,但我知道這也會調用onPause()方法。有什麼辦法可以解決這個問題嗎?Android:使用onPause()選擇性地
0
A
回答
0
我結束了通過檢測和忽略的畫面關閉事件的onPause()方法中這樣做見Android Docs。如何做到這一點的說明可以在這裏找到:http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
具體來說,我用這個代碼的註釋(凱爾提供):
@Override
protected void onCreate() {
// initialize receiver
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
//NEW
PowerManager pm =(PowerManager) getSystemService(Context.POWER_SERVICE);
// your code
}
@Override
protected void onPause() {
// when the screen is about to turn off
// Use the PowerManager to see if the screen is turning off
if (pm.isScreenOn() == false) {
// this is the case when onPause() is called by the system due to the screen turning off
System.out.println(「SCREEN TURNED OFF」);
} else {
// this is when onPause() is called when the screen has not turned off
}
super.onPause();
}
0
您可以覆蓋onBackPressed
以允許您在按下後退按鈕時放置一些額外的邏輯。但是,更好的方法可能是將代碼放在onStop()
中,只有當另一個Activity移動到前臺時纔會調用該代碼。
爲更詳細
相關問題
- 1. onPause android應用
- 2. 要選擇性地使用AWK
- 3. 如何選擇性地使用XmlSerializer的
- 4. Android onPause()或saveInstanceState()?
- 5. Android MediaPlayer onPause()
- 6. Android unregisterReceiver與onPause
- 7. Android - onPause和onResume
- 8. Android - onPause和WidgetLocker
- 9. Android onPause問題
- 10. Android onPause()在onStop()後調用
- 11. 可以使用XmlAttributes來選擇性地使用XmlIgnore嗎?
- 12. 圈地選擇使用PIVOT
- 13. 選擇性地禁用WebControl元素
- 14. 選擇性地禁用.NET Framework版本
- 15. 使用選擇性@Entity
- 16. 使用onResume和onPause
- 17. Android pre onPause通知
- 18. 使用Android IME的選擇
- 19. 選擇在Android中使用
- 20. 使用Google地圖選擇地點
- 21. 使用jQuery遞歸地在選擇器上應用colspan屬性
- 22. 使用谷歌地圖Android API v2或谷歌地圖選擇器的地理位置選擇
- 23. 使用perl來選擇性地更換行
- 24. 選擇性地使用yield_content在Padrino application.haml模板
- 25. 如何選擇性地使用CSS類更改文本?
- 26. 使用CSS選擇性地顯示內容,避免衝突
- 27. 使用awk來一列選擇性地改變語序
- 28. 使用CSS選擇性地隱藏信息
- 29. 使用iText選擇性地隱藏pdf的部分
- 30. 屬性選擇和地點<T>查詢使用LINQ
我不明白這是如何回答我的問題 - 我的問題不與後退按鈕但關閉屏幕。 –
我很抱歉,我應該更清楚。而不是重寫'onPause'並暫停你的定時器,將定時器暫停邏輯移到'onStop'。 – Moshe
當屏幕關閉時,onStop和onPause都會被調用。 –