简体   繁体   中英

How to let Activity know that cyclic Service has finished its task once?

I have some problems working with Android Services. I already have a Service which downloads a file from a server. (The Service checks cyclic for new data) Aftwerwards it parses the file and adds values to an ArrayList wich will be saved to SharedPreferences. In my Activity there are two methods. One will display the values from the ArrayList/SharedPreferences in UI and the second method sets a Notification if needed.

But how do I now when my Service completed its task so the two methods can be started?

Register a BroadcastReceiver in your Activity something like:

myReceiver = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
                 // do my stuff
   }
};
registerReceiver(myReceiver , new IntentFilter("com.myapp.DOWNLOADCOMPLETE"));

Then in your service send the broadcast:

Intent i = new Intent("com.myapp.DOWNLOADCOMPLETE");
sendBroadcast(i);

You can also putExtras on your intent if you need to pass some values:

Documentation BroadcastReceiver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM