2013-02-21 215 views
2

是否有無論如何檢查onResume是否從在Android中從睡眠狀態喚醒的設備中調用?如何確定設備是否已從睡眠模式內部被喚醒onResume

我之所以需要檢查這是我不希望在從睡眠狀態恢復它來調用特定的方法:

@Override 
public void onResume() { 
    super.onResume(); 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction()) 
     && !SomeCrazyMethodOrPropertyCheckingIfDeviceWakedUpFromSleep) { 
     processIntent(getIntent()); 
    } 
} 

你可能會說「把processintent方法出來的onResume的」。 ..這不是一種選擇,NFC P2P模式要求您在onResume內處理收到的NDEF消息。

回答

2

我會建議壓倒一切的onNewIntent()處理NFC意圖:

@Override 
public void onNewIntent(final Intent intent) { 
    setIntent(intent); 
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { 
    processIntent(intent); 
    } 
} 

processIntent()可以檢查是否意圖已經處理:

private void processIntent(final Intent intent) { 
    if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) { 
// Log.v(TAG, "Ignoring intent; already treated this intent."); 
    return; 
    } 
    intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY); 
    // new intent; process it 
    ... 
} 

可能的,這將解決您的問題。

2

我想你可以嘗試做一些與ACTION_SCREEN_ON

  • 註冊爲它的接收器(你需要它的代碼,它不會在清單工作)。
  • onReceive做這樣的事情:

MY_FLAG_JUST_WAKE_UP = true;

,並在onResume()

if(!MY_FLAG_JUST_WAKE_UP){ 
    doStuff(); 
} 
MY_FLAG_JUST_WAKE_UP = false; 

但是,它需要進行測試,我不知道,如果你永遠在調用onResume()之前收到意圖。