I have a service that is downloading a file. When the download is done, I would like to update my "Downloaded files" list in my Activity
, but only if the Activity
is running. I do not want the Activity
to start if it's not already running.
I was hoping I could do this by making a new Intent
with some special flag.
Anyone have any idea how I can achieve this? A tiny code example maybe?
The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);
An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity() . The Intent describes the activity to start and carries any necessary data.
Intent in = new Intent(getApplicationContext(),SecondaryScreen. class); startActivity(in); This is an explicit intent to start secondscreen activity.
You can create new BroadcastReceiver instance and do something along these lines on your Activity's onResume() method:
registerReceiver(myReceiver, new IntentFilter(DownloadService.ACTION_FILE_DOWNLOADED));
After that, override myReceiver's onReceive() method to call a function that updates the component you want:
@Override
public void onReceive(Context context, Intent intent) {
...
updateViewWithData(service.getNewFileData());
...
}
On your Activity's onPause() method, just unregister the receiver:
unregisterReceiver(myReceiver);
I hope that this would help you, feel free to ask if there is something unclear.
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