Activity与Service通过广播交换复杂对象数据用法详解

 最近学习新浪微博开放平台,实现了一个应用,通过后台Service监控微博数据,发现数据更新后通知前台程序,并将博客数据列表发送给前台Activity。
       其中利用BroadcastReceiver对象分别在Activity和Service注册了一个广播,通过发送不同的广播控制前台和后台的数据交换,并通过Serializable对象传递复杂的自定义对象类型给Activity。
       程序片段如下:后台监控weibo Service
  1. //继承自Service的子类
复制代码


AndriodFocusMe 主UI界面Activity
  1. public class AndriodFocusMe extends Activity implements Runnable{
  2.         /** Called when the activity is first created. */
  3.     DataReceiver dataReceiver;//BroadcastReceiver对象

  4.    
  5.     ProgressBar progressbar;
  6.    

  7.     ListView listview;
  8.    
  9.     int CMD_STOP_SERVICE = 0;
  10.     int CMD_RESET_SERVICE = 1;
  11.     int CMD_GET_WEIBO_DATA = 2;
  12.    
  13.         @Override
  14.         public void onCreate(Bundle savedInstanceState) {
  15.                 super.onCreate(savedInstanceState);
  16.                 setContentView(R.layout.weibodataview);
  17.                
  18.             Button beginOuathBtn=  (Button) findViewById(R.id.Button_WeiBo);
  19.             Button endBtn = (Button) findViewById(R.id.flashData);
  20.             
  21.             listview = (ListView)findViewById(R.id.weibodatalist);

  22.             progressbar = (ProgressBar)findViewById(R.id.wbprogressbar);
  23.             progressbar.setVisibility(View.INVISIBLE);

  24.             beginOuathBtn.setOnClickListener(new Button.OnClickListener()
  25.         {

  26.             @Override
  27.             public void onClick( View v )
  28.             {            
  29.                 Intent myIntent = new Intent(AndriodFocusMe.this, WeiboService.class);
  30.                 AndriodFocusMe.this.startService(myIntent);//发送Intent启动Service
  31.                 progressbar.setVisibility(View.VISIBLE);
  32.                
  33.                
  34.                

  35.             }
  36.         } );
  37.             
  38.             endBtn.setOnClickListener(new Button.OnClickListener()
  39.         {

  40.             @Override
  41.             public void onClick( View v )
  42.             {            
  43.                      Intent intent = new Intent();//创建Intent对象
  44.                  intent.setAction("weibo4andriod.focusme.weiboService");
  45.                  intent.putExtra("cmd", CMD_GET_WEIBO_DATA);
  46.                  sendBroadcast(intent);//发送广播
  47.                

  48.             }
  49.         } );
  50.    
  51.             
  52.         }
  53.         
  54.     private class DataReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
  55.             
  56.             ArrayList<HashMap<String, String>> weibodatalist;
  57.                 @Override
  58.         public void onReceive(Context context, Intent intent) {//重写onReceive方法
  59.                         
  60.                         try {
  61.                     Bundle bundle = intent.getExtras();
  62.                     //反序列化,在本地重建数据
  63.                     Serializable data = bundle.getSerializable("weibodata");
  64.                     
  65.                     if (data != null) {
  66.                             weibodatalist = (ArrayList<HashMap<String, String>>)data;
  67.                                        
  68.                             SimpleAdapter mSchedule = new SimpleAdapter(AndriodFocusMe.this,weibodatalist,
  69.                                          R.layout.weibodata_itemview,
  70.                                          new String[] {"CreatedAt", "WeiBoText"},
  71.                                          new int[] {R.id.title,R.id.weibotext});
  72.                     
  73.                              listview.setAdapter(mSchedule);

  74.                     } else {
  75.                         return;
  76.                     }
  77.                 } catch (Exception e) {
  78.                     Log.v("test", e.toString());
  79.                 }
  80.                            progressbar.setVisibility(View.GONE);
  81.                
  82.         }               
  83.         }
  84.         @Override
  85.         protected void onStart() {//重写onStart方法
  86.                                 //注册用于接收Service传送的广播
  87.         dataReceiver = new DataReceiver();
  88.         IntentFilter filter = new IntentFilter();//创建IntentFilter对象
  89.         filter.addAction("weiboDataChanged");
  90.         registerReceiver(dataReceiver, filter);//注册Broadcast Receiver
  91.         NotificationManager m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  92.         m_NotificationManager.cancel(R.id.TextView01);
  93.         super.onStart();

  94.         }
  95.         @Override
  96.         protected void onStop() {//重写onStop方法
  97.         unregisterReceiver(dataReceiver);
  98.         finish();
  99.         super.onStop();
  100.         }

  101. }
复制代码

通过这个例子,基本可以掌握Android常用组件的用法,希望能给各位读者带来帮助,并欢迎提出宝贵意见。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值