利用Handler更简单的实现Service与Activity的通信,更新UI

Service与Activity通信主要有两种方法:

1)利用广播Broadcas,特点:消耗大,但是可以通知多个activity。

2)利用Handler快速实现Service和activity的通信。

3)还有前边介绍的接口回掉+Handler。

在这里介绍第二种方法,也就是最简单的方法:

布局文件:

  1. <pre name="code" class="java"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello_world" />  
  12.         />  
  13.       <Button   
  14.           android:id="@+id/bindService_btn"  
  15.           android:layout_width="match_parent"  
  16.           android:layout_height="wrap_content"  
  17.           android:text="启动service"  
  18.           />  
  19.         <TextView  
  20.             android:id="@+id/ceshiText"  
  21.             android:layout_width="match_parent"  
  22.             android:layout_height="wrap_content"  
  23.             />  
  24.       <Button android:id="@+id/unbindButton"   
  25.           android:layout_width="match_parent"  
  26.           android:layout_height="wrap_content"  
  27.           android:text="解绑Service"  
  28.         />  
  29. </LinearLayout>  

  1. //Service类,模仿下载线程  
  1. </pre><pre name="code" class="java">public class DownService extends Service {  
  2.   
  3.     private int i=0;  
  4.     private boolean isStop;  
  5.     public DownService() {  
  6.         System.out.println("实例化downService");  
  7.     }  
  8.      
  9.     public void onCreate() {  
  10.           
  11.         System.out.println("downService->onCreate");  
  12.         new Thread(new DownThread()).start();//启动线程  
  13.         super.onCreate();  
  14.     }  
  15.       
  16.     public int onStartCommand(Intent intent, int flags, int startId) {  
  17.         // TODO Auto-generated method stub  
  18.         return super.onStartCommand(intent, flags, startId);  
  19.     }  
  20.   
  21.     public void onDestroy() {  
  22.         super.onDestroy();  
  23.         isStop=true;  
  24.         System.out.println("downService->onDestroy");  
  25.     }  
  26.     //每个一秒发一个通知  
  27.     class DownThread implements Runnable{  
  28.            
  29.         public void run() {  
  30.             while(!isStop){  
  31.                 i++;  
  32.             try {  
  33.                 Thread.sleep(1000);  
  34.                  Message msg = new Message();   
  35.                   msg.what = i;  
  36.                 MainActivity.handler.sendMessage(msg);   
  37.             } catch (InterruptedException e) {  
  38.                 // TODO Auto-generated catch block  
  39.                 e.printStackTrace();  
  40.             }  
  41.         }  
  42.      }  
  43.    }  
  44.       
  45.     public boolean isStop() {  
  46.         return isStop;  
  47.     }  
  48.     public void setStop(boolean isStop) {  
  49.         this.isStop = isStop;  
  50.     }  
  51.   
  52.     public IBinder onBind(Intent intent) {  
  53.         System.out.println("onBind");  
  54.           
  55.         return echoBinder;  
  56.     }  
  57.    private CountBinder echoBinder = new CountBinder();  
  58.       
  59.     public class CountBinder extends Binder{  
  60.         public DownService getService(){  
  61.             return DownService.this;  
  62.         }  
  63.     }  
  64.    
  65. }  

Activity与Service交互,更新UI。
  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.os.Message;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     Button bindButton,unbindButton;  
  16.     TextView ceshiTextView;  
  17.     public static MyHandler handler;   
  18.     Intent intent;  
  19.     int i=0;  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.         initView();  
  25.         initEvent();  
  26.     }  
  27.       
  28.     private void initView() {  
  29.         handler = new MyHandler();  
  30.         bindButton = (Button) findViewById(R.id.bindService_btn);//注册观察者  
  31.         unbindButton = (Button) findViewById(R.id.unbindButton);//删除观察者  
  32.         ceshiTextView = (TextView) findViewById(R.id.ceshiText);     
  33.                   
  34.     }  
  35.     private void initEvent() {  
  36.         bindButton.setOnClickListener(new OnClickListener() {  
  37.               
  38.             public void onClick(View v) {  
  39.                 intent = new Intent(MainActivity.this, DownService.class);  //描述跳转服务的intent  
  40.                 startService(intent);  
  41.             }  
  42.         });  
  43.         unbindButton.setOnClickListener(new OnClickListener() {  
  44.               
  45.             public void onClick(View v) {  
  46.                 stopService(intent);  
  47.             }  
  48.         });  
  49.     }  
  50.     class MyHandler extends Handler{  
  51.   
  52.         @Override  
  53.         public void handleMessage(Message msg) {  
  54.             if(msg.what!=0){  
  55.                 int i = msg.what;  
  56.                 ceshiTextView.setText("下载了:"+i);  
  57.             }  
  58.               
  59.         }  
  60.           
  61.     }  
  62. }  
代码很简单,相信一看就明白。。。转载请注明出处:http://blog.csdn.net/jycboy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值