Android学习笔记(五一):服务Service(上)- IntentService

转载来自:http://blog.csdn.net/flowingflying/article/details/7616333

对于需要长期运行,例如播放音乐、长期和服务器的连接,即使已不是屏幕当前的activity仍需要运行的情况,采用服务方式。服务将通过API触发启动或者通过IPC(Interprocess Communication)连接请求触发启动。服务将一直运行直至被关闭,或者内存不足时由系统关闭。一般而言,为了节省电量,服务应进行优化减少CPU的消耗和大量网络通信。服务可用于以下的场景:

1、用户离开activity后,仍需继续工作,例如从网络下载文件,播放音乐
2、无论activity出现(重现)或离开,都需持续工作,例如网络聊天应用 
3、连接网络服务,正在使用一个远程API提供的服务 
4、定时触发的服务,例如Linux中的cron。

在manifest中声明服务

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

<application … … > 
     ... ...
 
    <service android:name=".ServiceDownloader"> 
</application >

命令模式:IntentService

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

服务ServiceDownloader

/* 命令模式的服务由client请求服务,服务进行处理,并在完成后关闭服务,client无需关心是否需要结束服务,适合一次性的处理,如本例 */
public class ServiceDownloader extends IntentService{  
    private HttpClient client = null;  
     
    public ServiceDownloader(){ 
        super("ServiceDownloader"); 
    }  
    //client通过startService()请求服务时,如果服务没有开启,则首先执行onCreate(),我们在此进行服务的初始化工作,请注意,onCreate()是在主线程中运行。 
    public void onCreate()
 {  
        super.onCreate(); 
        client = new DefaultHttpClient(); 

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

   //这是必须override的方法,在收到客户端命令,处理完onStartCommand()后执行,注意onHandlerIntent是在后台线程中运行,应将主要的处理内容放置此处
   protected void onHandleIntent(Intent i) { 
        /*HTTP的例子之前学习过,首先是采用GET的方法获取远程文件。将返回的HTTP存放在responseHandler中,我们写了个私类ByteArrayResponseHandler来处理,检查HTTP的返回值,如果不是200 OK,例如3xx-6xx,则说明出现异常,如成功,将获取的内容存放至文件中。*/ 
        HttpGet getMethod = new HttpGet(i.getData().toString());
        try{ 
            ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler(); 
            byte[] responseBody = client.execute(getMethod,responseHandler);
            File output = new File(Environment.getExternalStorageDirectory(), 
            i.getData().getLastPathSegment()); 
            if(output.exists()){ 
                output.delete(); 
            } 
            FileOutputStream fos = new FileOutputStream(output.getPath()); 
            fos.write(responseBody); 
            fos.close(); 
        }catch(Exception e){ 
            Log.e(getClass().getName(),"Exception : " + e.toString()); 
        }
 
    } 

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

    //检查返回HTTP Response的返回值,如果是3xx-6xx,不是2xx,则说明出错,例如404,Not Found。
    private class ByteArrayResponseHandler implements ResponseHandler<byte[]>{
        public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            StatusLine statusLine = response.getStatusLine();  
            if(statusLine.getStatusCode() >= 300){  
                throw new HttpResponseException (statusLine.getStatusCode(),statusLine.getReasonPhrase()); 
            } 
            HttpEntity entity = response.getEntity(); 
            if(entity == null) 
                return null; 
            return EntityUtils.toByteArray(entity); 
        }
 
    } 
}

命令模式服务的客户端

