I am working on an android application that uses phonegap/cordova. This app uses a javascript to fetch content from the server from time to time and issues a systemtray alert if there is new content.
The problem I was facing was that the app would eventually just quit when android was short on memory or whatsoever.
So I added a service to my manifest (everybody says a service would solve the problem).
<service android:name="UpdateService" />
Verywell, but just having this service there does nothing (obviously). So how do I make my service trigger the main application to keep running?
ADDITION:
I also managed to start the service in my main application
Try registering a PendingIntent with the AlarmManager that will check to make sure your application is running every n milliseconds. If it isn't running have the service start your application again.
Use some code like this to start your application:
Intent serviceIntent = new Intent("com.myapp.LaunchActivity");
startService(serviceIntent);
Replace com.myapp.LaunchActivity with your package name and LaunchActivity.
Related
So if I understand right, the difference between START_STICKY and START_NOT_STICKY is that the first will be restarted by the system, in case it kills it.
Does anyone know whether this also happens when I kill my application using Process.killProcess(Process.myPid())? Or does the system only restart the service if the service was actually being killed externally, not from within the application process?
Not always, you can't depend on START_STICKY for continuing running the service. Better way is to use startForeground for continuing running or if you want to repeat specific task then schedule your service on specific time. Also, manage it when the phone is Restarted as well.
Notification.Builder builder = new Notification.Builder(getBaseContext())
.setContentTitle("");
.setContentText("Your content text");
startForeground(1, builder.build());
Yes, the START_STICKY service is restarted after the application is killed by the Android System.
Until and unless you call stopService() method from an app component or stopSelf() from within the service, the service will be restarted if the app is killed.
But you can always make the service run using startForeground() method, in which you will have to show a notification in the status bar for your service. If you create a service using this method, your service will run at the same priority as an active activity. This means that it is highly unlikely that your service will be stopped, and no restart would be required.
I've developed an android application with a service that runs in the background
the service is called like that
startService(new Intent(getApplicationContext(),myService.class));
but when I remove the app from the recent apps, the service stops and it crashes if I test it..
is there a way to prevent the service from being destroyed when swiping the app from the recent apps?
thank you
You can register a service to run on a repeating cycle using AlarmManager
http://developer.android.com/reference/android/app/AlarmManager.html
The service will run even if the application is not running.
Hello im triying to run a service in background that it doesn't stop when app is destroyed by task manager. The idea of the service is verify every "x" min if there a new insert in a database that i got in a server.
The service is running great even if i close the app but when i use the task manager to destroy my app all the threads are closed too.
So i want to know if its possible to run a thread that ask in background forever unless user cancel it in the app itself, that ignore the destroy caused by task manager so in the future i can use notification bar to tell the user that a new insert happened in the database.
Tryed:
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
askServer(); // i made a timertask that ask every "x" minute
return START_STICKY;
}
As i read START_STICKY should run again the service if it get killed for some reason and i know that this can be done since some app get closed by taskmanager and still get notifications from it as whatsapp,bbms and others. Please tell me if im wrong in anything and thank you for reading!.
UPDATE: Im not trying to break any law or security rule from Android and im not trying to ignore the stoping services option from an app in settings. I want that the service that listen for new incoming "events " inserts in my case keep running after user used the interface that appear when you press home for a while :
UPDATE : sorry for talking to much about this app but is the one that i can use as an example. In whatsapp when i close the app by the interface that i showed above the process and services are killed but after a couple of second they relaunch, this is exactly what i want to do to keep user informed about database events. From setting you still can stop the service without problem or even i can put the option in the app itself to stop notifiying.
Is a bad implementation call in OnDestroy() method an instance of the service so it relaunch after destroy?
UPDATE : welp looks like my service is still running on background after i close the app. I will just have to work on my service design to not waste battery life and resources. Also i was using the log.i() to check if service was running, looks like when main process closes i can't use log or toast just notifications ( still not implemented) because the service is there running just won't show in log .
UPDATE : now is working using using startForeground(0, null). In future i will send a notification to show when a event on database happen building it and calling startForeground(1, notification).
For services, look at Settings -> Applications -> Services. and see if it is running.
However, poorly designed services may run more often or perform syncing operations. So yes it is possible.
I had a problem similar to this when developing my first android game; force-stop was the only way to kill it.
START_NOT_STICKY will kill the background service when you swipe the app away from the task manager. START_STICKY is, as the name implies, "sticky", Meaning it sticks to the android system even when the app is gone.
That's from my experience, anyway.
On my device runs a service in an app (its not my app and I cannot uninstall it and my device is not rooted) that is always draining power. I can stop that service in my devices application settings but is there a way to stop it programatically, so I don't have to do that manually all the time ?
Could maybe something like stopService(...); work ?
Is there maybe a way to change the scheduler to prevent the restarts of the service ?
"W/ActivityManager(178): Scheduling restart of crashed service xxxx in 5000ms
Get the process id of that particular application and then
android.os.Process.killProcess(processIdKillService)
Here is a long shot (I've never tried it):
Create a Context instance for that app using Context.createPackageContext()
Use it to stop the service via Context.stopService()
Hi i make an antivirus and anti theft application on android. I want to relaunch my application immediately after being killed by a task killer. For now, i only use START_STICKY method from service, but it takes a few time to restart my application and service after being killed. My antivirus and anti theft application should be launch immediately because if the owner's mobile phone was lost, an owner still can monitoring where the last location of their mobile phone. Has anyone know how to restart an application on android immediately which is better than START_STICKY method? like a Lookout mobile security?
The best thing you could do is use a service activity, this cannot be killed by task killers if I remember this right :)!
A service is made by making a simple extension like this:
public class MyService extends Service {
And you can start this with this:
startService(new Intent(this, MyService.class));
And stop this with
stopService(new Intent(this, MyService.class));