开源中国android版代码用了两个广播接收器,一个是静态注册到AndroidManifest.xml中的,一个是动态注册到Main.java这个主activity中的。
对于静态注册的这个Receiver是类BroadCast.java。用来接收“@我、留言、评论、新粉丝”信息,并在接受到这些信息后发通知提醒。鉴于这部分代码不多,直接上代码:
public void onReceive(Context context, Intent intent) { String ACTION_NAME = intent.getAction(); if("net.oschina.app.action.APPWIDGET_UPDATE".equals(ACTION_NAME)) { int atmeCount = intent.getIntExtra("atmeCount", 0);//@我 int msgCount = intent.getIntExtra("msgCount", 0);//留言 int reviewCount = intent.getIntExtra("reviewCount", 0);//评论 int newFansCount = intent.getIntExtra("newFansCount", 0);//新粉丝 int activeCount = atmeCount + reviewCount + msgCount + newFansCount;//信息总数 //动态-总数 if(Main.bv_active != null){ if(activeCount > 0){ Main.bv_active.setText(activeCount+""); Main.bv_active.show(); }else{ Main.bv_active.setText(""); Main.bv_active.hide(); } } //@我 if(Main.bv_atme != null){ if(atmeCount > 0){ Main.bv_atme.setText(atmeCount+""); Main.bv_atme.show(); }else{ Main.bv_atme.setText(""); Main.bv_atme.hide(); } } //评论 if(Main.bv_review != null){ if(reviewCount > 0){ Main.bv_review.setText(reviewCount+""); Main.bv_review.show(); }else{ Main.bv_review.setText(""); Main.bv_review.hide(); } } //留言 if(Main.bv_message != null){ if(msgCount > 0){ Main.bv_message.setText(msgCount+""); Main.bv_message.show(); }else{ Main.bv_message.setText(""); Main.bv_message.hide(); } } //通知栏显示 this.notification(context, activeCount); } }
对于动态在Main这个Activity中注册的Receiver是TweetReceiver,用来接收动弹的信息。由于是在Activity内部注册的,因此遵循activity的生命周期,只有在activity启动时才会监听到广播。上代码:
public class TweetReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { int what = intent.getIntExtra("MSG_WHAT", 0); if(what == 1){ Result res = (Result)intent.getSerializableExtra("RESULT"); UIHelper.ToastMessage(context, res.getErrorMessage(), 1000); if(res.OK()){ //发送通知广播 if(res.getNotice() != null){ UIHelper.sendBroadCast(context, res.getNotice()); } //发完动弹后-刷新最新动弹、我的动弹&最新动态(当前界面必须是动弹|动态) if(curTweetCatalog >= 0 && mCurSel == 2) { loadLvTweetData(curTweetCatalog, 0, lvTweetHandler, UIHelper.LISTVIEW_ACTION_REFRESH); } else if(curActiveCatalog == ActiveList.CATALOG_LASTEST && mCurSel == 3) { loadLvActiveData(curActiveCatalog, 0, lvActiveHandler, UIHelper.LISTVIEW_ACTION_REFRESH); } } }else{ final Tweet tweet = (Tweet)intent.getSerializableExtra("TWEET"); final Handler handler = new Handler(){ public void handleMessage(Message msg) { if(msg.what == 1){ Result res = (Result)msg.obj; UIHelper.ToastMessage(context, res.getErrorMessage(), 1000); if(res.OK()){ //发送通知广播 if(res.getNotice() != null){ UIHelper.sendBroadCast(context, res.getNotice()); } //发完动弹后-刷新最新、我的动弹&最新动态 if(curTweetCatalog >= 0 && mCurSel == 2) { loadLvTweetData(curTweetCatalog, 0, lvTweetHandler, UIHelper.LISTVIEW_ACTION_REFRESH); } else if(curActiveCatalog == ActiveList.CATALOG_LASTEST && mCurSel == 3) { loadLvActiveData(curActiveCatalog, 0, lvActiveHandler, UIHelper.LISTVIEW_ACTION_REFRESH); } if(TweetPub.mContext != null){ //清除动弹保存的临时编辑内容 appContext.removeProperty(AppConfig.TEMP_TWEET, AppConfig.TEMP_TWEET_IMAGE); ((Activity)TweetPub.mContext).finish(); } } } else { ((AppException)msg.obj).makeToast(context); if(TweetPub.mContext != null && TweetPub.mMessage != null) TweetPub.mMessage.setVisibility(View.GONE); } } }; Thread thread = new Thread(){ public void run() { Message msg =new Message(); try { Result res = appContext.pubTweet(tweet); msg.what = 1; msg.obj = res; } catch (AppException e) { e.printStackTrace(); msg.what = -1; msg.obj = e; } handler.sendMessage(msg); } }; if(TweetPub.mContext != null) UIHelper.showResendTweetDialog(TweetPub.mContext, thread); else UIHelper.showResendTweetDialog(context, thread); } } }