Android Service学习篇二:Service启动方式之startService

       由上面的生命周期图能够看出,它的生命周期只有三个阶段:onCreate、onStartCommand、onDestroy。
java

       须要注意的有:
android

        1. 若是是调用者直接退出而没有调用stopService的话,那么被启动的Service会一直在后台运行,直至它的stopService方法被调用,或者它本身调用stopSelf方法。
app

        2. 在服务未被建立时,系统会先调用服务的onCreate方法,接着调用onStartCommand方法。若是调用startService方法前服务已经被建立,那么会再次调用onStartCommand方法。而且,无论调用了多少次onStartCommand方法,只须要一次stop即可以将相应的service关闭。
ide

        3. 具体的操做是在onStartCommand方法中执行的。布局

为了便于理解,咱们建立一个工程,命名为ServiceINS:this

package com.example.serviceins;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

/**
 * startService: 服务在同一时间只会被建立一次,能够经过外部调用stopService或自身调用stopSelf来终止服务
 * 
 * 当执行一个已启动的服务,会直接调用onStartCommand方法来执行任务
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	/**
	 * 启动一个服务
	 * 
	 * @param view
	 */
	public void startClick(View view) {
		Intent intent = new Intent(this, MyService.class);
		startService(intent);
	}

	/**
	 * 中止一个服务
	 * 
	 * @param view
	 */
	public void stopClick(View view) {
		Intent intent = new Intent(this, MyService.class);
		stopService(intent);
	}
}

在布局中加入两个按钮:spa

<LinearLayout 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:orientation="vertical"
    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=".MainActivity" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startClick"
        android:text="启动Service" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="stopClick"
        android:text="中止Service" />

</LinearLayout>

实现一个Service:code

package com.example.serviceins;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		System.out.println("MyService-onCreate");
	}

	// 在该方法中实现服务的核心业务
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		for (int i = 0; i < 20; i++) {
			System.out.println("onStartCommand" + i);
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("MyService-onDestroy");
	}
}
  <service
        android:name=".MyService"
        android:enabled="true">
  </service>

下面,咱们经过操做来观察一下LogCat中的输出状况:
orm

首先点击“启动Service”,以下图,前后执行了Service当中的onCreate方法和onStartCommand方法xml

再点击一次“启动Service”,这时,咱们看到再次执行了一次onStartCommand中的方法,但并无再次去调用onCreate方法

而后依次点击“中止Service”和“启动Service”,只调用了一次onDestroy方法就将service关闭掉了,再次开启service的时候又会从新调用onCreate和onStartCommand方法

相关文章
相关标签/搜索