am trying to build android long-term service/intentService application.
after user open the application it has only one activity with single EditText to allow user input Authentication login code
after user input that the application should running subclass of WakefulBroadcastReceiver, and this subclass having alarm manager to running an intentService every 10mins
i am implemented this example
but after one day
the application doesn't back to send or receive message from the server
is there any practice can help to make application running the whole time
Android stops normal service after some time(due to memory request)
You can Override this method in your service class to start the service again after it is stopped.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
Also have a look at this question.
Related
i want to run a script even if the Task is destroyed. That works fine but is it possible to keep this service running, after the user destroyes the app?
I read something about binding the service but this is not working for me.
No. If the user uninstalls the app, all components are destroyed and removed from the operating system.
However, you can make a Service automatically restart after the app is killed (but not uninstalled) by starting through context.startService(Intent), and returning Service.START_STICKY in onStartCommand()
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
I want to run my Android service(real-time notification service) after application installed and force it to work even application closed. This service responsible for listening my real-time notification server, when notification received I create Android notification and maybe run my app, if it is closed. I assume that my way is not correct for this case and there is another way working with real-time notification. I will be grateful if you give some link or a small explanation.
the better approach to listening my real-time notification server, you've to use GCM. here you can start reading about GCM. and if you want to implement it yourself you have to write a service yourself. ex:
public class ListenerService extends Service{
#Override
public void onStartCommand(Intent intent, int flags, int startId){
return START_STICKY; //this will restart your service even though the system kills
}
// you can write other listener service method inside the service
}
but still I recommend you to use GCM or other cloud messaging services instead of writing your own.
Extend the Android Service class in your class which you want to run in background always.Override the following method and return START_STICKY which makes your service to always run in background until you stop it.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//your code here
return START_STICKY;
}
If you need to do any long running task than create a seperate thread for it and use it inside the service because service runs on main thread.
I have a function that reads the content of the Android clipboard every two seconds and communicates all changes with a remote server.
This works fine in the app is opened. But I need to be able to continue to log the clipboard changes after the app has been closed.
So I tried an IntentService but it doesn't appreciate long loops.
How can I run my infinite loop in the background?
I have a function that reads the content of the Android clipboard every two seconds and communicates all changes with a remote server.
Polling? You are doing it VERY wrong. You should use OnPrimaryClipChangedListener instead.
EDIT
communicates all changes with a remote server.
I just realised you are basically doing some bad things there and I am bit sceptical believing your users aware you literaly spying on them? Is your app in Google Play if so what's your app package id?
IntentService should be used to process single "request", from another application component, at time.
Use started service which should be more suitable for problem you describe. Started service runs until you manually stops it or until system gets out of resources and kill whole process.
Use START_STICKY as return from onStartCommand() method to automatically start it again when system kills it.
If you also start it in BroadcastReceiver with ACTION_BOOT_COMPLETED action service will be started after device boot.
Implement a Service and make it sticky by returning START_STICKY in onStartCommand (see Documentation):
#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;
}
Hi Everyone I want to create location tracking application in android which runs in background even after user terminates application by left swipe from recent application. till now i have used intent service started from main activity but it gets killed when i terminate application.
Any other idea to implement this?
I have tried by registering location updates and doing work of service in onLocation changed event. but listener also gets unregister after application exists.
I don't need full code but i need approach to do this.
Have you tried to track in a Service? Without reference to the activity. You have the start the service from the activity for the first time.
Note: A service isn't killed by swiping from recent apps, rather it is killed by the OS when more ram is required. To restart it after its killed you have to return START_STICKY to onStartCommand i.e:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
Also, here you should register your LocationListener and perform whatever action you need.
And as always, remember to add permissions.
I build an application that collects data about the battery. In order to collect this data I need my application to run on the background in order to be able to collect this data.
How can I do it?
You need to change your activities into Service
if you're against using a service for whatever reason you can have it thread off, then get the data when the user calls the application to the front.
you can use the onStart, onPause, onResume functions as well as making the application single instance so when you run it again, it mearly pulls it up from memory (assuming Android doesn't kill it for some reason).
You can use ongoing notification to prevent it from being killed in the background and moveTaskToBack.
But as pentium10 says, the intended way to handle background processes is through a service which gathers the data you are looking for, then when the activity comes back to the front, it gets the data from the service and displays it.
- #Override public int onStartCommand(Intent intent, int flags, int
startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY; }