i am working on my first android application and what it does is that it has an activity which after signing in starts a service that pings user location to the server after a certain time period or whenever user location changes. Now for the communication from the service to the activity I am using BroadcastReceiver.
The service after logging the location to the server broadcasts a message which my activity receives and update the UI accordingly. Now everything is good up till this point but when I delete my activity from the background while my service is still running in the background what happens is that whenever the service logs the location on the server and broadcasts the message it crashes and a message is shown on the screen saying your application has been stopped.
I think that the android broadcast system is expecting my activity to be alive to receive the broadcast and when it doesn't find it then it crashes the service.
What I want is that if my activity is around it will receive the broadcast but if it is not then the broadcast should be ignored. It should not crash my service.
I am following proper steps by registering for the service in onResume() and unregistering in onPause(). Moreover I am registering the service dynamically rather than in the manifest.
you should use bindService(intent) not startservice(intent) for indefinite internal of time.so it would work till your activity is binded to your service when you finish activity then call onUnbind().
onDestroy()in activity call stop service.
check in manifest that your receiver is not registered there.
I was doing a stupid mistake there. I was passing the references of variables defined in my activity to the broadcast service. It worked fine when my activity is still alive but when my activity was killed the service ended up with null pointers which upon updating crashed the service.
Related
I have tried using START_STICKY on the onStartCommand() method of intent services, I have even tried changing the process name of the service on the manifest.xml file. But none works. Whenever I kill the app, the service also gets destroyed. I want to design a service that gets triggered when the user starts the app for the first time and then keeps on running even after user kills the application. Any help will be useful
In service class implement on destroy method which will trigger a broadcast. In that broadcast receiver class implement on receive method. So whenever service destroyed this broadcast receiver class will execute onreceive method. In that onreceive method start the service again.
Your service must be in the foreground to prevent the system from closing it. See the startForeground method in the Service class.
I would like to send the broadcast message back to the activity when the service is stopped by the user or OS. I have tried to add the code in onDestroy of the service while onDestroy have not been called every time.
Can anyone give me suggestion how I can send the message when the service is stopped?
Try using StartSticky and sending a broadcast every time the service start(onStart). So when it is destroyed, the service will try to run again by itself and will send the broadcast. This way you will know when the server have been closed and open again.
P.D: Remember to send the broadcast in the onStart, not in onCreate.(Service Lifecycle.)
My application consists of one activity which creates a service. I want the service to be keep running as long as application is running. I know:
It is not guaranteed as Android system can kill activity in low memory conditions and if activity is in background.
The service can be stopped (and killed) by system.
If I bind service to the activity, the activity would get notification in case service is being stopped or started. However, the service may stop running if activity goes in background (onStop()). Please correct me if I am wrong here.
If I bind to service in onResume() of activity and unbind() in onStop(), it might happen that service stops running when my application goes in background. If I bind in onCreate() and unbind() in onDestroy() of activity, would it mean that my activity will keep getting notification from service even when in background.
What is the best way to keep service running and get notification from service to Activity as long as application is running. Please note that there is just one activity in the application so sending activity in background means application goes in background.
Thanks
true
true, but its more rare if us use startForeground()
The service usually won't stop until all activities have unbound. But when the last has, it will. So u can prevent the service from dieing when going to background, if you only unbind in onPause if isFinishing() == true.
see 3.
I personally like to set up a Handler in the Activity and send Messages to it from the service.
If you are binding a Service to your Activity. It simply means that you need service to run as long as your activity is running. If you do not need to bind Service with activity or you do not need to update your UI while your Service is running. you must not bind your Service to your Activity. In this case, for different actions done by Service you can notify user using Android Notifications. Like notifying user that xx download has been completed.
It totally depends upon your purpose that you want to achieve from Service.
if you can use IntentService for your application, you can pass data to the service through an Intent. results can be passed back to the Activity through a ResultReceiver
If you bind your Service to your unique Activity, you'll have it alive as long as the Activity is not terminated or the service isn't unbound. Just bind it on the onCreate() and let it get unbound when stopping your activity (no need to do anything).
You can create a Listener interface within your service, that you'll implement in the Activity, so you can send those notifications from the Service to the Activity. You'll find suitable example and information about this if Googling.
I have built an application which implements a number of broadcast receivers and registers them within a service based on user settings. The service is bound to an activity which calls some of its methods.
When a broadcast receiver is called it starts the service (or calls onstart of the service if it is already running) and passes it a string telling the service what to do. My problem is when the activity is destroyed (back button) the service is also destroyed which in turn kills the broadcast receivers.
I know I can register the receivers in the manifest which would mean doing a check when they are called to see if the user has selected that option. However one of the receivers critical to the application is 'android.intent.action.HEADSET_PLUG' which can only be registered programatically.
So I guess my question is, is there a way to keep this broadcast receiver active when the service is destroyed?
If not can anyone see a workaround for this issue?
Thanks,
Rob
My problem is when the activity is killed the service is killed which in turn kills the broadcast receivers.
If by "killed" you mean the user terminated your app with a task killer or "Force Stop" in the Settings app, then "killed" is the appropriate verb. However, your whole process is "killed" -- it does not follow the chain of events that you describe here.
If by "killed" you mean the user exited your activity via the BACK button, that is because you elected to bind to the service, rather than start it. If you want the service to continue executing past the lifetime of the activity, you must use startService(), and ensure that there is some path by which the user can indicate that they no longer want this service, so you know when to call stopService().
So I guess my question is, is there a way to keep this broadcast receiver active when the service is killed?
No.
If not can anyone see a workaround for this issue?
Start your service, instead of (or possibly in addition to) binding to the service.
I'm trying to write a Service which sits and waits for SMS messages (using a BroadcastReceiver which the server registers) and will take some action depending on the details of the SMS message. I use an Activity to startService() and stopService() for the Service. After I close the Activity, the Service continues to sit there with its state kept and its BroadcastReceiver waiting, as it should... However, I find that over time, randomly, the Service will restart itself. That is, onCreate() and onStartCommand() will be called even when the Service has already been started. This happens sometimes when my phone sits idle overnight...
Can anyone provide insight on why this is (phone requests resources and kills a service?), what I can do to prevent it, or what I can do to prevent the state of the service from being lost?
Thanks in advance.
I don't think you need to start service from activity at all. Have a broadcast receiver that listens to your SMS messages and starts the service. After work is done, service should call stopSelf();
I'll try Service.startForeground()
Android sometimes (when it's low on resources, mem/cpu) removes running services and once it's high on resources again brings them back. This is why you see that your services has been restarted a couple of times during night.