Android BroadcastReceiver or simple callback method?

前端 未结 5 1413
情深已故
情深已故 2021-01-29 20:09

In my projects I am using BroadcastReceivers as a callback from a long running thread (eg. notify the activity that a download was finished and send some response d

5条回答
  •  迷失自我
    2021-01-29 20:28

    I'll just add another option to the other great answers you've received already...

    You don't have to create a broadcast receiver to receive Intents. In your android manifest file you can register any activity to receive intents:

    
            
                   
                   
                 
    ....
    
    

    Then override the onNewIntent(Intent) method in your activity to receive it.

    To send the Intent, use the Context.startActivity(Intent) method. Most likely you'll want to add the FLAG_ACTIVITY_SINGLE_TOP flag to your Intent so it doesn't create a new instance of your activity if one is already running.

    EDIT: I just noticed you are running within a single application. Therefore, a simple callback is probably best. The solution above does work in a single app, but is more appropriate for different applications. I'll leave this here just in case it helps someone. Good luck!

提交回复
热议问题