/*客户端采用命令方式触发服务,由于IntentService在执行完后自动关闭,则只需通过startService( )命令触发即可 */
public class ServiceTest1 extends Activity{  
    … … 
    //调起服务和调起Activity非常相似,都是通过Intent来出传递,通过setData传递参数,在本例是直接http的Uri地址。 
    private void startDownloader(){ 
        Intent intent = new Intent(this,ServiceDownloader.class); 
        intent.setData
(Uri.parse("http://commonsware.com/Android/excerpt.pdf")); 
        startService(intent); 
    } 
   //一般而言命令模式的服务,不需要考虑终止服务。此处只做试验用。注意,终止服务是终止整个服务,会触发服务中的onDestroy( ),如果队列中还有其他命令等等服务处理,将由onDestroy()中的代码停止。因此影响的是所有正在和等待服务处理,而不单是客户端的请求,此需特别注意!!
    private void stopDownloader(){  
        stopService(new Intent(this,ServiceDownloader.class); 
    }  
}

服务和客户端的通信

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

客户端代码如下

public class ServiceTest1 extends Activity{  
    … … 
    private void startDownloader(){ 
        …… 
        intent = new Intent(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, new Messenger(handler)); 
        startService(intent); 
    }  
    //Handler通过handlerMessage()接受消息,运行在主线程,用于处理UI等内容。 
    private Handler handler = new Handler(){ 
        public void handleMessage(Message msg) {  
           super.handleMessage(msg); 
            buttonStart.setEnabled(true); 
            buttonStop.setEnabled(false); 
            switch(msg.arg1){ 
            case Activity.RESULT_OK: 
                Toast.makeText(ServiceTest1.this, "Result : OK " , Toast.LENGTH_LONG).show();
                break; 
            case Activity.RESULT_CANCELED: 
                Toast.makeText(ServiceTest1.this, "Result : Cancel " , Toast.LENGTH_LONG).show();
                break; 
            default:  
                break; 
            } 
        } 
    }; 
}

服务端代码如下:

    //避免出现命名重复,将类的命名空间加在前面
    public static final String EXTRA_MESSAGER="com.wei.android.learning.ServiceDownloader.EXTRA_MESSAGER";

    protected void onHandleIntent(Intent i) { 
        int result = Activity.RESULT_CANCELED   
       //下载文件的处理,成功则,设置result = Activity.RESULT_OK; 
        … …    
       //步骤1:从Intent的Extras中获取Messenger
       Bundle extras = i.getExtras();
 
        if(extras != null){ 
            Messenger mesenger = (Messenger)extras.get(EXTRA_MESSAGER); 
           //步骤2:使用Message.obtain()获得一个空的Message对象
            Message msg = Message.obtain( );  
           //步骤3:填充message的信息。  
            msg.arg1 = result; 
          //步骤4:通过Messenger信使将消息发送出去。  
            try{ 
                mesenger.send(msg);  
            }catch(Exception e){ 
                Log.w(getClass().getName(),"Exception Message: " + e.toString());
            } 
        }  
    }


 

相关链接: 我的Android开发相关文章

对于需要长期运行,例如播放音乐、长期和服务器的连接,即使已不是屏幕当前的activity仍需要运行的情况,采用服务方式。服务将通过API触发启动或者通过IPC(Interprocess Communication)连接请求触发启动。服务将一直运行直至被关闭,或者内存不足时由系统关闭。一般而言,为了节省电量,服务应进行优化减少CPU的消耗和大量网络通信。服务可用于以下的场景:

1、用户离开activity后,仍需继续工作,例如从网络下载文件,播放音乐
2、无论activity出现(重现)或离开,都需持续工作,例如网络聊天应用 
3、连接网络服务,正在使用一个远程API提供的服务 
4、定时触发的服务,例如Linux中的cron。

在manifest中声明服务

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

<application … … > 
     ... ...
 
    <service android:name=".ServiceDownloader"> 
</application >

命令模式:IntentService

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

服务ServiceDownloader

/* 命令模式的服务由client请求服务,服务进行处理,并在完成后关闭服务,client无需关心是否需要结束服务,适合一次性的处理,如本例 */
public class ServiceDownloader extends IntentService{  
    private HttpClient client = null;  
     
