首页 > 代码库 > android Service简介及启动关闭方式

android Service简介及启动关闭方式

(1)Service是Android系统中的四大组件之一,和Activity是同一层次的组件;它是一种生命周期较长,没有可视化界面,运行于后台的一种服务;例如,我们听音乐可以使用Service,下载东西可以使用Service,我们还可以用它来记录我们所在地理位置的改变,或者启动一个服务来一直监听某种动作;我们还可以使用Service更新Content Provider,发送Intent以及启动系统的通知等;

  

(2)Service生命周期不太复杂,它只继承了onCreate(),onStartCommand(),onDestroy()方法;它的启动方式有两种:

  Context.startService() ->onCreate()- >onStartCommand()->Service running--调用Context.stopService() ->onDestroy();

  Context.bindService()->onCreate()->onBind()->Service running--调用>onUnbind() -> onDestroy()

 

  需注意的是:原本的onStart()方法已经deprecated,已经被onStartCommand()替代,但onStartCommand()内部则是先调用了onStart();

  生命周期如图:

 

实例如下:

 

点击startServiceButton时:多点击几次;

 

点击stopServiceButton时;

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/LinearLayout01"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     <Button         android:id="@+id/startButton"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="startServiceButton"        />    <Button         android:id="@+id/stopButton"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="stopServiceButton"        /></LinearLayout>

 

FirstService.java

package com.xiaozhang.androidservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class FirstService extends Service {    @Override    public IBinder onBind(Intent intent) {        System.out.println("Service onBind!");        return null;    }    @Override    public void onCreate() {        super.onCreate();        System.out.println("Service onCreate();");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        System.out.println("startId" + startId);        System.out.println("Service onStartCommand()");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        System.out.println("Service onDestory()");        super.onDestroy();    }}

MainActivity.java

package com.xiaozhang.androidservice;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private Button startButton = null;    private Button stopButton = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        startButton = (Button) findViewById(R.id.startButton);        stopButton = (Button) findViewById(R.id.stopButton);        startButton.setOnClickListener(new StartServiceListener());        stopButton.setOnClickListener(new StopServiceListener());    }    class StartServiceListener implements OnClickListener {        @Override        public void onClick(View v) {            Intent intent = new Intent();            intent.setClass(MainActivity.this, FirstService.class);            startService(intent);        }    }    class StopServiceListener implements OnClickListener {        @Override        public void onClick(View v) {            Intent intent = new Intent();            intent.setClass(MainActivity.this, FirstService.class);            stopService(intent);        }    }}

 

最后还需要把服务给注册到AndroidManifest.xml中:

 <service android:name=".FirstService"></service>

 

 

内容一部分转自:

http://www.cnblogs.com/zhangdongzi/archive/2012/01/08/2316711.html

 

推荐阅读:

http://blog.csdn.net/ithomer/article/details/7364024

android Service简介及启动关闭方式