2010-03-24 71 views
4

我正在開發一個應用程序,在用戶選擇的Activity啓動時得到通知。爲此,最好的方法是註冊一個BroadcastReceiverACTION_MAIN明確的Intent s,據我所知這是行不通的(因爲這些Intent有特定的目標)。另一種可能效率較低的方法是使用系統ActivityManager並在getRunningTask()上進行輪詢,該操作返回當前所有正在運行的任務的列表。輪詢可以通過後臺服務完成。通過監視此列表中的更改,我可以查看某個活動是否正在運行,以便我的應用程序可以得到通知。不利之處當然是投票。我還沒有嘗試過,但我認爲這最後的方法可能會起作用。活動開始時獲取通知/接收顯式意圖

有誰知道更好的方法或建議是不是密集?

+0

我已經實現了第二種可能性。這一切都取決於您的應用程序多久進行一次輪詢 – Blundell 2011-07-09 22:42:57

回答

0

爲什麼不能只是撥打getParent()

這將是你的孩子Activity

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Intent intent = new Intent(); 

    //Create this method in your Parent Activity 
    getParent().onChildCreated(this, intent); 
} 

這將是你的父母Activity

public void onChildCreated(Activity child, Intent intent) { 
    /* 
    * Have fun (Edited to pass intent) 
    */ 
}