    public ServiceDownloader(){ 
        super("ServiceDownloader"); 
    }  
    //client通过startService()请求服务时,如果服务没有开启,则首先执行onCreate(),我们在此进行服务的初始化工作,请注意,onCreate()是在主线程中运行。 
    public void onCreate()
 {  
        super.onCreate(); 
        client = new DefaultHttpClient(); 

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

   //这是必须override的方法,在收到客户端命令,处理完onStartCommand()后执行,注意onHandlerIntent是在后台线程中运行,应将主要的处理内容放置此处
   protected void onHandleIntent(Intent i) { 
        /*HTTP的例子之前学习过,首先是采用GET的方法获取远程文件。将返回的HTTP存放在responseHandler中,我们写了个私类ByteArrayResponseHandler来处理,检查HTTP的返回值,如果不是200 OK,例如3xx-6xx,则说明出现异常,如成功,将获取的内容存放至文件中。*/ 
        HttpGet getMethod = new HttpGet(i.getData().toString());
        try{ 
            ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler(); 
            byte[] responseBody = client.execute(getMethod,responseHandler);
            File output = new File(Environment.getExternalStorageDirectory(), 
            i.getData().getLastPathSegment()); 
            if(output.exists()){ 
                output.delete(); 
            } 
            FileOutputStream fos = new FileOutputStream(output.getPath()); 
            fos.write(responseBody); 
            fos.close(); 
        }catch(Exception e){ 
            Log.e(getClass().getName(),"Exception : " + e.toString()); 
        }
 
    } 

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

    //检查返回HTTP Response的返回值,如果是3xx-6xx,不是2xx,则说明出错,例如404,Not Found。
    private class ByteArrayResponseHandler implements ResponseHandler<byte[]>{
        public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            StatusLine statusLine = response.getStatusLine();  
            if(statusLine.getStatusCode() >= 300){  
                throw new HttpResponseException (statusLine.getStatusCode(),statusLine.getReasonPhrase()); 
            } 
            HttpEntity entity = response.getEntity(); 
            if(entity == null) 
                return null; 
            return EntityUtils.toByteArray(entity); 
        }
 
    } 
}

命令模式服务的客户端

/*客户端采用命令方式触发服务,由于IntentService在执行完后自动关闭,则只需通过startService( )命令触发即可 */
public class ServiceTest1 extends Activity{  
    … … 
    //调起服务和调起Activity非常相似,都是通过Intent来出传递,通过setData传递参数,在本例是直接http的Uri地址。 
    private void startDownloader(){ 
        Intent intent = new Intent(this,ServiceDownloader.class); 
        intent.setData
(Uri.parse("http://commonsware.com/Android/excerpt.pdf")); 
        startService(intent); 
    } 
   //一般而言命令模式的服务,不需要考虑终止服务。此处只做试验用。注意,终止服务是终止整个服务,会触发服务中的onDestroy( ),如果队列中还有其他命令等等服务处理,将由onDestroy()中的代码停止。因此影响的是所有正在和等待服务处理,而不单是客户端的请求,此需特别注意!!
    private void stopDownloader(){  
        stopService(new Intent(this,ServiceDownloader.class); 
    }  
}

服务和客户端的通信

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

客户端代码如下

public class ServiceTest1 extends Activity{  
    … … 
    private void startDownloader(){ 
        …… 
        intent = new Intent(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, new Messenger(handler)); 
        startService(intent); 
    }  
    //Handler通过handlerMessage()接受消息,运行在主线程,用于处理UI等内容。 
    private Handler handler = new Handler(){ 
        public void handleMessage(Message msg) {  
           super.handleMessage(msg); 
            buttonStart.setEnabled(true); 
            buttonStop.setEnabled(false); 
            switch(msg.arg1){ 
            case Activity.RESULT_OK: 
                Toast.makeText(ServiceTest1.this, "Result : OK " , Toast.LENGTH_LONG).show();
                break; 
            case Activity.RESULT_CANCELED: 
                Toast.makeText(ServiceTest1.this, "Result : Cancel " , Toast.LENGTH_LONG).show();
                break; 
            default:  
                break; 
            } 
        } 
    }; 
}

服务端代码如下:

    //避免出现命名重复,将类的命名空间加在前面
    public static final String EXTRA_MESSAGER="com.wei.android.learning.ServiceDownloader.EXTRA_MESSAGER";

    protected void onHandleIntent(Intent i) { 
        int result = Activity.RESULT_CANCELED   
       //下载文件的处理,成功则,设置result = Activity.RESULT_OK; 
        … …    
       //步骤1:从Intent的Extras中获取Messenger
       Bundle extras = i.getExtras();
 
        if(extras != null){ 
            Messenger mesenger = (Messenger)extras.get(EXTRA_MESSAGER); 
           //步骤2:使用Message.obtain()获得一个空的Message对象
            Message msg = Message.obtain( );  
           //步骤3:填充message的信息。  
            msg.arg1 = result; 
          //步骤4:通过Messenger信使将消息发送出去。  
            try{ 
                mesenger.send(msg);  
            }catch(Exception e){ 
                Log.w(getClass().getName(),"Exception Message: " + e.toString());
            } 
        }  
    }


 

相关链接: 我的Android开发相关文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值