启动和停止Service

activity_main

<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/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/stop"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="44dp"
        android:text="开启服务" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/start"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="163dp"
        android:text="关闭服务" />

</RelativeLayout>

MainActivity

package com.example.android_service;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends Activity {

	private Button stop,start;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        start=(Button)this.findViewById(R.id.start);
        stop=(Button)this.findViewById(R.id.stop);
        final Intent intent=new Intent();
        intent.setAction("dashu.test.service");
        start.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startService(intent);
			}
		});
        stop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				stopService(intent);
			}
		});
    }
}


MyService

package com.example.android_service;

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

public class MyService extends Service {

	private final static String TAG = "dashu";

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

	// Service创建时候回调
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.d(TAG, "-->服务创建");
	}

	//服务启动时候回调
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.d(TAG, "-->服务启动");
		return super.onStartCommand(intent, flags, startId);	
	}

	//服务关闭时候回调
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d(TAG, "-->服务关闭");
	}

}


 

使用context.startService() 启动Service是会会经历:
context.startService()  ->onCreate()- >onStart()->Service running
context.stopService() | ->onDestroy() ->Service stop 
 
假设Service还没有执行,则android先调用onCreate()然后调用onStart();假设Service已经执行,则仅仅调用onStart()。所以一个Service的onStart方法可能会反复调用多次。 
 
stopService的时候直接onDestroy,假设是调用者自己直接退出而没有调用stopService的话,Service会一直在后台执行。该Service的调用者再启动起来后能够通过stopService关闭Service。
 
所以调用startService的生命周期为:onCreate --> onStart(可多次调用) --> onDestroy

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/zhchoutai/p/8424351.html