Local services of four major components of Anroid and four major services of anroid
A service is one of the four main components of Android. Like Activity, it represents executable programs. But unlike Activity, the Service does not have an operable user interface, and it is always running in the background. Easy to understand:
If an application needs to present operable information to the user at runtime, select Activity. If not, select Service.
The Service lifecycle is as follows:
The Service is created only once and destroyed only once. So how to create a local service?
The implementation code is as follows:
Package temp.com. androidserivce; import android. app. service; import android. content. intent; import android. OS. IBinder; import android. OS. systemClock; import android. support. annotation. nullable; import android. util. log;/*** Created by Administrator on login /8/18. */public class Myservice extends Service {@ Override public void onCreate () {Log. I ("test", "service created"); super. onCreate () ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {Log. I ("test", "service started"); new Thread (new myRunnable (startId )). start (); return super. onStartCommand (intent, flags, startId) ;}@ Override public void onDestroy () {Log. I ("test", "Service destroyed"); super. onDestroy () ;}@ Nullable @ Override public IBinder onBind (Intent intent) {return null;} class myRunnable implements Runnable {int startId; public myRunnable (int startId) {this. startId = startId ;}@ Override public void run () {for (int I = 0; I <10; I ++) {SystemClock. sleep (1000); Log. I ("test", I + "") ;}// stop the service // stopSelf (); stopSelf (startId); // when the service is stopped without parameters, the services started for the first time will be destroyed. // when the service is stopped with parameters, the last started service will be destroyed ;}}}
To declare a service, you must configure it in manifests.
<manifest ... > ... <application ... > <service android:name=".Myservice" android:exported="true"/>
...
</application>
</manifest>
Android: exported = "true" sets this attribute to indicate that others can use your service.
There is also a small point to note. In Myservice, we can see that I used a subthread To Help Me implement the work at startup, so why didn't I write the for loop code in the onStartCommand method,
It is because the ANR program does not return a response error in onStartCommand. That is, when all your tasks are handed over to the main thread, it will cause the memory overflow of the main thread and it will blow up. In this case, IntentService can also be used to replace the Service.
package temp.com.androidserivce;import android.app.IntentService;import android.content.Intent;import android.os.SystemClock;import android.util.Log;/** * Created by Administrator on 2017/8/18. */public class MyService2 extends IntentService { public MyService2() { super(""); } public MyService2(String name) { super(name); } @Override protected void onHandleIntent(Intent intent) { for (int i = 0; i <10 ; i++) { SystemClock.sleep(1000); Log.i("test",i+""); } }}
It is relatively easy to use. IntentService is a subclass of Service. It uses a worker thread to process all startup requests one by one.