0

I am just learning to use Services, and am sort of stuck.

I have an IntentService that I use to download a queue of files. I have an Activity that has the ability to turn what files should be downloaded on and off. After a file is done downloading I update a boolean in my SharedPreferences. I have two questions/problems:

  1. The original Activity that started the Service will most likely be gone, but is very possible that a new instance of that Activity has been started. In that case I need send a message to that Activity to check the SharedPreferences and update it's UI accordingly. Therfore my question is: How can I send a message to an Activity that may or may not be running?

  2. The Activity can be turing on and off what files should be downloaded while the Service is running. This creates a couple situations. If the Service is not running and the file gets turned on, then I want to start it with this file added to it's queue. If the Service is running and the file gets turned on, then I want to simply add the file to it's queue. If the Service is not running and a file gets turned off, I already have a method to delete it. However, if the Service is running and has not downloaded the file I want to remove it from it's queue or if it is currently downloading it, to stop downloading it. I am keeping what files should be downloaded in my SharedPreferences, so I guess all of these could be covered by sending a message to the Service to check the SharedPreferences. This leads to my second question: How can I get if a Service is running, and if it is, send it a message?

1
  • how u resolved your issue ?
    – nida
    Commented Feb 19, 2015 at 14:51

4 Answers 4

3

What you probably want is some kind of bound service. http://developer.android.com/guide/topics/fundamentals/bound-services.html You can send messages to and receive messages from it. So when your activity is started again, it can bind to the service again and receive callbacks when downloads have finished. Plus the activity can send messages to the service to start or stop downloads. The nice part: The service gets started when you bind to it and it's not already running.

Hope this helped :)

1

possible answer to your question 1 and also question 2:

use broadcastReceivers: http://developer.android.com/reference/android/content/BroadcastReceiver.html

for question 1: you can register your activity to some kind of receiver you'll define, and send whenever you'd like a broadcast to it. if the activity is registered to the broadcast - it receive it...

for question 2: same answer: register your service to special broadcast represents your message you want to deliver to the service. if it not running (and not registerd to the broadcast) - then nothing would hepend.

1

Use LocalBroadcastManager to send intents, they will stay in application scope, then use broadcast receiver in activity.

0
0
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager
            .getRunningServices(Integer.MAX_VALUE)) {
        if ("com.pkg.PlayerService".equals(service.service
                .getClassName())) {
            isrunning = true;
            ;
        }
    }
    return isrunning;

running this code gives u status of service.U can put a log in onDestroy() of u r intentService and check when does the your service gets stopped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.