How to relaunch a application immediately in android after being killed? - java

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));

Related

Is a START_STICKY service restarted after application killed itself

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.

Running Android App in the Background after "killed"

i'm developing an Android App for mobile sensor monitoring (accelerometer, gyroscope etc). The application is build on a class that extends from BroadCastReceiver and has communication with a service (a class that extends from Service) which is responsible of recording the sensor data when some android events occurs. When the app is running in the background works nice until I kill the activity (in this activity the user can adjust some params). I want to continue recording after the app is killed, how to fix it? Thanks!
i think you forgot to add service into your application you need to start service from your activity and then register broadcastreciever with IntentFilters after that your application will never be stop might be you will interested in this answer
You can use both of this framework to schedule your service to start at the time that you want. Like on every hour or so
http://developer.android.com/reference/android/app/job/JobScheduler.html
http://developer.android.com/reference/android/app/AlarmManager.html
but remember that if someone will go to task manager and kill your process there is no possibility to avoid that.
cheers
Solved with return START_STICKY (onstartcommand() of the service) and PowerManager inside the service to prevent the Android CPU from going to sleep. Thanks to all!!

Is it possible to detect exit of an application?

My android application allows to launch other installed applications from this.This shows some allowed apps. If the user try to launch a disallowed application then show a message and go back to my activity (from where each application launch) using running tasks. My application act as a home launcher.So intent to this activity if the is a blocked application.For eg: It is possible to launch Camera from Gallery in Samsung device.If the camera is not an allowed one shows blocked message and exited to my acivity.But when relaunching Gallery the blocked message shows again beause the top activity(Camera activity) lied in the stack.
But the exiting of these blocked application did not work perfectly.
Is it possible to get close/exit event of an application?
How can i finish an application as whole(By finishing all its applications).
How to launch an application without having any history of previous launch?
Thanks in Advance
Is it possible to get close/exit event of an application?
Yes it is possible inside your LauncherActivity You can override onDestroy this method will be called on application exit.
How can i finish an application as whole(By finishing all its applications)?
I believe you want to stop your all running activities here. This can be achieved in multiple ways.
android.os.Process.killProcess(android.os.Process.myPid());
or
Intent intent = new Intent(getApplicationContext(), YourHomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities and will brings the user to the HomeActivity. While calling this you can add a flag in intent and using that value you can finish the HomeActivity also. Use finish() method to finish the activity.
How to launch an application without having any history of previous
launch?
You can use the same above Solution to achieve this. The second one.
Hope this will help.
There is an onTerminate method in application class, but this cannot be used in production environment. See more about this here

How to force an application to stay open?

I have seen apps like Lookout, JuiceDefender, and MagicJack run in the background indefinitely, unless force closed by a user directly through the task manager. (And even then, in Gingerbread, it wouldn't close unless you browsed to the application that was running under "Downloaded Apps" in the settings and force closed it once you were at the menu where you have options to manage the app like "Clear Memory" and "Force Close".
I am wondering how this is accomplished? I need to do something similar for an app of mine but I don't know how to avoid the Android OS's automatic task killing.. And don't say it's not possible because if that were true, JuiceDefender, MagicJack, and Lookout would not work.
What you can have is a service that stays alive indefinitely. You achieve that returning Service.START_STICKY on your Service's onStartCommand method.
Whenever the os needs resources and chooses to kill your app, your service will be respawned as soon as the resources are available again.
Bear in mind that having an application that is continuously alive will result in consuming the phone's battery. You should (at least) notify the user with a notification that your app is still alive in the background.
On top of that, you can register a broadcast receiver for the BOOT_COMPLETED event in order to restart your service while the device gets restarted. Yet, bear in mind that this could result in eating the phone's battery and so be careful on what you are doing in the service.
I believe these apps are launching a Service when their Activity get started (i.e when onCreate() is called).
A Service keeps running when the application get paused. When the Service is launched, you may return START_STICKY in your onStartCommand.
Also, to prevent a Service from being killed by Android's memory killer, you can specify that your Service is important to the user by calling startForeground(). Android Developers website states that :
A foreground service is a service that's considered to be something
the user is actively aware of and thus not a candidate for the system
to kill when low on memory.
I am creating an app and I have to use one or more of the following super functions inside OnCreate():
onDestroy()
onPause()
onResume()
onSaveInstanceState()
to close an app completely from the memory. And also do not use Activity.finish() method. Usually Android does a pretty good job in closing the app when memory is needed, called pop out of stack and not recommended to forcefully stay in memory, unless there is a very very good reason to. Hope it helps.
You can also check the Android DOC website for more information and examples to your request.
You need to start a service. Services runs in background and is useful to push alerts.
This some links about it:
http://developer.android.com/guide/components/services.html
http://www.vogella.com/articles/AndroidServices/article.html
In the service onStartCommand method return "START_STICKY".
http://developer.android.com/guide/components/services.html
/Thomas

How to have an android service keep the phonegap ui alive

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.

Categories