开发者

Get notified/receive explicit intents when an activity starts

开发者 https://www.devze.com 2022-12-24 17:13 出处:网络
I am developing an application that gets notified when an Activity chosen by a user is started. For this to work, the best approach would be to register a BroadcastReceiver for ACTION_MAIN explicit In

I am developing an application that gets notified when an Activity chosen by a user is started. For this to work, the best approach would be to register a BroadcastReceiver for ACTION_MAIN explicit Intents, which as far as I know doesn't work (because these Intents have specific targets). Another, probably less efficient approach, is to use the system ActivityManager and poll on the getRunningTask() which returns a list of all running tasks at t开发者_运维技巧he moment. The polling can be done by a background service. By monitoring the changes in this list, I can see whether an activity is running or not, so that my application can get notified. The downside is of course the polling. I have not tried this yet, but I think that this last approach will probably work.

Does anyone know of a better approach(es) or suggestions which are less intensive?


Why can't you just call getParent()?

This would be in your child 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); 
}

This would be in your parent Activity

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

精彩评论

暂无评论...
验证码 换一张
取 消