`
djun100
  • 浏览: 165730 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Android之动态更新通知栏

 
阅读更多

我们在QQ项目中实现了通知栏后台运行,以及来新消息提示,通常在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的UI组件赢得了用户的一致好评,就连苹果也开始模仿了。其实有点类似于Windows的托盘显示。

下面我们就来根据QQ小项目,来具体分析一下。先看下两张效果图:



一、通知栏的布局文件,在我们这个QQ小项目中,当我们在好友列表的Activity按返回键的时候,先作一个程序进入后台运行的标记(可以是全局变量,也可以保存到SharedPreferenced文件中),然后发送一个广播,我们通过在服务里接收这个广播,就马上初始化后台运行的通知栏的view,当新消息到来时,我们就不把消息通过广播发送出去了(因为没有Activity在运行),而是直接通过更新通知栏来提醒用户,同时发送一个通知(带声音、带振动)。下面是我们这个在通知栏的view的布局文件notify_view.xml:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical"
  10. android:padding="2dp">
  11. <RelativeLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content">
  14. <ImageView
  15. android:id="@+id/notify_imageLog"
  16. android:layout_width="40dp"
  17. android:layout_height="40dp"
  18. android:layout_alignParentLeft="true"
  19. android:layout_centerVertical="true"
  20. android:paddingLeft="5dp"
  21. android:src="@drawable/h001"/>
  22. <TextView
  23. android:id="@+id/notify_name"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_centerVertical="true"
  27. android:layout_toRightOf="@+id/notify_imageLog"
  28. android:paddingLeft="5dp"
  29. android:text="name"
  30. android:textColor="#000000"
  31. android:textSize="20sp"/>
  32. </RelativeLayout>
  33. <LinearLayout
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:layout_gravity="center"
  37. android:orientation="horizontal">
  38. <TextView
  39. android:id="@+id/notify_msg"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_weight="1"
  43. android:paddingLeft="15dp"
  44. android:text="msg"
  45. android:textColor="@color/black"
  46. android:textSize="15sp"/>
  47. <TextView
  48. android:id="@+id/notify_time"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_weight="1"
  52. android:gravity="right"
  53. android:paddingRight="15dp"
  54. android:text="time"
  55. android:textColor="@color/black"
  56. android:textSize="15sp"/>
  57. </LinearLayout>
  58. </LinearLayout>
  59. </LinearLayout>



二、初始化通知栏view的方法,在GetMsgService中写一个方法,初始化我们这个通知栏的view:

  1. /**
  2. *创建通知
  3. */
  4. privatevoidsetMsgNotification(){
  5. inticon=R.drawable.notify;
  6. CharSequencetickerText="";
  7. longwhen=System.currentTimeMillis();
  8. mNotification=newNotification(icon,tickerText,when);
  9. //放置在"正在运行"栏目中
  10. mNotification.flags=Notification.FLAG_ONGOING_EVENT;
  11. RemoteViewscontentView=newRemoteViews(mContext.getPackageName(),
  12. R.layout.notify_view);
  13. contentView.setTextViewText(R.id.notify_name,util.getName());
  14. contentView.setTextViewText(R.id.notify_msg,"手机QQ正在后台运行");
  15. contentView.setTextViewText(R.id.notify_time,MyDate.getDate());
  16. //指定个性化视图
  17. mNotification.contentView=contentView;
  18. Intentintent=newIntent(this,FriendListActivity.class);
  19. PendingIntentcontentIntent=PendingIntent.getActivity(mContext,0,
  20. intent,PendingIntent.FLAG_UPDATE_CURRENT);
  21. //指定内容意图
  22. mNotification.contentIntent=contentIntent;
  23. mNotificationManager.notify(Constants.NOTIFY_ID,mNotification);
  24. }


三,好友列表Activity返回按键的广播接收者,用户按返回键发送广播,并做好标记,程序进入后台运行:

  1. //收到用户按返回键发出的广播,就显示通知栏
  2. privateBroadcastReceiverbackKeyReceiver=newBroadcastReceiver(){
  3. @Override
  4. publicvoidonReceive(Contextcontext,Intentintent){
  5. //TODOAuto-generatedmethodstub
  6. Toast.makeText(context,"QQ进入后台运行",0).show();
  7. setMsgNotification();
  8. }
  9. };


四,通过handler更新通知栏,我们是通过handler来处理消息并更新通知栏的:

  1. //用来更新通知栏消息的handler
  2. privateHandlerhandler=newHandler(){
  3. publicvoidhandleMessage(Messagemsg){
  4. switch(msg.what){
  5. caseMSG:
  6. intnewMsgNum=application.getNewMsgNum();//从全局变量中获取
  7. newMsgNum++;//每收到一次消息,自增一次
  8. application.setNewMsgNum(newMsgNum);//再设置为全局变量
  9. TranObject<TextMessage>textObject=(TranObject<TextMessage>)msg
  10. .getData().getSerializable("msg");
  11. //System.out.println(textObject);
  12. if(textObject!=null){
  13. intform=textObject.getFromUser();//消息从哪里来
  14. Stringcontent=textObject.getObject().getMessage();//消息内容
  15. ChatMsgEntityentity=newChatMsgEntity("",
  16. MyDate.getDateEN(),content,-1,true);//收到的消息
  17. messageDB.saveMsg(form,entity);//保存到数据库
  18. //更新通知栏
  19. inticon=R.drawable.notify_newmessage;
  20. CharSequencetickerText=form+":"+content;
  21. longwhen=System.currentTimeMillis();
  22. mNotification=newNotification(icon,tickerText,when);
  23. mNotification.flags=Notification.FLAG_NO_CLEAR;
  24. //设置默认声音
  25. mNotification.defaults|=Notification.DEFAULT_SOUND;
  26. //设定震动(需加VIBRATE权限)
  27. mNotification.defaults|=Notification.DEFAULT_VIBRATE;
  28. mNotification.contentView=null;
  29. Intentintent=newIntent(mContext,
  30. FriendListActivity.class);
  31. PendingIntentcontentIntent=PendingIntent.getActivity(
  32. mContext,0,intent,0);
  33. mNotification.setLatestEventInfo(mContext,util.getName()
  34. +"("+newMsgNum+"条新消息)",content,
  35. contentIntent);
  36. }
  37. mNotificationManager.notify(Constants.NOTIFY_ID,mNotification);//通知一下才会生效哦
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. };


四,监听消息,我们监听收消息线程中收到的消息先判断程序是否运行在后台,如果在后台,我们就直接把消息发送给handler,如果不是,就通过广播发送出去这个消息,所以:我们首先需要在按返回键的进入后台的时候,做一个标记,表示程序进入后台运行,我这里是通过保存在SharedPreferenced文件中的,其实可以保存到应用的全局变量:

  1. in.setMessageListener(newMessageListener(){
  2. @Override
  3. publicvoidMessage(TranObjectmsg){
  4. //System.out.println("GetMsgService:"+msg);
  5. if(util.getIsStart()){//<spanstyle="color:#ff0000;">如果是在后台运行,就更新通知栏,否则就发送广播给</span>Activity
  6. if(msg.getType()==TranObjectType.MESSAGE){//只处理文本消息类型
  7. //System.out.println("收到新消息");
  8. //把消息对象发送到handler去处理
  9. Messagemessage=handler.obtainMessage();
  10. message.what=MSG;
  11. message.getData().putSerializable("msg",msg);
  12. handler.sendMessage(message);
  13. }
  14. }else{
  15. IntentbroadCast=newIntent();
  16. broadCast.setAction(Constants.ACTION);
  17. broadCast.putExtra(Constants.MSGKEY,msg);
  18. sendBroadcast(broadCast);//把收到的消息已广播的形式发送出去
  19. }
  20. }
  21. });
  22. 转自http://blog.csdn.net/way_ping_li/article/details/8054416
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics