Android keep BroadcastReceiver running in background - android

I did a service starts a BroadcastReceiver. The BroadcastReceiver is working only if the app is shown i recent apps screen. When I remove the app from recent apps screen the BroadcastReceiver stops.
How can I save the BroadcastReceiver always in background?

Your service might get killed along with your activity
You should add process=":background" in your manifest file within the <service /> node.
Or, alternatively,
In your service, you must return START_STICKY
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

declare the BroadcastReceiver in its own class, and add it to the Android Manifest, instead of registering it with some context.
This should be a simple starting point for you
Whoops I'm wrong. http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED

Related

android running background service even after killing the application

What I need?
I need something as startForeground() but I dont want to show any icon
What apps similars is?
Whatsapp, facebook etc
you can close their apps but a "service" continue running listening for notifications.
Well in my case I need a service for "Send data" event when app has crashed or has been closed.
#Override onStartCommand:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
to kill it use stopSelf(); , the rest of the service logic is up to you :)
You can do it by launching your service in a different process. In your AndroidManifest.xml add this to your service tag:
android:process="myNewProcess"

Service stops when application is closed

I need a service to run in background and calculate every minute distance between two locations. I used Thread in order to execute a method every minute, then I understood that when the application is closed, the service stops too since application and service use the same thread.
How can i create a simple method that is invoked every 1 minute, in background even when the application is closed?
You can run the Service in a separate process by modifying the manifest:
<service
android:name="com.example.myapplication.MyBackgroundService"
android:exported="false"
android:process=":myBackgroundServiceProcess" >
</service>
But that might not really bring any benefit. And most of the time it may even be a bad idea.
Of course the main thing is that if the Service gets shut down it's then re-started.
The Service's onStartCommand() can return the START_STICKY flag:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Other code goes here...
return START_STICKY;
}
This (and the other) options are explained in the documentation. Basically START_STICKY means "Hey Android! If you really really have to shut down my precious Service because of running low on memory, then please please try to start it again."
Whereas START_NOT_STICKY would mean "Nahh...don't bother. I'll call startService() again myself if I really need my Service running."
This (start sticky) is probably fine most of time. Your Service will just start from scratch again. You could try if that's suitable for your use case.
Then there are "foreground services" which are less likely to get shut down by Android as they are treated more like visible apps. And in fact they show up in the notification drawer with an icon and (if you make it so) a status text. So they are visible to the user like e.g. SportsTracker, Beddit and such apps.
This involves modifying your Service's onStartCommand():
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Tapping the notification will open the specified Activity.
Intent activityIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// This always shows up in the notifications area when this Service is running.
// TODO: String localization
Notification not = new Notification.Builder(this).
setContentTitle(getText(R.string.app_name)).
setContentInfo("Doing stuff in the background...").setSmallIcon(R.mipmap.ic_launcher).
setContentIntent(pendingIntent).build();
startForeground(1, not);
// Other code goes here...
return super.onStartCommand(intent, flags, startId);
}
The Service is started as usual and you can get out of the foreground mode with:
myBackgroundService.stopForeground(true);
The boolean parameters defines if the notification should also be dismissed or not.
You have to use thread for this and set a flag on starting the service. And check that flag for stoping the service.
In addition to the previous solutions provided by our friends...
MAKE SURE YOU CHECK THE "Allow background activity" BUTTON IN YOUR APP'S SETTINGS!
See the picture: In the Battery section

Android Service doesnt restart by itself

In my application I have an activity and a service (extends IntentService ). the service's onStartCommand looks like below
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
My onHandleIntent method:
#Override
protected void onHandleIntent(Intent intent) {
while(continueLoop){ //continueLoop is controlled by the Binder
//Do stuff
}
}
I also bind to the service from activity, so I can break the infinite loop. I started the app and it's service, and then started other applications, after a while my Activity got stopped and destroyed, so is my Service. When I close the other applications using task manager , the service doesn't start by itself.
I waited and then launched my app, as soon as activity is launched service also started. I thought the android system will restart the service automatically when memory is available. Am I missing something or should i wait longer.
Thanks in advance
If you read this IntentService you'll see that
onStartCommand(Intent intent, int flags, int startId)
You should not override this method for your IntentService.
Instead
The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent).
Per the IntentService documentation:
Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
If you are binding to the Service and/or controlling the lifecycle of the service yourself, then you should use a Service and not an IntentService.

Can we create an IntentService as STICKY?

The document says intentservice calls stopself implicitly. So was wondering if intentservice can be made Sticky.
Thanks
sticky is like a property (it´s not a property) that you can give to a service, and it gets activated after the system shuts it down because of low memory, when the system has enough memory it will restart the services that return START_STICKY on their onStartCommand, otherwise the will remain off. In IntentServices you cannot return START_STICKY (at least i haven't found the way) but what you can do is:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
currently i´m using it in my app and it works well

How to restart a killed service automatically?

When a service has been killed, how to restart it automatically?
sometimes without even calling onDestroy()
I inherited an IntentService, so I had to be gentle.
It worked for me when I overrode onStartCommand() but
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
That is, let the parent do what it should and return START_STICKY.
Overrides the onStartCommand() and give START_STICKY or START_REDELIVER_INTENT (depends on your needs) as the return value. The system will then make sure to restart your service until you explicitly stop the service.
http://developer.android.com/reference/android/app/Service.html#START_REDELIVER_INTENT
http://developer.android.com/reference/android/app/Service.html#START_STICKY
If you have your service killed, the system will try to restart it later.
Read more.

Categories

Resources