项目中有用到消息提醒功能,所以来记录一下。
效果图1:

效果图2:

效果图3:

>关于消息的三个接口:

1.请求消息列表的接口
2.请求是否有未读数据的接口
3.设置消息已读的接口(就是告诉服务器,哪条数据已读,把消息id传过去就行)

>具体思路:

1.每一个消息对象中需要有一个字段isRead,标记消息是否已读,默认false。
2.刚进入程序时,需要调用是否有未读数据这个接口,根据返回值来控制底部导航栏上红点的显示与否。
3.点击进入消息这个界面时,调用消息列表的接口,将请求到的数据显示出来,显示的时候在适配器的getView中根据消息对象的isRead字段的值来控制条目左边红点的显示。
4.点击某一条消息时,将其对应消息对象的isRead置为true,表示已读,刷新界面,并调用设置消息已读的接口,服务器返回成功后再调用是否有未读数据的接口,也就是实时控制底部导航栏红点的显示。
5.当退出程序再次进入,然后进入消息界面时,由于是从服务器返回的字段isRead来判断每一条消息左边红点的显示,所以不会有什么问题。
备注:之前是在本地做的处理,用了SharedPreferences,这样当再次进入时,状态会有问题。

一部分代码:
处理设置消息已读请求返回的结果:

public void handleSetMsgHasRead(Message msg){
if(null!=msg){
String json = (String) msg.obj;
JSONObject object;
try {
object = new JSONObject(json);
String fromJson = object.getString("message");
if("OK".equals(fromJson)){
//去请求是否有未读消息
LshItaskSEApplication.netService.getMsgNotReadList(state,"hasNoReadMessage",handler);
}else{
Toast.makeText(context, ""+fromJson, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}

处理是否有未读数据返回的结果:

public void handleMsgNotRead(Message msg){
if(null!=msg){
String json = (String) msg.obj;
JSONObject object;
try {
object = new JSONObject(json);
boolean fromJson = object.getBoolean("data");
if(true==fromJson){//没有未读消息
//这时底部导航栏消息上的红点需要消失
MainActivity.fragmentBtm.setTips("", "#00000000", "#FFFFFF", 3);
}else{//有未读消息
//这时底部导航栏消息上的红点是存在的
MainActivity.fragmentBtm.setTips("", "#FF0000", "#FFFFFF", 3);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}

设置红点的代码:

public class FragmentBtmButton extends HorizontalScrollView {Context context;int[] selectedDrawble;int[] notSelectedDrawble;String[] tipsTexts;String[] tipsBgColors;String[] tipsTextColors;int btWidth, btHeight;ScrollView x;TextView[] btArray;CKNameView[] ckNameViews;RelativeLayout[] relativeLayouts;int[] btID;LinearLayout ll_container;int defSelect;public FragmentBtmButton(Context context) {super(context);}public FragmentBtmButton(Context context, AttributeSet attrs) {super(context, attrs);setHorizontalScrollBarEnabled(false);}public void setData(Context context, int[] selectedDrawble, int[] notSelectedDrawble, int defSelect) {this.context = context;this.selectedDrawble = selectedDrawble;this.notSelectedDrawble = notSelectedDrawble;this.defSelect = defSelect;btWidth = (int) getBtnWidth();btHeight = DensityUtil.dip2px(context, 50);btID = new int[selectedDrawble.length];tipsTexts = new String[selectedDrawble.length];tipsBgColors = new String[selectedDrawble.length];tipsTextColors = new String[selectedDrawble.length];for (int i = 0; i < selectedDrawble.length; i++) {btID[i] = i + 100;tipsTexts[i] = "";tipsBgColors[i] = "#00ffffff";tipsTextColors[i] = "#00ffffff";}addViewSub();}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);if (l == 0) {isIndicateShow.isShow(View.INVISIBLE, View.VISIBLE);} else {if (oldl == 0 || oldl + getWidth() == computeHorizontalScrollRange()) {isIndicateShow.isShow(View.VISIBLE, View.VISIBLE);}if (l + getWidth() == computeHorizontalScrollRange()) {isIndicateShow.isShow(View.VISIBLE, View.INVISIBLE);}}}/*** 为scrollview添加子view*/private void addViewSub() {FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,btHeight);ll_container = new LinearLayout(context);ll_container.setOrientation(LinearLayout.HORIZONTAL);addView(ll_container, frameLayoutParams);LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(btWidth, btHeight);RelativeLayout.LayoutParams btAddlayoutParams = new RelativeLayout.LayoutParams(btWidth, btHeight);RelativeLayout.LayoutParams tipsAddlayoutParams = new RelativeLayout.LayoutParams(DensityUtil.dip2px(context, 10), DensityUtil.dip2px(context, 10));tipsAddlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);tipsAddlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);tipsAddlayoutParams.rightMargin = btWidth/5;btArray = new TextView[selectedDrawble.length];ckNameViews = new CKNameView[selectedDrawble.length];relativeLayouts = new RelativeLayout[selectedDrawble.length];for (int i = 0; i < selectedDrawble.length; i++) {btArray[i] = new TextView(context);btArray[i].setId(btID[i]);ckNameViews[i] = new CKNameView(context);ckNameViews[i].setText(tipsTexts[i]);ckNameViews[i].setPaintColor(tipsTextColors[i], tipsBgColors[i]);relativeLayouts[i] = new RelativeLayout(context);if (i == defSelect) {btArray[i].setBackgroundResource(selectedDrawble[i]);} else {btArray[i].setBackgroundResource(notSelectedDrawble[i]);}btArray[i].setOnClickListener(new OnButtonClick());ll_container.addView(relativeLayouts[i], linearLayoutParams);relativeLayouts[i].addView(btArray[i], btAddlayoutParams);relativeLayouts[i].addView(ckNameViews[i], tipsAddlayoutParams);}};public void setTips(String[] tipsTexts,String[] tipsBgColors,String[] tipsTextColors) {this.tipsTexts = tipsTexts;this.tipsBgColors = tipsBgColors;this.tipsTextColors = tipsTextColors;for (int i = 0; i < ckNameViews.length; i++) {ckNameViews[i].setText(tipsTexts[i]);ckNameViews[i].setPaintColor(tipsTextColors[i], tipsBgColors[i]);ckNameViews[i].invalidate();}}/*** 设置指定位置的tips 文字,颜色,背景色* * @param tipsText* @param position*/public void setTips(String tipsText, String tipsBgColor,String tipsTextColor,int position) {tipsTexts[position] = tipsText;tipsBgColors[position] = tipsBgColor;tipsTextColors[position] = tipsTextColor;ckNameViews[position].setText(tipsTexts[position]);ckNameViews[position].setPaintColor(tipsTextColors[position], tipsBgColors[position]);ckNameViews[position].invalidate();}class OnButtonClick implements OnClickListener {public void onClick(View v) {int tempId = v.getId();for (int i = 0; i < selectedDrawble.length; i++) {if (tempId == btID[i]) {btArray[i].setBackgroundResource(selectedDrawble[i]);if (onBtmButtonSelect != null) {onBtmButtonSelect.onBtmButtonSelect(i);}} else {btArray[i].setBackgroundResource(notSelectedDrawble[i]);}}}}/*** 获取底部单个button的宽度* * @return*/private float getBtnWidth() {float tempWidth = context.getResources().getDisplayMetrics().widthPixels;float result = 0;float size = selectedDrawble.length >= 5 ? 5 : selectedDrawble.length;result = tempWidth / (size);return result;}public LinearLayout getLayoutContainer() {return ll_container;}public interface OnBtmButtonSelect {void onBtmButtonSelect(int position);}public void setOnBtmButtonSelect(OnBtmButtonSelect onBtmButtonSelect) {this.onBtmButtonSelect = onBtmButtonSelect;}OnBtmButtonSelect onBtmButtonSelect;public interface IsIndicateShow {void isShow(int leftState, int rightState);}public void setIsIndicateShow(IsIndicateShow isIndicateShow) {this.isIndicateShow = isIndicateShow;}IsIndicateShow isIndicateShow;
}

Android消息提醒相关推荐

  1. Android仿QQ锁屏状态下消息提醒(震动+提示音)

    导读: 最近在开发一个定时提醒业务,类似于闹钟,然后遇到了一个问题,当APP应用在后台运行时,用户关闭了手机屏幕(手机进入灭屏休眠状态),这个时候使用系统震动和闹钟没有起到作用.why? 同样是灭屏休 ...

  2. Android底部导航栏+消息提醒

    Android底部导航栏+消息提醒 最近想在网上找一些Android底部导航栏切换并能提供消息提醒的案例,虽然有很多案例但都不是我想要的.我就开始自己瞎研究了,废话不多说了,直接上代码. 1.先创建一 ...

  3. android新消息提醒功能,Android仿微信新消息提示音

    怕有些人不知道怎么进入微信的新消息提示音功能,我这里说下操作步骤: 打开微信----我---设置---新消息提醒---新消息提示音. 经过以上的步骤就进入了这样的界面 具体实现的步骤. 难点之一:获取 ...

  4. Android仿微信朋友圈6之实现消息提醒功能

    之前有朋友问我消息提醒咋实现,我一直没有整理出来,今天就放出来.微信朋友圈的消息提醒就是收到朋友的评论后背景底部显示消息条数和评论用户,顶部是一张相册背景和当前用户昵称头像. 1.消息提醒的布局如下: ...

  5. Android Studio_Toast消息提醒

    Android Studio_Toast消息提醒 1.Toast是Android系统提供的一种非常简洁的消息提醒方式,程序中可以使用它实现将短小的消息通知给用户,一点时间后自动消失,且不占用屏幕的任何 ...

  6. 华为设置android系统提醒功能,华为Watch GT2消息提醒怎么设置?短信微信消息提醒设置方法...

    华为Watch GT2是一款物美价廉的智能手表,同样也是一款非常实用的运动手表,那么华为watch gt2怎么设置消息提醒呢,为了不让大家错过手表上的短信.QQ和微信消息提醒,今天智能手机网小编就来分 ...

  7. 华为设置android系统提醒功能,华为Watch GT智能手表怎么设置消息提醒功能

    华为最近上线发售了一款智能手表-Watch GT,这款产品可能是很多粉丝期待已久的了.而很多用户订购之后回去可能有些问题不会使用,比如怎么设置华为Watch GT的消息提醒功能.接下来小编给大家带来相 ...

  8. Android之基于xmpp openfire smack开发之Android消息推送技术原理分析和实践[4]

    http://blog.csdn.net/shimiso/article/details/8156439 前面几篇给大家系统讲解的有关xmpp openfire smack asmack相关的技术和使 ...

  9. 微信消息提醒与消息数字提示之BadgeView

    微信消息提醒与消息数字提示之BadgeView BadgeView 一个可以自由定制外观.支持拖拽消除的 MaterialDesign 风格 Android BadgeView. GitHub地址:h ...

最新文章

  1. Web开发者推荐的最佳HTML5/CSS3代码生成器
  2. Java中Calendar.DAY_OF_WEEK、DAY_OF_MONTH需要减一的原因
  3. 为什么在C#中捕获并抛出异常?
  4. STM32 LCD中英文字符显示学习笔记
  5. linux 打开端口1935,CentOS服务器开放端口
  6. WebLogic 12c 修改节点 Managed Server 和 AdminServer 内存方法
  7. 视频专辑:轻松学习flash动画制作视频教程
  8. ASP.NET Core 动作结果 - ASP.NET Core 基础教程 - 简单教程,简单编程
  9. .NET FM的未来计划
  10. r vector 4 elements_Vector类与Enumeration接口
  11. mvc identity连接mysql_asp.net MVC5,如何使用mysql数据库,使用MVC框架中自带的identity用户验证体系...
  12. jquery $.each遍历json数组方法
  13. 机器手六维坐标怎么定义_机器人学——2.4-坐标系的旋转和运动增量
  14. ajax php cookie,php setcookie没有使用ajax调用
  15. [转] 先验概率与后验概率贝叶斯与似然函数
  16. 智能优化算法:纵横交叉算法-附代码
  17. 常用工具软件使用【1】
  18. 计算机连接不上蓝牙鼠标,如果蓝牙鼠标无法连接到计算机该怎么办?
  19. 百钱百鸡问题最优解法
  20. Html中几种图片格式的区别与使用--我的笔记

热门文章

  1. Windows必装的3款免费效率工具,排名不分先后
  2. 【电路理论】1-10 两类约束 KVL、KCL方程的独立性
  3. 考研计算机专业课时长,计算机考研复习经验
  4. 分布式计算模式:MapReduce
  5. Android应用面试题及答案汇总
  6. checkbox 点击搜索失去焦点_jquery获取焦点和失去焦点事件代码
  7. Servlet和tomcat部署
  8. 【计算机网络】数据流简单分析
  9. 北京2017年7月开始 社保最低缴费
  10. qmail Vpopmail And sqwebmail HOWTO