1
如何知道Android手機會睡覺? 請幫我一個示例代碼。 感謝您的閱讀。如何瞭解Android手機將要入睡?
如何知道Android手機會睡覺? 請幫我一個示例代碼。 感謝您的閱讀。如何瞭解Android手機將要入睡?
你需要註冊一個BroadcastReceiver爲Intent.ACTION_SCREEN_OFF
因此,創建廣播接收器
這是你處理screen_off意圖。
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(final Context context, final Intent intent) {
/*
* dispatch screen_off
* to handler method
*/
String iAction = intent.getAction();
if (iAction.equals(Intent.ACTION_SCREEN_OFF))
handleScreenAction(iAction);
}
};
現在過濾器註冊接收器。
static void registerReciever() {
IntentFilter myFilter = new IntentFilter();
// Catch screen off event
myFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, myFilter);
}
什麼也沒有發生 你能提供的完整代碼,請這將是一個很大的幫助 – James 2010-09-18 17:47:39
你需要聲明的接收器在AndroidManifest.xml中了。 – 2011-06-08 12:40:02
錯誤。這是清單中的接收方無法聲明的意圖之一。無論如何,你不想同時執行清單和代碼。 – 2011-06-15 14:15:15