service

服务的生命周期

onCreate

onStartCommand

onstart 

onBind

onUnbind

onDestory

 

可以有两种方式来开启service

 

1.startService(intent);

我们可以多次使用startService(intent)开启服务,但是服务只会创建一次,

也就是说如果服务,如果服务没被创建,当我们以startService(intent)开

启服务时,此时系统会帮我们创建我们要开启的服务,也就会走onCreate()方法;

如果已经存在的话,我们以startService(intent)开启服务,系统不会再从新创

建service,所以此时不会走onCreate()方法。同时我们也可以通过stopService(service)的方法来停止一个service,如果此service存在,即未被销毁, 此时系统会调用此service的onDeatory()方法,销毁此service

以startService(intent)方式开启服务,当开启者推出后,服务还是可以再后台继续运行 的,所以服务非常适合做没有界面的长期的后台操作的事情,比如:播放音乐,上传数 据等

 

2.bindService()

服务只能被绑定一次,也就是说当服务一旦被宿主(activity,recivier等能与service发生绑定关系的组件)a绑定,就不能再被其他 宿主绑定,一直 到与a解除绑定关系。 此服务才能被其他宿主绑定。另外当a通过bindService(intent)绑定服务时,当调用过一次bindService()方法后

,并且绑定成功后,a在调用bindService()方法是不起作用的。

并且宿主和服务端的生命周期是相关联的,如果宿主销毁了(调用了onDestory方法),服务也会跟着销毁(调用了onDestory 方法),但是如果服务挂了,宿 主不一定挂,这个容易理解,因为宿主可以通过unBindService()的方法来解除 绑定并销毁service,而服务去没有这种能力。当我们已绑定的方式开启了服务, 如果宿主销毁了(调用了onDestory方法),而没有显示的调用unBindService()解除绑定,系统会抛异常,所以必须要求显示调用unBindService()方法进行解 绑,而且最好是在宿主的onDestodyf方法中调用此方法比较好。而且显示解绑 也只能显示解绑一次,多次显示解绑,也会抛异常。

如果服务被创建了,我们还想调用服务的方法,此时我们需要用到bindservice的方法了


测试代码

MainActivity.java

package com.example.servicedemo;

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

public class MyService extends Service {

	private static final String TAG = "MyService";

	@Override
	public IBinder onBind(Intent intent) {
		Log.e(TAG, "onBind(Intent intent)" + System.currentTimeMillis());
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.e(TAG, "onCreate()" + System.currentTimeMillis());
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		Log.e(TAG, "onStart(Intent intent, int startId)" + System.currentTimeMillis());
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.e(TAG, "onStartCommand(Intent intent, int flags, int startId)" + System.currentTimeMillis());
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.e(TAG, "onDestroy()" + System.currentTimeMillis());
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Log.e(TAG, "onUnbind(Intent intent)" + System.currentTimeMillis());
		return super.onUnbind(intent);
	}

	
	
	

}




MyService.java



package com.example.servicedemo;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

	Intent intent ;
	private ServiceConnection conn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intent = new Intent(this, MyService.class);
		conn =new MyServiceConnection();
	}

	public void startservice(View view) {
		startService(intent);

	}

	public void stopservice(View view) {
		stopService(intent);
	}

	public void bindservice(View view) {
		bindService(intent, conn, BIND_AUTO_CREATE);
	}

	public void unbindservice(View view) {
		unbindService(conn);
	}
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	private class MyServiceConnection implements ServiceConnection{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			
		}
		
	}

}



运行程序:

android 问Service问如果多次start要如何回调_ide


点击startservice按钮

02-07 18:49:55.732: E/MyService(27318): onCreate()1423306195742
02-07 18:49:55.732: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306195742
02-07 18:49:55.732: E/MyService(27318): onStart(Intent intent, int startId)1423306195742


再点击点击startservice按钮

02-07 18:49:55.732: E/MyService(27318): onCreate()1423306195742
02-07 18:49:55.732: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306195742
02-07 18:49:55.732: E/MyService(27318): onStart(Intent intent, int startId)1423306195742
02-07 18:49:56.242: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306196246
02-07 18:49:56.242: E/MyService(27318): onStart(Intent intent, int startId)1423306196246

再点击点击startservice按钮

02-07 18:49:55.732: E/MyService(27318): onCreate()1423306195742
02-07 18:49:55.732: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306195742
02-07 18:49:55.732: E/MyService(27318): onStart(Intent intent, int startId)1423306195742
02-07 18:49:56.242: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306196246
02-07 18:49:56.242: E/MyService(27318): onStart(Intent intent, int startId)1423306196246
02-07 18:49:56.822: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306196833
02-07 18:49:56.822: E/MyService(27318): onStart(Intent intent, int startId)1423306196833

