I have searched through the net to find this answer. But no result found. Sorry that I'm a very newbie in Java & Android programming.
I would elaborate on my question more. Let say my Activity started an IntentService and it's running independently in the background. The service is "subscribed" to the callback of ResultReceiver to update the Activity UI. And during the service is executing the long running process, the Activity is destroyed. So what will happen if the service send a reply or progress update to the Activity via the ResultReceiver ?
Cause as I know, the ResultReceiver need to have a reference of the Activity.
In my project I'm required to develop an video clip messaging app. Which when user captured the video, it will pass the data to the service and the service will perform uploading as well as saving some info into db.. while keep posting the progress update to the Activity UI via ResultReceiver.
User is able to quit or kill the Activity as they like. But when they navigate back to the app/Activity, if the upload/download is still ongoing, it need to show the current progress..
Initially I think of Asynctask, but it's also having similar problem as I mentioned. It need a reference of the caller Activity.
Is there anywhere that I can achieve the requirement I mentioned ? Sorry for the long post. Hope someone can enlighten me a bit and it's even better to have some code snippet. Thanks a lot :)
Edit: In short, is there a way to dynamically bind a newly created Activity to a running IntentService so that the service can send the progress update to the correct Activity ?
Use a BroadcastReceiver on your Activity and register/unregister it in onResume()
and onPause()
respectively. You can use an IntentFilter
to listen for an Intent
from your IntentService
.
In your IntentService
, create an Action value for the IntentFilter
and send a broadcast with that Action when you want to update:
public static final String ACTION = "com.example.yourapp.action.ACTION_TAG";
private void handleResult() {
if (BuildConfig.DEBUG) {
Log.v(TAG, "handleResult");
}
// Broadcast result
Intent intent = new Intent(ACTION);
intent.putExtra("your_extra", someValue);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
In your Activity
:
private BroadcastReceiver mYourReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "onReceive");
}
Bundle bundle = intent.getExtras();
if (bundle != null) {
// Update with content from bundle
}
}
};
@Override
public void onResume() {
super.onResume();
if (BuildConfig.DEBUG) {
Log.v(TAG, "onResume");
}
// Register broadcast receiver
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourReceiver, new IntentFilter(YourIntentService.ACTION));
}
@Override
public void onPause() {
super.onPause();
if (BuildConfig.DEBUG) {
Log.v(TAG, "onPause");
}
// Unregister broadcast receiver
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mYourReceiver);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With