We have an article about how to enable auto-start for Android, But I have written about how to start the activity. This article will introduce how to start the service.
First, attach the link of the previous article for reference: [Android] boot self-start.
Service startup is more complicated than acitivty. In fact, it is called by other classes, but the activity may be more familiar to everyone and the service is unfamiliar.
If you are not familiar with Android service, refer to the following articles: [Android] service lifecycle and [Android] service learning local service.
Next, let's go back to the question and see how the service is self-started.
Like self-starting an activity, we also need to register a broadcast statically.
Then, create a startup class that inherits from broadcastreveiver.
Context. startservice (new intent ("cn. etzmico. etzmico_autorunservice"); // call the service
Calling/starting a service is much easier than starting an activity. The parameter in intent is a "tag". The following content is described in detail.
Static registration in manifest. xml.
<receiver android:name="cn.etzmico.autorunservice.BootReceiver" > <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver>
Similarly, you need to add permissions.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" > </uses-permission>
Now let's look at the service.
Create a class that inherits from the service. For verification, we can print a sentence in the oncreate () method.
Here we will briefly describe the service lifecycle.
Service also has a lifecycle, and also has an oncrate ...... However, services include onstart () and onbind ().
The difference between oncreate () and onstart () is:
Oncreate () is called only when the service is called for the first time.
Onstart () is called every time a service is called. It is no exception when a service is called for the first time.
Onbind () is called in bindservice () (this article does not cover ).
Therefore, we can write methods in oncreate (), onstart (), and onbind () as needed.
In addition, the Service also needs to be declared.
We all know that when creating an activity, we need. the same is true for the Service declared in XML, but the difference between the service is that each service requires a "tag", which is unique in a project, it is used to let the system know which service we need to call.
<service android:name="cn.etzmico.autorunservice.Etzmico_AutoRunService" > <intent-filter > <action android:name="cn.etzmico.Etzmico_AutoRunService" /> <category android:name="android.intent.category.default" /> </intent-filter> </service>
The content in actiong Android: name of intent-filter is "tag. If we want to start this service, the intent parameter in the startservice () method of the broadcast class is the "tag"; otherwise, the system will prompt that "cn" cannot be found in the new intent when starting the service. etzmico. etzmico_autorunservice "(the specific content in the quotation marks depends on the current situation, that is, the intent parameter when startservice ).
Engineering Resources: http://download.csdn.net/detail/etzmico/4110913