Service的生命周期

来源:互联网 发布:网络歌手一亿个伤心dj 编辑:程序博客网 时间:2024/06/08 15:07

1.MyService类

package com.gst.user.application;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MyService extends Service {    private static final String TAG = "MyService";    public MyService() {    }    @Override    public IBinder onBind(Intent intent) {        // TODO: Return the communication channel to the service.        Log.d(TAG,"ServiceLifecycle:onBind");       return new MyBinder();    }    public class MyBinder extends android.os.Binder{    }    @Override    public void onCreate() {        Log.d(TAG,"ServiceLifecycle:onCreate");        super.onCreate();    }    @Override    public void onDestroy() {        Log.d(TAG,"ServiceLifecycle:onDestroy");        super.onDestroy();    }    @Override    public boolean onUnbind(Intent intent) {        Log.d(TAG,"ServiceLifecycle:onUnbind");        return super.onUnbind(intent);    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d(TAG,"ServiceLifecycle:onStartCommand");        return super.onStartCommand(intent, flags, startId);    }}
2.布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.gst.user.application.TempActivity"    tools:showIn="@layout/activity_temp">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceMedium"        android:text="Medium Text"        android:id="@+id/textView3"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="44dp" />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/editText"        android:layout_marginTop="48dp"        android:layout_below="@+id/textView3"        android:layout_centerHorizontal="true" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="开启服务"        android:onClick="onClick_startService"        android:id="@+id/button"        android:layout_marginTop="32dp"        android:layout_below="@+id/editText"        android:layout_alignStart="@+id/button2" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="停止服务"        android:onClick="onClick_stopService"        android:id="@+id/button2"        android:layout_centerVertical="true"        android:layout_centerHorizontal="true" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="绑定服务"        android:onClick="onClick_bindService"        android:id="@+id/button3"        android:layout_marginTop="32dp"        android:layout_below="@+id/button2"        android:layout_alignStart="@+id/button2" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="解除绑定"        android:onClick="onClick_unbindService"        android:id="@+id/button4"        android:layout_marginTop="37dp"        android:layout_below="@+id/button3"        android:layout_alignStart="@+id/button3" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="同步数据"        android:id="@+id/button5"        android:layout_below="@+id/button4"        android:layout_alignStart="@+id/button4"        android:layout_marginTop="22dp" /></RelativeLayout>

3.主程序

package com.gst.user.application;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;public class TempActivity extends AppCompatActivity{    private static final String TAG = "TempActivity";    private Intent intent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_temp);        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);        fab.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)                        .setAction("Action", null).show();            }        });                intent=new Intent(this,MyService.class);    }    private ServiceConnection connection=new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            Log.d(TAG,"ServiceConnection:onServiceConnected");        }        @Override        public void onServiceDisconnected(ComponentName name) {            Log.d(TAG,"ServiceConnection:onServiceDisconnected");        }    };        public void onClick_startService(View view){        startService(intent);    }    public void onClick_stopService(View view){        stopService(intent);    }    public void onClick_bindService(View view){        bindService(intent, connection, BIND_AUTO_CREATE);    }    public void onClick_unbindService(View view){        unbindService(connection);    }}
运行结果:

1.开启服务

01-26 11:32:41.092 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onCreate

01-26 11:32:41.096 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onStartCommand

再次点击

01-26 11:33:46.987 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onStartCommand

2.停止服务

01-26 11:34:09.190 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onDestroy

3.绑定服务

01-26 11:36:23.630 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onCreate

01-26 11:36:23.637 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onBind

再次绑定

无反应

退出程序

01-26 11:38:21.681 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onUnbind

01-26 11:38:21.684 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onDestroy

4.解除绑定

01-26 11:39:17.795 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onUnbind

01-26 11:39:17.805 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onDestroy

5.先开启服务,再绑定服务

01-26 11:40:20.606 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onCreate

01-26 11:40:20.609 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onStartCommand

01-26 11:40:24.637 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onBind

解除绑定

01-26 11:41:29.811 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onUnbind

停止服务

01-26 11:41:51.714 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onDestroy

6.先绑定服务,再开启服务

01-26 11:43:53.100 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onCreate

01-26 11:43:53.103 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onBind

01-26 11:43:56.070 24013-24013/com.gst.user.application D/MyService: ServiceLifecycle:onStartCommand

退出程序

服务未停止

停止服务

服务未停止

解除绑定

01-26 11:48:17.870 2935-2935/com.gst.user.application D/MyService: ServiceLifecycle:onUnbind

01-26 11:48:17.873 2935-2935/com.gst.user.application D/MyService: ServiceLifecycle:onDestroy












0 0