51服务的开启方式

服务开启方式的知识点见博文:http://blog.csdn.net/zengmingen/article/details/49425161


步骤:

1、新建Android项目名“51服务的开启方式”

2、新建一个类 MyService,继承 Service

3、在清单文件里配置第二步建的service

4、在Myservice类中覆写 onBind,onUnbind,onCreate,onStartCommand,onDestroy,并在这些方法里写上简单的syso

5、编写activity_main.xml布局文件,简单的放置4个按钮,按钮设置onclick方法

6、编写按钮方法


API解释:

bindService(Intent service,ServiceConnection conn, int flags)

作用:绑定服务。将参数service里的服务,通过服务连接ServiceConnection 以参数flags的方式绑定服务

service:设置了要启动的服务,本地或远程服务

conn:该类有两个回调方法

1、public void onServiceConnected(ComponentName name, IBinder service) 

该方法在MyService类的onBind()方法返回一个IBinder 的实例的时候被调用。如果返回的null,则不被调用。

2、public void onServiceDisconnected(ComponentName name)

该方法在连接正常关闭的情况下是不会被调用的, 该方法只在Service 被破坏了或者被杀死的时候调用。例如, 系统资源不足, 要关闭一些Services, 刚好连接绑定的 Service 是被关闭者之一,  这个时候onServiceDisconnected() 就会被调用。

flags:一般选择使用 BIND_AUTO_CREATE,它是Context的常量,自动根据服务是否存在创建服务


代码:


MyService.java

package com.example.serviceway;

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

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("MyService:onBind()");
		return null;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("MyService:onUnbind()");
		return super.onUnbind(intent);
	}
	
	@Override
	public void onCreate() {
		System.out.println("MyService:onCreate()");
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("MyService:onStartCommand()");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		System.out.println("MyService:onDestroy()");
		super.onDestroy();
	}

}


MainActivity.java

package com.example.serviceway;

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

public class MainActivity extends Activity {
	
	private Intent intent;
	
	private ServiceConnection conn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		intent=new Intent();
		intent.setClass(this, MyService.class);
		
		conn=new MyServiceConnection();
	}
	
	public void startService(View v){
		startService(intent);
	}
	
	public void stopService(View v){
		stopService(intent);
	}
	
	public void bindService(View v){
		//BIND_AUTO_CREATE 是上下文里的常量,如果服务不存在,则自动创建
		bindService(intent, conn, BIND_AUTO_CREATE);
	}
	
	public void unBindService(View v){
		unbindService(conn);
	}
	
	class MyServiceConnection implements ServiceConnection{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			//服务连接成功时,该方法调用
			System.out.println("MyServiceConnection:服务连接了!");
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			//服务失去连接时,该方法调用
			System.out.println("MyServiceConnection:服务连接断开了!");
		}
	}

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.serviceway"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.serviceway.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.example.serviceway.MyService"></service>
    </application>

</manifest>


51服务的开启方式_第1张图片

运行结果:


开启服务:

10-26 08:38:26.386: I/System.out(2072): MyService:onCreate()
10-26 08:38:26.386: I/System.out(2072): MyService:onStartCommand()

停止服务:

10-26 08:39:03.076: I/System.out(2072): MyService:onDestroy()

绑定服务:

10-26 08:39:21.436: I/System.out(2072): MyService:onCreate()
10-26 08:39:21.436: I/System.out(2072): MyService:onBind()

解绑服务:

10-26 08:39:48.807: I/System.out(2072): MyService:onUnbind()
10-26 08:39:48.807: I/System.out(2072): MyService:onDestroy()

绑定服务---->返回键退出activity

10-26 08:40:34.818: I/System.out(2072): MyService:onCreate()
10-26 08:40:34.818: I/System.out(2072): MyService:onBind()
10-26 08:40:36.578: I/System.out(2072): MyService:onUnbind()
10-26 08:40:36.578: I/System.out(2072): MyService:onDestroy()

activity销毁,服务销毁

绑定服务---->home键

10-26 08:41:16.698: I/System.out(2072): MyService:onCreate()
10-26 08:41:16.698: I/System.out(2072): MyService:onBind()

activity还在,服务没销毁

开启服务----->返回键退出activity

10-26 08:42:43.020: I/System.out(2072): MyService:onCreate()
10-26 08:42:43.020: I/System.out(2072): MyService:onStartCommand()

activity销毁,服务不销毁

--------------------------------------

开启服务---->绑定服务---->停止服务

10-26 08:44:05.991: I/System.out(2072): MyService:onCreate()
10-26 08:44:06.001: I/System.out(2072): MyService:onStartCommand()
10-26 08:44:07.402: I/System.out(2072): MyService:onBind()

停止服务按钮使劲点,服务也没有停止销毁,why?

因为绑定服务后,停止服务是无效的,需要使用解绑方法,解绑的时候同时销毁服务

开启服务---->绑定服务---->---->停止服务----->解绑服务

10-26 08:55:52.942: I/System.out(2545): MyService:onCreate()
10-26 08:55:52.942: I/System.out(2545): MyService:onStartCommand()
10-26 08:55:53.871: I/System.out(2545): MyService:onBind()
10-26 08:55:56.691: I/System.out(2545): MyService:onUnbind()
10-26 08:55:56.691: I/System.out(2545): MyService:onDestroy()


代码下载,0积分下载

http://download.csdn.net/detail/zengmingen/9214327


你可能感兴趣的:(51服务的开启方式)