I have made the intent service and its all working fine. I have made the interfaces and made call backs. What call back does , it receives 3 following messages :
- Service Start: When this message arrives, my "Start Service" button changes into "Stop Service" button.
- Service Finished: when this message arrives my button gets disappear
- Error: when this message arrives my button change into the "Error" button.
The call back is working perfectly. now I have following problems noticed when testing. these are as under
Problem:
When I have started the service by clicking the "Start Service" button then the button change into the "Stop Service" button but when I got out of activity, and then came back in , the S"top Service" button change back into "Start Service" button. same like when the "Service Completed" messages comes the button disappear but later on coming back again to activity , there is a button again.
What I want:
I know that what is the problem and that is "Callback" gets called when the event occurred. But how can I save the state of button to get persistent or if there is something like callback to check that if the service is running and get the current status also ?
Please let me know if there is such a way of checking out ? I hope there would be, Please tell me ans share a little example also. It would be great help.
Edit One:
Also I am facing problem in setting the activity as a result receiver. I know how to set it when it is the same class but the problem is I am starting the service in splash activity and trying to set the main activity to get the results but it is not permitting look here
mReceiver = new DownloadResultReceiver(new Handler()); mReceiver.setReceiver(MainActivity.class); //getting error
and
mReceiver = new DownloadResultReceiver(new Handler()); mReceiver.setReceiver(Spalsh.this);// no error
You just need to call the method which will check the status of the service in onResume mthod of your activity like this.
In onResume
Your problem: but when I got out of activity [onBackPressed();], and then came back in [onCreate()]. This resets everything in your activity to default.
Solution: You can as well set a Flag in your service class and check the flag on the onResume or onCreate method of your activity class. For example:
Then in your activity, you can do the following:
This approach is faster than iterating through list of running services on android os, In case your user's phone is running lots of services.
As for the BroadcastReceiver, you need to have a class that extends BroadcastReceiver and implement the onReceive method of the class. For example:
Now you can set your receiver to the instance of this class: