1. 继承service类,重写方法service的 onBinder() 方法

2.在 Manifest中注册 service,设置过滤的url ( public static final String ACTION = "com.lql.service.ServiceDemo"; //自定义)

3. 启动service   startservice(new Intent(ServiceDemo.ACITION));

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

4. 总结:service的生命周期

第一次启动:

04-09 21:46:09.637: I/ServiceDemo(9280): onCreate
04-09 21:46:09.637: I/ServiceDemo(9280): onStartCommand
04-09 21:46:09.637: I/ServiceDemo(9280): onStart

 

第二次启动:

04-09 21:46:49.369: I/ServiceDemo(9413): onStartCommand
04-09 21:46:49.369: I/ServiceDemo(9413): onStart

 

 

 

 

 

ServiceDemo.java

public class ServiceDemo extends Service {
	 private static final String TAG = "ServiceDemo" ;  
	 public static final String ACTION = "com.lql.service.ServiceDemo";  

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Log.i(TAG, "onCreate");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.i(TAG, "onDestroy");
		super.onDestroy();
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onStart");
		super.onStart(intent, startId);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	
     
}

 

StartService.java

public class StartService extends Activity {
  
	Button start_service;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start_service);
		start_service = (Button) findViewById(R.id.start_service);
		start_service.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startService(new Intent(ServiceDemo.ACTION));
			}
		});
	}

	
}

 

 

start_service_activity.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StartService" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/start_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="110dp"
        android:text="开启服务" />

</RelativeLayout>