Android消息提醒
来源:互联网 发布:ipad绿色上网软件 编辑:程序博客网 时间:2024/05/17 23:46
项目中有用到消息提醒功能,所以来记录一下。
效果图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(); } @Override protected 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消息提醒
- android 消息提醒(Toast,Notification)
- Android 桌面图标消息数提醒
- Android 桌面图标消息数提醒
- Android luancher消息提醒-ShortcutBadger
- [android]消息的小图标提醒
- android实现消息提醒,拨打电话功能
- Android自定义带消息提醒控件
- Android----------短信消息提醒(Notification)
- android 消息提醒控件之 Toast
- 【Android 初学者】菜单和消息提醒
- Android给控件添加消息提醒
- android控件上面实现提醒信息(消息数量)
- android消息通知更新(小红点,数字提醒)之badgeview
- Android仿qq实现锁屏消息提醒
- 消息到达提醒功能
- [IOS]消息提醒--TSMessages
- [IOS消息提醒]--- DMRNotificationView
- C++ Primer课后练习12.2
- Dropout资源【2】
- MATLAB常用函数
- win10的linux子系统的安装、卸载与使用
- Java排序算法(转)
- Android消息提醒
- 目标
- Eclise常用快捷键
- codeforces 766 A Mahmoud and Longest Uncommon Subsequence
- 归并排序原理及Java实现
- 事件驱动的HLog写入模型
- {小结}GDKOI2017 被翻盘记
- PHP+ffmpeg+nginx的配置实现视频转码
- 性能测试工具原理与架构