A service is a component of the Android system that is about the same level as the activity, but he can't run it on his own, it can only run in the background and interact with other components.
During the Android development process, every time StartService (Intent) is invoked, the Onstartcommand (Intent,int,int) method of the service object is invoked, Then do some processing in the Onstartcommand method.
From the official Android documentation, we know that Onstartcommand has 4 int return values, starting with simply talking about the role of the int return value.
One, Onstartcommand has 4 kinds of return value:
Start_sticky: If the service process is killed, the status of the service remains in the starting state, but the delivery intent object is not retained. The system then attempts to re-create the service, and the Onstartcommand (Intent,int,int) method is definitely invoked after the creation of the services because the service status is started. If no startup commands are passed to the service during this time, then the parameter intent will be null.
Start_not_sticky: "Not sticky". When this return value is used, the service is not automatically restarted if the service is killed abnormally after the Onstartcommand is executed.
Start_redeliver_intent: Retransmission INTENT. With this return value, if the service is killed abnormally after the Onstartcommand is executed, the service is automatically restarted and the intent value is passed in.
Start_sticky_compatibility:start_sticky compatible version, but does not guarantee that the service will be able to reboot after the kill.
Ii. Creating a service that is not killed
1. Rewrite the following method in service, this method has three return values, Start_sticky (or start_sticky_compatibility) is automatically rewritten after service is killed
@Override public
int Onstartcommand (Intent Intent, int flags, int startid)
{return
start_sticky_ compatibility;
Return Super.onstartcommand (Intent, flags, Startid);
}
Or
@Override public
int Onstartcommand (Intent Intent, int flags, int startid)
{
flags = Start_sticky;
Return Super.onstartcommand (Intent, flags, startid);
return start_redeliver_intent;
}
@Override public
void OnStart (Intent Intent, int startid)
{
//dynamically register broadcast again
Intentfilter Localintentfilter = new Intentfilter ("Android.intent.action.USER_PRESENT");
Localintentfilter.setpriority (Integer.max_value)//shaping maximum
myreceiver searchreceiver = new Myreceiver ();
Registerreceiver (Searchreceiver, localintentfilter);
Super.onstart (Intent, Startid);
}
2. Restart service in the OnDestroy () of the service.
public void OnDestroy ()
{
Intent localintent = new Intent ();
Localintent.setclass (this, myservice.class); Restart Service
This.startservice (localintent) when destroyed;
3. Create a broadcast
The public class Myreceiver extends Broadcastreceiver
{
@Override the public
void OnReceive Intent Intent)
{
context.startservice (new Intent (context, Google.class));
}
4.androidmanifest.xml registered broadcast Myreceiver and MyService services
<receiver android:name= ". Myreceiver" >
<intent-filter android:priority= "2147483647" ><!-- Priority plus highest-->
<!--will call-->
<action android:name= "Android.intent.action.BOOT_COMPLETED" after the system startup completes >
<!--will call-->
<action android:name= "Android.intent.action.USER_PRESENT"/> When the unlock is
complete -Monitor scenario switching-->
<action android:name= "Android.media.RINGER_MODE_CHANGED"/>
</intent-filter>
</receiver>
<service android:name= ". MyService ">
Note: Unlock, start, toggle the scene to activate the broadcast need to add permissions, such as start-up completion, and mobile phone status.
<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission Android:name= "Android.permission.READ_PHONE_STATE"/>
Pro-Test ZTE U795 Mobile Android 4.0.4 version of ADB push to System\app android:persistent= "true"
Become the core process, in 360 kill process, myreceiver still effective, ensure service rebirth. Uh
Kill question:
1. Stop Service in Settings
In the OnDestroy method, the StartService is invoked to restart the service.
Force stop application in 2.settings
Capture system for broadcast (action is Android.intent.action.PACKAGE_RESTARTED)
3. Kill off running task with third party application
Upgrade service priority, program signature, or ADB push to system\app inferior
Compared with the application under/data/app, the application under/system/app enjoys more privileges, for example, if you set the Persistent property to true in its manifest.xml file, you can protect it from out-of-memory killer. such as the Androidmanifest.xml file for application ' Phone ':
<application android:name= "Phoneapp"
android:persistent= true "
android:label=" @string/ Dialericonlabel "
android:icon=" @drawable/ic_launcher_phone "> ...
</application>
The
App is promoted to the system core level after setting.