service是运行在后台的服务,你可以启动一个服务Service来播放音乐,或者记录你地理信息位置的改变,或者启动一个服务来运行并一直监听某种动作。

接下来分析一下service 的生命周期:

1:actiivty_main.xml

Android 之Service

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button         android:id="@+id/btn_bind_service"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Bind Service"/>        <Button         android:id="@+id/btn_stop_bind"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/btn_bind_service"        android:text="Stop Bind"/>        <Button         android:id="@+id/btn_start_service"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/btn_stop_bind"        android:text="Start Service"/>        <Button         android:id="@+id/btn_stop_service"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/btn_start_service"        android:text="Stop Service"/></RelativeLayout>

2:ServiceDemo.java

public class ServiceDemo extends Service{    private static final String TAG="ServiceDemo";    public static final String ACTION="com.yan.service.ServiceDemo";        @Override    public IBinder onBind(Intent arg0) {        Log.i(TAG, "onBind");        return null;    }    @Override    public void onCreate() {        Log.i(TAG,"onCreate");        super.onCreate();    }    @Override    @Deprecated    public void onStart(Intent intent, int startId) {        Log.i(TAG, "onStart");        super.onStart(intent, startId);    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i(TAG, "onStartCommand");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        Log.i(TAG, "onDestroy");        super.onDestroy();    }    @Override    public boolean onUnbind(Intent intent) {        Log.i(TAG, "onUnbind");        return super.onUnbind(intent);    }}

3:MainActivity.java

public class MainActivity extends Activity {    private Button btnBindService=null;    private Button btnStopBind=null;    private Button btnStartService=null;    private Button btnStopService=null;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                btnBindService=(Button)findViewById(R.id.btn_bind_service);        btnStopBind=(Button)findViewById(R.id.btn_stop_bind);        btnStartService=(Button)findViewById(R.id.btn_start_service);        btnStopService=(Button)findViewById(R.id.btn_stop_service);                btnBindService.setOnClickListener(new OnClickListener(){            public void onClick(View view){                Intent intent=new Intent(ServiceDemo.ACTION);                bindService(intent,sc,BIND_AUTO_CREATE);            }        });                btnStopBind.setOnClickListener(new OnClickListener(){            public void onClick(View view){                unbindService(sc);            }        });                btnStartService.setOnClickListener(new OnClickListener(){            public void onClick(View view){                Intent intent=new Intent(ServiceDemo.ACTION);                startService(intent);            }        });                        btnStopService.setOnClickListener(new OnClickListener(){            public void onClick(View view){                Intent intent=new Intent(ServiceDemo.ACTION);                stopService(intent);            }        });    }    ServiceConnection sc=new ServiceConnection(){        @Override        public void onServiceConnected(ComponentName arg0, IBinder arg1) {            Log.i("SC", "onServiceConnected");        }        @Override        public void onServiceDisconnected(ComponentName arg0) {            Log.i("SC", "onServiceDisconnected");        }    };}

4:AndroidManifest.xml

<service             android:name="com.yan.service.ServiceDemo">            <intent-filter>                <action android:name="com.yan.service.ServiceDemo"/>            </intent-filter>        </service>

5:运行结果分析。

(1)点击StartService:

执行 onCreate()->onStartCommand()->onStart()

(2)点击StopService:

执行 onDestroy()

(3)点击BindService

执行 onCreate()->onBind()

(4)点击StopBind

执行 onUnbind()->onDestory()

更多相关文章

  1. Android—— Activity生命周期
  2. Android Fragment 生命周期及回调方法
  3. 新书内容连载(2):Android中的Activity的生命周期
  4. Android Service 浅析(生命周期,启动方式,前台Service)
  5. Android——Activity生命周期

随机推荐

  1. Android应用开发——系统自带样式Android
  2. android 中系统自带的主题与样式(theme a
  3. Android(安卓)标签的主题样式
  4. android综合资讯App、自定义悬浮框、屏幕
  5. android 中系统自带的主题与样式(theme a
  6. Android(安卓)imageView图片按比例缩放
  7. 文章标题
  8. Android(安卓)app version code and name
  9. android:inputType 参数详解
  10. android:inputType参数类型说明