点击stopservice按钮

02-07 18:49:55.732: E/MyService(27318): onCreate()1423306195742
02-07 18:49:55.732: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306195742
02-07 18:49:55.732: E/MyService(27318): onStart(Intent intent, int startId)1423306195742
02-07 18:49:56.242: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306196246
02-07 18:49:56.242: E/MyService(27318): onStart(Intent intent, int startId)1423306196246
02-07 18:49:56.822: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306196833
02-07 18:49:56.822: E/MyService(27318): onStart(Intent intent, int startId)1423306196833
02-07 18:50:01.042: E/MyService(27318): onDestroy()1423306201049


再点击stopservice按钮

无相关log输出


清空log后

点击bindservice按钮

02-07 18:52:36.322: E/MyService(27318): onCreate()1423306356331
02-07 18:52:36.322: E/MyService(27318): onBind(Intent intent)1423306356331

再点击bindservice按钮

无相关log输出


点击unbindservice按钮

02-07 18:52:36.322: E/MyService(27318): onCreate()1423306356331
02-07 18:52:36.322: E/MyService(27318): onBind(Intent intent)1423306356331
02-07 18:52:48.692: E/MyService(27318): onUnbind(Intent intent)1423306368702
02-07 18:52:48.692: E/MyService(27318): onDestroy()1423306368702

再点击unbindservice按钮

 抛异常,程序有可能会停止运行

