您的位置:首页 > 移动开发 > Android开发

十二.Android中Service的初步介绍

2012-02-11 11:11 246 查看
一.Service介绍
1. Service在android是系统中四大组件之一。Service的级别和Activity的级别一样。都是在同一个线程中执行。但Service没有界面,只能在后台运行。

2. Service有两种启动方式,一种是使用context.startService()方法,另一种是使用context.bindService()方法。

3. 使用context.startService()方式启动Service会经历如下的过程。

启动过程:context.startService()àonCreate()àonStartCommand ()àService running

在启动过程中,如果Service没有运行onCreat()方法只在第一次启动Service时调用一次。如果Service已经运行,onCreate()方法将不会被调用,而直接调用onStartCommand ()方法。在Service启动后onStartCommand ()可以被多次调用。

结束过程:context.stopService()àonDestroy()àService Stop

4. 使用context.bindService()启动Service会经历如下过程。

启动过程:context.bindService()àonCreate()àonBind()àService running

onBind()方法只可以绑定一次。

结束过程:onUnbind()àonDestroy()àService Stop

5. context.startService()启动的服务如果不调用context.stopService()方法来结束服务而直接关闭程序,则该服务会一直在后台运行。可以再次运行程序来将其关闭。所以只有当调用stopService()后服务才会真正的被关闭,否则服务将一直在后台运行。调用者和startService()启动的服务之间没有联系

使用bindService()方法来启动服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了。调用者和服务之间是绑定关系

6. Service中还提供了一个IntentService来进行异步请求的处理

IntentService是Service的子类,用来处理处理异步的请求。它会在Service中单独创建一个线程用来处理数据,但每次只会创建一个线程并在工作列队中提取一个Intent对象到该线程中进行处理,当执行完成以后会自动停止Service。(IntentService会从工作列队中逐个提取Intent对象进行处理,而不是像在Service中增加线程一样进行同步处理

二.使用startService()创建一个Service

1. 创建一个Activity,添加两个按钮并创建一个监听器,把监听器绑定到按钮上。

//创建一个监听器

private OnClickListener listener = new OnClickListener() {

public void onClick(View v) {

//创建一个intent对象。并指定创建的service类

Intent intent = new Intent(ServiceDemoActivity.this, ExampleService.class);

//intent.setClass(ServiceDemoActivity.this, ExampleService.class);

//根据ID判断点击的那个按钮

switch (v.getId()) {

case R.id.startServerBtn:

//启动Service

startService(intent);

break;

case R.id.stopServiceBtn:

//停止Service

stopService(intent);

break;

default:

break;

}


2. 创建一个类ServiceDemoActivity并继承android的Service

package paj.MainService;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;

public class ExampleService extends Service {

//定义个TAG常量用于在logcat中查看调试信息

private static final String TAG = "ExampleService";

//在Service初次运行时调用一次

@Override

public void onCreate() {

// TODO Auto-generated method stub

//打印测试语句

Log.i(TAG, "ExampleService ---> onCreate()");

super.onCreate();

}
//当调用stopService()关闭Service时调用

@Override

public void onDestroy() {

// TODO Auto-generated method stub

//打印测试语句

Log.i(TAG, "ExampleService ---> onDestroy()");

super.onDestroy();

}
//onStartCommand()可以被多次运行。如果Service已经被运行,将跳过onCreate()方法直接运行onStartCommand()

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stub

//打印测试语句

Log.i(TAG, "ExampleService ---> onStartCommand()");

return super.onStartCommand(intent, flags, startId);
}
@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

}


三.使用BandService()启动Service

1. 创建一个binderService类并继承Service。

2. 实现onBind()方法。

onBind()方法需要返回一个binder对象,所以需要先创建一个自定义内部类继承Binder子类,并在自定义类里面创建一个返回binderService方法。

3. 创建自定类的实例,供onBinder()方法调用。

代码如下:

package paj.MainService;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

public class binderService extends Service {

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

//返回返回自定义内部类的实例

return myBinder;

}

MyBinder myBinder = new MyBinder();

//创建一个内部类并实现Binder

public class MyBinder extends Binder{

//创建一个返回binderService的方法

public binderService getBinderService()

{

return binderService.this;

}

}

}
//根据需要创建自己的方法

public void PrintLog(){

Log.i(tag, "binderService--->MyF");

}


4. 创建一个Activity,使用bindService()方法绑定服务。

5. bindService中有的第二个参数为ServiceConnection实例,所以要实现ServiceConnection

6. 代码如下:

//创建BindService绑定监听器

private OnClickListener bindListener = new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case R.id.startBindServerBtn:

//启动Service

Intent intent = new Intent(ServiceDemoActivity.this, binderService.class);

//使用bindService绑定服务

//第二个参数conn为ServiceConnection的实例

bindService(intent, conn,BIND_AUTO_CREATE);

break;

case R.id.stopBindServiceBtn:

//停止Service

unbindService(conn);

break;

default:

break;

}

}

};

//创建ServiceConnection实例并实现其中的方法

private ServiceConnection conn = new ServiceConnection() {

//当与服务连接断开时调用

public void onServiceDisconnected(ComponentName name) {

// TODO Auto-generated method stub

Log.i("TAG", "Disconnected");

}

//当与服务连接时调用

public void onServiceConnected(ComponentName name, IBinder service) {

// TODO Auto-generated method stub

//把传入IBinder类型的service向下转型为自定义的MyBinder

MyBinder myBinder = (MyBinder)service;

binderService binderservice = myBinder.getBinderService();

binderservice.PrintLog();

}

};


四.如何实现IntentService进行异步请求处理

1. 创建一个类并继承IntentService

2. 复写onHandleIntent()方法。

3. 添加一个无参数的构造器,该构造器需要调用父类的有参构造器。

4. 代码如下:

a package paj.MainService;

import android.app.IntentService;

import android.content.Intent;

import android.util.Log;

public class ExampleIntentService  extends IntentService{

//添加一个无参数构造器,并调用有参数的父类构造器

public ExampleIntentService() {

super("ExampleIntentService");

// TODO Auto-generated constructor stub

}

@Override

protected void onHandleIntent(Intent intent) {

try {

//打印服务中的线程ID

Log.i("ExampleIntentService", "ExampleIntentService线程ID--->" + Thread.currentThread().getId() );

Log.i("ExampleIntentService", "文件下载...");

//使线程阻塞5秒,方便观察log输出

Thread.sleep(5000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


5. IntentService使用startService()方法调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: