Take a look at this section: Service Lifecycle Changes (since version 1.6)
updated
after some research (created a project, run the described command step by step, etc.), I found that the code works exactly as described in "Changing the service life cycle"
what i found:
adb shell am kill com.tavianator.servicerestart kills nothing
(try adb shell , then in am kill com.tavianator.servicerestart
You will come across a message Error: Unknown command: kill )
so, run the application,
run adb shell
at the ps command prompt
find the PID number of your application
on the command line command line kill <app_xx_PID>
where is your PID number Repeat killing for maintenance (if it works in its own process)
check if the service is working (should not), restart after 5-7 seconds
update One solution (not good enough, but applicable in some cases) stopSelf() for example:
@Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "onStartCommand", Toast.LENGTH_LONG).show(); stopSelf(); return START_NOT_STICKY; }
update Updated solution
void writeState(int state) { Editor editor = getSharedPreferences("serviceStart", MODE_MULTI_PROCESS) .edit(); editor.clear(); editor.putInt("normalStart", state); editor.commit(); } int getState() { return getApplicationContext().getSharedPreferences("serviceStart", MODE_MULTI_PROCESS).getInt("normalStart", 1); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (getState() == 0) { writeState(1); stopSelf(); } else { writeState(0); Toast.makeText(this, "onStartCommand", Toast.LENGTH_LONG).show(); } return START_NOT_STICKY; }
Why does a service become restricted when a process is killed?
According to this :
When a service starts, it has a life cycle that is independent of the component that launched it, and the service can work in the background indefinitely, even if the component that started it is destroyed. Thus, a service must stop when it is running by calling stopSelf () , or another component can stop its call to stopService () .
Attention It is important that your application terminates its services when it is running, to avoid wasting system resources and consuming battery power. If necessary, other components can stop the service by calling stopService (). Even if you enable service binding, you should always stop the service if it ever received a call to onStartCommand ()
from another document it says:
* START_NOT_STICKY * - If the system kills the service after returning onStartCommand (), do not recreate the service unless waiting for delivery intentions. This is the safest option to avoid starting the service when itβs not necessary, and when your application can simply restart any incomplete tasks.
So, after reading this document and some experiments, I think that the system treats manually killed services as incomplete (crashed: @see W/ActivityManager(306): Scheduling restart of crashed service ) and restarts them, despite the value returned by onStartCommand.
stopSelf () or stopService () - no reboot , why not get the job done?