02-07 18:52:36.322: E/MyService(27318): onCreate()1423306356331
02-07 18:52:36.322: E/MyService(27318): onBind(Intent intent)1423306356331
02-07 18:52:48.692: E/MyService(27318): onUnbind(Intent intent)1423306368702
02-07 18:52:48.692: E/MyService(27318): onDestroy()1423306368702
02-07 18:52:49.882: E/AndroidRuntime(27318): FATAL EXCEPTION: main
02-07 18:52:49.882: E/AndroidRuntime(27318): Process: com.example.servicedemo, PID: 27318
02-07 18:52:49.882: E/AndroidRuntime(27318): java.lang.IllegalStateException: Could not execute method of the activity
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.view.View$1.onClick(View.java:3982)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.view.View.performClick(View.java:4597)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.view.View$PerformClick.run(View.java:18750)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.os.Handler.handleCallback(Handler.java:733)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.os.Handler.dispatchMessage(Handler.java:95)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.os.Looper.loop(Looper.java:136)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.app.ActivityThread.main(ActivityThread.java:5061)
02-07 18:52:49.882: E/AndroidRuntime(27318): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 18:52:49.882: E/AndroidRuntime(27318): at java.lang.reflect.Method.invoke(Method.java:515)
02-07 18:52:49.882: E/AndroidRuntime(27318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
02-07 18:52:49.882: E/AndroidRuntime(27318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
02-07 18:52:49.882: E/AndroidRuntime(27318): at dalvik.system.NativeStart.main(Native Method)
02-07 18:52:49.882: E/AndroidRuntime(27318): Caused by: java.lang.reflect.InvocationTargetException
02-07 18:52:49.882: E/AndroidRuntime(27318): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 18:52:49.882: E/AndroidRuntime(27318): at java.lang.reflect.Method.invoke(Method.java:515)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.view.View$1.onClick(View.java:3977)
02-07 18:52:49.882: E/AndroidRuntime(27318): ... 11 more
02-07 18:52:49.882: E/AndroidRuntime(27318): Caused by: java.lang.IllegalArgumentException: Service not registered: com.example.servicedemo.MainActivity$MyServiceConnection@426e7e30
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:922)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.app.ContextImpl.unbindService(ContextImpl.java:1611)
02-07 18:52:49.882: E/AndroidRuntime(27318): at android.content.ContextWrapper.unbindService(ContextWrapper.java:529)
02-07 18:52:49.882: E/AndroidRuntime(27318): at com.example.servicedemo.MainActivity.unbindservice(MainActivity.java:38)
02-07 18:52:49.882: E/AndroidRuntime(27318): ... 14 more


清空logcat


点击startservice按钮,bindservice按钮,unbindservice按钮,stopservice按钮

startservice按钮

02-07 18:54:19.922: E/MyService(9919): onCreate()1423306459929
02-07 18:54:19.942: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306459950
02-07 18:54:19.942: E/MyService(9919): onStart(Intent intent, int startId)1423306459950

bindservice按钮

02-07 18:54:19.922: E/MyService(9919): onCreate()1423306459929
02-07 18:54:19.942: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306459950
02-07 18:54:19.942: E/MyService(9919): onStart(Intent intent, int startId)1423306459950
02-07 18:54:21.492: E/MyService(9919): onBind(Intent intent)1423306461502

unbindservice按钮

02-07 18:54:19.922: E/MyService(9919): onCreate()1423306459929
02-07 18:54:19.942: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306459950
02-07 18:54:19.942: E/MyService(9919): onStart(Intent intent, int startId)1423306459950
02-07 18:54:21.492: E/MyService(9919): onBind(Intent intent)1423306461502
02-07 18:54:24.932: E/MyService(9919): onUnbind(Intent intent)1423306464943

stopservice按钮

02-07 18:54:19.922: E/MyService(9919): onCreate()1423306459929
02-07 18:54:19.942: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306459950
02-07 18:54:19.942: E/MyService(9919): onStart(Intent intent, int startId)1423306459950
02-07 18:54:21.492: E/MyService(9919): onBind(Intent intent)1423306461502
02-07 18:54:24.932: E/MyService(9919): onUnbind(Intent intent)1423306464943
02-07 18:54:25.642: E/MyService(9919): onDestroy()1423306465648


清空logcat

依次点击startservice按钮,bindservice按钮,stopservice按钮,unbindservice按钮

startservice按钮

02-07 18:55:43.152: E/MyService(9919): onCreate()1423306543161
02-07 18:55:43.152: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306543164
02-07 18:55:43.152: E/MyService(9919): onStart(Intent intent, int startId)1423306543164

bindservice按钮

02-07 18:55:43.152: E/MyService(9919): onCreate()1423306543161
02-07 18:55:43.152: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306543164
02-07 18:55:43.152: E/MyService(9919): onStart(Intent intent, int startId)1423306543164
02-07 18:55:44.082: E/MyService(9919): onBind(Intent intent)1423306544093

stopservice按钮(注意确实是按下了stopservice按钮的)

02-07 18:55:43.152: E/MyService(9919): onCreate()1423306543161
02-07 18:55:43.152: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306543164
02-07 18:55:43.152: E/MyService(9919): onStart(Intent intent, int startId)1423306543164
02-07 18:55:44.082: E/MyService(9919): onBind(Intent intent)1423306544093

unbindservice按钮


02-07 18:55:43.152: E/MyService(9919): onCreate()1423306543161
02-07 18:55:43.152: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306543164
02-07 18:55:43.152: E/MyService(9919): onStart(Intent intent, int startId)1423306543164
02-07 18:55:44.082: E/MyService(9919): onBind(Intent intent)1423306544093
02-07 18:55:50.032: E/MyService(9919): onUnbind(Intent intent)1423306550041
02-07 18:55:50.032: E/MyService(9919): onDestroy()1423306550042

清空logcat

依次点击bindservice按钮,startservice按钮,unbindservice按钮,stopservice按钮

bindservice按钮

02-07 18:56:47.942: E/MyService(9919): onCreate()1423306607951
02-07 18:56:47.942: E/MyService(9919): onBind(Intent intent)1423306607951

startservice按钮

02-07 18:56:47.942: E/MyService(9919): onCreate()1423306607951
02-07 18:56:47.942: E/MyService(9919): onBind(Intent intent)1423306607951
02-07 18:56:48.752: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306608760
02-07 18:56:48.752: E/MyService(9919): onStart(Intent intent, int startId)1423306608760

unbindservice按钮

02-07 18:56:47.942: E/MyService(9919): onCreate()1423306607951
02-07 18:56:47.942: E/MyService(9919): onBind(Intent intent)1423306607951
02-07 18:56:48.752: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306608760
02-07 18:56:48.752: E/MyService(9919): onStart(Intent intent, int startId)1423306608760
02-07 18:56:52.592: E/MyService(9919): onUnbind(Intent intent)1423306612601

stopservice按钮


02-07 18:56:47.942: E/MyService(9919): onCreate()1423306607951
02-07 18:56:47.942: E/MyService(9919): onBind(Intent intent)1423306607951
02-07 18:56:48.752: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306608760
02-07 18:56:48.752: E/MyService(9919): onStart(Intent intent, int startId)1423306608760
02-07 18:56:52.592: E/MyService(9919): onUnbind(Intent intent)1423306612601
02-07 18:56:55.342: E/MyService(9919): onDestroy()1423306615347

清空logcat

依次点击bindservice按钮,startservice按钮,stopservice按钮,unbindservice按钮

bindservice按钮

02-07 18:57:58.852: E/MyService(9919): onCreate()1423306678864
02-07 18:57:58.862: E/MyService(9919): onBind(Intent intent)1423306678867

startservice按钮

02-07 18:57:58.852: E/MyService(9919): onCreate()1423306678864
02-07 18:57:58.862: E/MyService(9919): onBind(Intent intent)1423306678867
02-07 18:57:59.662: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306679668
02-07 18:57:59.662: E/MyService(9919): onStart(Intent intent, int startId)1423306679669

stopservice按钮(注意确实是按下了stopservice按钮的)

02-07 18:57:58.852: E/MyService(9919): onCreate()1423306678864
02-07 18:57:58.862: E/MyService(9919): onBind(Intent intent)1423306678867
02-07 18:57:59.662: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306679668
02-07 18:57:59.662: E/MyService(9919): onStart(Intent intent, int startId)1423306679669

unbindservice按钮



02-07 18:57:58.852: E/MyService(9919): onCreate()1423306678864
02-07 18:57:58.862: E/MyService(9919): onBind(Intent intent)1423306678867
02-07 18:57:59.662: E/MyService(9919): onStartCommand(Intent intent, int flags, int startId)1423306679668
02-07 18:57:59.662: E/MyService(9919): onStart(Intent intent, int startId)1423306679669
02-07 18:58:03.112: E/MyService(9919): onUnbind(Intent intent)1423306683118
02-07 18:58:03.112: E/MyService(9919): onDestroy()1423306683119

清空logcat,

点击startservice,然后按后退键退出应用

02-07 18:47:53.652: E/MyService(27318): onCreate()1423306073659
02-07 18:47:53.652: E/MyService(27318): onStartCommand(Intent intent, int flags, int startId)1423306073659
02-07 18:47:53.652: E/MyService(27318): onStart(Intent intent, int startId)1423306073660

清空logcat,

点击bindservice按钮,然后按后退键退出应用(说明service的生命周期受宿主约束)

02-07 18:37:08.872: E/MyService(27318): onCreate()1423305428876
02-07 18:37:08.872: E/MyService(27318): onBind(Intent intent)1423305428876
02-07 18:37:14.692: E/MyService(27318): onUnbind(Intent intent)1423305434699
02-07 18:37:14.692: E/MyService(27318): onDestroy()1423305434699
02-07 18:37:14.692: E/ActivityThread(27318): Activity com.example.servicedemo.MainActivity has leaked ServiceConnection com.example.servicedemo.MainActivity$MyServiceConnection@426b9a58 that was originally bound here
02-07 18:37:14.692: E/ActivityThread(27318): android.app.ServiceConnectionLeaked: Activity com.example.servicedemo.MainActivity has leaked ServiceConnection com.example.servicedemo.MainActivity$MyServiceConnection@426b9a58 that was originally bound here
02-07 18:37:14.692: E/ActivityThread(27318): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:970)
02-07 18:37:14.692: E/ActivityThread(27318): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:864)
02-07 18:37:14.692: E/ActivityThread(27318): at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1577)
02-07 18:37:14.692: E/ActivityThread(27318): at android.app.ContextImpl.bindService(ContextImpl.java:1560)
02-07 18:37:14.692: E/ActivityThread(27318): at android.content.ContextWrapper.bindService(ContextWrapper.java:517)
02-07 18:37:14.692: E/ActivityThread(27318): at com.example.servicedemo.MainActivity.bindservice(MainActivity.java:34)
02-07 18:37:14.692: E/ActivityThread(27318): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 18:37:14.692: E/ActivityThread(27318): at java.lang.reflect.Method.invoke(Method.java:515)
02-07 18:37:14.692: E/ActivityThread(27318): at android.view.View$1.onClick(View.java:3977)
02-07 18:37:14.692: E/ActivityThread(27318): at android.view.View.performClick(View.java:4597)
02-07 18:37:14.692: E/ActivityThread(27318): at android.view.View$PerformClick.run(View.java:18750)
02-07 18:37:14.692: E/ActivityThread(27318): at android.os.Handler.handleCallback(Handler.java:733)
02-07 18:37:14.692: E/ActivityThread(27318): at android.os.Handler.dispatchMessage(Handler.java:95)
02-07 18:37:14.692: E/ActivityThread(27318): at android.os.Looper.loop(Looper.java:136)
02-07 18:37:14.692: E/ActivityThread(27318): at android.app.ActivityThread.main(ActivityThread.java:5061)
02-07 18:37:14.692: E/ActivityThread(27318): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 18:37:14.692: E/ActivityThread(27318): at java.lang.reflect.Method.invoke(Method.java:515)
02-07 18:37:14.692: E/ActivityThread(27318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
02-07 18:37:14.692: E/ActivityThread(27318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
02-07 18:37:14.692: E/ActivityThread(27318): at dalvik.system.NativeStart.main(Native Method)