配色: 字号:
IntentService简介
2017-01-17 | 阅:  转:  |  分享 
  
IntentService简介



在manifest中声明服务

和activity、contentprovider一样,服务也必须要在AndroidManifest文件中进行声明是中的子节点。例如我们下面第一个service的例子ServiceDownloader。



......





命令模式:IntentService

编写自己的Service将继承Android的Service类,或者Service的子类IntentService。触发Service的方式有两种,一种是发送命令,即这次学习的命令模式,一种绑定服务,与服务之间建立双向的通信渠道。命令模式例子为http远程下载文件的服务。

服务ServiceDownloader

/命令模式的服务由client请求服务,服务进行处理,并在完成后关闭服务,client无需关心是否需要结束服务,适合一次性的处理,如本例/

publicclassServiceDownloaderextendsIntentService{

privateHttpClientclient=null;



publicServiceDownloader(){

super("ServiceDownloader");

}

//client通过startService()请求服务时,如果服务没有开启,则首先执行onCreate(),我们在此进行服务的初始化工作,请注意,onCreate()是在主线程中运行。

publicvoidonCreate(){

super.onCreate();

client=newDefaultHttpClient();

}

//如果client发出startService()时,如果服务没有开启,则先开启服务onCreate(),在服务开启后或者如果服务已经开启,将触发onStartCommand(),请注意,这也是在主线程中运行,我们不应用将一些时间长的处理放置此处。一般而言,这里可以根据收到的命令,进行本次服务的初始化处理。原则上,由于是主线程,可进行UI操作,但是好的编程风格,service不处理activity的内容。

publicintonStartCommand(Intentintent,intflags,intstartId){

returnsuper.onStartCommand(intent,flags,startId);

}



//这是必须override的方法,在收到客户端命令,处理完onStartCommand()后执行,注意onHandlerIntent是在后台线程中运行,应将主要的处理内容放置此处

protectedvoidonHandleIntent(Intenti){

/HTTP的例子之前学习过,首先是采用GET的方法获取远程文件。将返回的HTTP存放在responseHandler中,我们写了个私类ByteArrayResponseHandler来处理,检查HTTP的返回值,如果不是200OK,例如3xx-6xx,则说明出现异常,如成功,将获取的内容存放至文件中。/

HttpGetgetMethod=newHttpGet(i.getData().toString());

try{

ResponseHandlerresponseHandler=newByteArrayResponseHandler();

byte[]responseBody=client.execute(getMethod,responseHandler);

Fileoutput=newFile(Environment.getExternalStorageDirectory(),

i.getData().getLastPathSegment());

if(output.exists()){

output.delete();

}

FileOutputStreamfos=newFileOutputStream(output.getPath());

fos.write(responseBody);

fos.close();

}catch(Exceptione){

Log.e(getClass().getName(),"Exception:"+e.toString());

}

}



//如果client发出stopService()请求停止服务,或者服务本身通过stopSelf()要求停止服务,都会触发onDestroy(),onDestroy也是在主线程中运行,在此我们应进行停止服务的工作。如果这是正在主线程执行onStartCommand(),则必须要等onStartCommand()的内容执行完,才依次执行onDestroy()的内容。如果这时后台线程onHandleIntent()正在执行,onDestroy()不会自动将后台线程停止,后台线程继续运行,我们必须在onDestroy()的代码中终结后台线程的运行。例如状态检查,或者本地中直接关闭连接,中断通信

publicvoidonDestroy(){

client.getConnectionManager().shutdown();

super.onDestroy();

}



//检查返回HTTPResponse的返回值,如果是3xx-6xx,不是2xx,则说明出错,例如404,NotFound。

privateclassByteArrayResponseHandlerimplementsResponseHandler{

publicbyte[]handleResponse(HttpResponseresponse)throwsClientProtocolException,IOException{

StatusLinestatusLine=response.getStatusLine();

if(statusLine.getStawww.tt951.comtusCode()>=300){

thrownewHttpResponseException(statusLine.getStatusCode(),statusLine.getReasonPhrase());

}

HttpEntityentity=response.getEntity();

if(entity==null)

returnnull;

returnEntityUtils.toByteArray(entity);

}

}

}

命令模式服务的客户端

/客户端采用命令方式触发服务,由于IntentService在执行完后自动关闭,则只需通过startService()命令触发即可/

publicclassServiceTest1extendsActivity{

……

//调起服务和调起Activity非常相似,都是通过Intent来出传递,通过setData传递参数,在本例是直接http的Uri地址。

privatevoidstartDownloader(){

Intentintent=newIntent(this,ServiceDownloader.class);

intent.setData(Uri.parse("http://commonsware.com/Android/excerpt.pdf"));

startService(intent);

}

//一般而言命令模式的服务,不需要考虑终止服务。此处只做试验用。注意,终止服务是终止整个服务,会触发服务中的onDestroy(),如果队列中还有其他命令等等服务处理,将由onDestroy()中的代码停止。因此影响的是所有正在和等待服务处理,而不单是客户端的请求,此需特别注意!!

privatevoidstopDownloader(){

stopService(newIntent(this,ServiceDownloader.class);

}

}

服务和客户端的通信

在上面的例子中,我们希望服务下载完后,能通知客户端。对于命令模式的服务,可采用Messenger的方式,Messenger可以发送消息给activity的Handler,在线程[学习笔记(三一)]中已学习过。

客户端代码如下

publicclassServiceTest1extendsActivity{

……

privatevoidstartDownloader(){

……

intent=newIntent(this,ServiceDownloader.class);

intent.setData(Uri.parse("http://commonsware.com/Android/excerpt.pdf"));

//activity在调起服务时,即startService()或者bindService()都可以携带Messenger作为Intent的extra传递,这样在服务和client之间可通过Messenger传递

intent.putExtra(ServiceDownloader.EXTRA_MESSAGER,newMessenger(handler));

startService(intent);

}

//Handler通过handlerMessage()接受消息,运行在主线程,用于处理UI等内容。

privateHandlerhandler=newHandler(){

publicvoidhandleMessage(Messagemsg){

Super.handlewww.baiyuewang.netMessage(msg);

buttonStart.setEnabled(true);

buttonStop.setEnabled(false);

switch(msg.arg1){

caseActivity.RESULT_OK:

Toast.makeText(ServiceTest1.this,"Result:OK",Toast.LENGTH_LONG).show();

break;

caseActivity.RESULT_CANCELED:

Toast.makeText(ServiceTest1.this,"Result:Cancel",Toast.LENGTH_LONG).show();

break;

default:

break;

}

}

};

}

服务端代码如下:

//避免出现命名重复,将类的命名空间加在前面

publicstaticfinalStringEXTRA_MESSAGER="com.wei.android.learning.ServiceDownloader.EXTRA_MESSAGER";



protectedvoidonHandleIntent(Intenti){

intresult=Activity.RESULT_CANCELED

//下载文件的处理,成功则,设置result=Activity.RESULT_OK;

……

//步骤1:从Intent的Extras中获取Messenger

Bundleextras=i.getExtras();

if(extras!=null){

Messengermesenger=(Messenger)extras.get(EXTRA_MESSAGER);

//步骤2:使用Message.obtain()获得一个空的Message对象

Messagemsg=Message.obtain();

//步骤3:填充message的信息。

msg.arg1=result;

//步骤4:通过Messenger信使将消息发送出去。

try{

mesenger.send(msg);

}catch(Exceptione){

Log.w(getClass().getName(),"ExceptionMessage:"+e.toString());

}

献花(0)
+1
(本文系thedust79首藏)