因为之前的是写的主要代码,现在贴出全部代码,有些瑕疵,哈哈哈哈。那我现在开始贴代码吧

1.聊天实体类
(1)聊天实体类
ChatUser.java

package com.smxy.lj.chat;import java.io.Serializable;public class ChatUser implements Serializable {private String cid;private String msg;public ChatUser() {}public ChatUser(String cid, String msg) {this.cid = cid;this.msg = msg;}public String getCid() {return cid;}public void setCid(String cid) {this.cid = cid;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}
}

(2)聊天界面实体类

package com.smxy.lj.message;public class Message {public static final int TYPE_RECEIVED = 0;//消息类型-收到的消息public static final int TYPE_SENT = 1;//消息类型-发出的消息private String content; //消息内容private int type;//消息类型public Message(String content, int type) {this.content = content;this.type = type;}public String getContent() {return content;}public int getType() {return type;}
}

(3)个推服务类
DemoIntentService.java

package com.smxy.lj.chat;import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.smxy.lj.utils.MyLog;
import com.smxy.lj.utils.SharedPreferencesHelper;/*** 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>* onReceiveMessageData 处理透传消息<br>* onReceiveClientId 接收 cid <br>* onReceiveOnlineState cid 离线上线通知 <br>* onReceiveCommandResult 各种事件处理回执 <br>*/
public class DemoIntentService extends GTIntentService {SharedPreferencesHelper sharedPreferencesHelper ;private OnProgressListener onProgressListener;public void setOnProgressListener(OnProgressListener onProgressListener) {this.onProgressListener = onProgressListener;}@Overridepublic void onReceiveServicePid(Context context, int i) {}@Overridepublic void onReceiveClientId(Context context, String clientid) {Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);sharedPreferencesHelper = new SharedPreferencesHelper(getApplicationContext(),"push_CID");sharedPreferencesHelper.put("cid",clientid);//MyLog.e("CID="+ sharedPreferencesHelper.getSharedPreference("cid",null));}//接收消息 ---透传@Overridepublic void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage) {Log.e(TAG, "onReceiveMessageData -> " + "message = " + new String(gtTransmitMessage.getPayload()));if (onProgressListener!=null){onProgressListener.onProgress(new String(gtTransmitMessage.getPayload()));}}@Nullable@Overridepublic IBinder onBind(Intent intent) {return new MyGeTui();}public class MyGeTui extends Binder{public DemoIntentService get(){return DemoIntentService.this;}}@Overridepublic void onReceiveOnlineState(Context context, boolean b) {}@Overridepublic void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {}@Overridepublic void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage) {}@Overridepublic void onNotificationMessageClicked(Context context, GTNotificationMessage gtNotificationMessage) {}//回调,将接收的内容传到主界面public interface OnProgressListener {void onProgress(String  progress);}
}

2.首先是聊天界面
(1)写聊天界面的adapter

message_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="10dp"><LinearLayoutandroid:id="@+id/left_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left"android:background="@drawable/message_left"><TextViewandroid:id="@+id/left_msg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp" /></LinearLayout><LinearLayoutandroid:id="@+id/right_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:background="@drawable/message_right"><TextViewandroid:id="@+id/right_msg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp" /></LinearLayout></LinearLayout>

MsgAdapter.java

package com.smxy.lj.message;import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;import com.smxy.lj.officedemo.R;import org.w3c.dom.Text;import java.util.List;public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {private List<Message> mMsgList;static class ViewHolder extends RecyclerView.ViewHolder {LinearLayout leftLayout, rightLayout;TextView leftMsg, rightMsg;public ViewHolder(View view) {super( view );leftLayout = view.findViewById( R.id.left_layout );rightLayout = view.findViewById( R.id.right_layout );leftMsg = view.findViewById( R.id.left_msg );rightMsg = view.findViewById( R.id.right_msg );}}public MsgAdapter(List<Message> mMsgList) {//构造方法this.mMsgList = mMsgList;}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.message_item, parent, false );return new ViewHolder( view );}@Overridepublic void onBindViewHolder(MsgAdapter.ViewHolder holder, int position) {Message msg = mMsgList.get( position );switch (msg.getType()) {//收到消息,显示左边布局,隐藏右边布局case Message.TYPE_RECEIVED:holder.leftLayout.setVisibility( View.VISIBLE );holder.rightLayout.setVisibility( View.GONE );holder.leftMsg.setText( msg.getContent() );break;//发送消息,显示右边布局,隐藏左边布局case Message.TYPE_SENT:holder.leftLayout.setVisibility( View.GONE );holder.rightLayout.setVisibility( View.VISIBLE );holder.rightMsg.setText( msg.getContent() );break;default:break;}}@Overridepublic int getItemCount() {return mMsgList.size();}
}

(2)聊天界面

memo_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><android.support.v7.widget.RecyclerViewandroid:id="@+id/msg_recycler_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"/><LinearLayoutandroid:layout_width="match_parent"android:orientation="horizontal"android:layout_height="wrap_content"><EditTextandroid:id="@+id/message_text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="Type Something Here"/><Buttonandroid:id="@+id/message_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送" /></LinearLayout></LinearLayout>

MessageFragment.java

package com.smxy.lj.myfragment;import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.smxy.lj.adapter.MessageNoticeAdapter;
import com.smxy.lj.chat.ChatUser;
import com.smxy.lj.chat.DemoIntentService;
import com.smxy.lj.message.Message;
import com.smxy.lj.message.MsgAdapter;
import com.smxy.lj.officedemo.R;
import com.smxy.lj.okhttputil.HttpUtil;
import com.smxy.lj.pojo.MessageNotice;
import com.smxy.lj.utils.MyLog;
import com.smxy.lj.utils.ServletUrl;
import com.smxy.lj.utils.SharedPreferencesHelper;import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;public class MessageFragment extends BaseFragement {ChatUser chatUser;//public static String URL = "http://lj1757620885.6655.la:54746/office/pushMessage?cid=";private SharedPreferencesHelper sharedPreferencesHelper;private String CID,sendMesageContent;//好友的CIDprivate List<Message> msgList = new ArrayList<>();@BindView(R.id.message_text) EditText inputText;@BindView(R.id.msg_recycler_view) RecyclerView msgRecyclerView;private MsgAdapter msgAdapter;private DemoIntentService msgService;public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.memo_lists, null);ButterKnife.bind(this,view);Intent intent = new Intent(getActivity(), DemoIntentService.class);getActivity().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);initMsgs();inputText = view.findViewById(R.id.message_text);msgRecyclerView = view.findViewById(R.id.msg_recycler_view);LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());msgRecyclerView.setLayoutManager(layoutManager);msgAdapter = new MsgAdapter(msgList);msgRecyclerView.setAdapter(msgAdapter);return view;}private void initMsgs() {//chatUser = new ChatUser();sharedPreferencesHelper = new SharedPreferencesHelper(getContext(),"push_CID");CID = (String) sharedPreferencesHelper.getSharedPreference("cid",null);MyLog.e("CID==="+CID);Message msg1 = new Message("物一群的邀请码是多少", Message.TYPE_RECEIVED);msgList.add(msg1);Message msg2 = new Message("0X897888V", Message.TYPE_SENT);msgList.add(msg2);Message msg3 = new Message("谢谢你", Message.TYPE_RECEIVED);msgList.add(msg3);}@OnClick(R.id.message_send)public void sedMessage(){sendMesageContent = inputText.getText().toString();chatUser = new ChatUser();chatUser.setCid("fb61f569efd33cf3eb536eb945537acb");chatUser.setMsg(sendMesageContent);if (!"".equals(sendMesageContent)) {Message msg = new Message(sendMesageContent, Message.TYPE_SENT);msgList.add(msg);//当有新消息时,调用适配器notifyItemInserted通知列表有新的数据插入,刷新RecyclerViewmsgAdapter.notifyItemInserted(msgList.size() - 1);//将RecyclerView定位到最后一行,保证可以看到最新消息msgRecyclerView.scrollToPosition(msgList.size() - 1);inputText.setText("");Gson gson = new Gson();String json = gson.toJson(chatUser);HttpUtil.getCall("/pushMessage",json).enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {}@Overridepublic void onResponse(Call call, Response response) throws IOException {}});}}ServiceConnection mServiceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName componentName, IBinder iBinder) {msgService = ((DemoIntentService.MyGeTui) iBinder).get();msgService.setOnProgressListener(new DemoIntentService.OnProgressListener() {@Overridepublic void onProgress(String  progress) {//   mProgressBar.setProgress(progress);android.os.Message message = android.os.Message.obtain();message.obj = progress;mHandler.sendMessage(message);}});}@Overridepublic void onServiceDisconnected(ComponentName componentName) {}};private Handler mHandler = new Handler(new Handler.Callback() {@Overridepublic boolean handleMessage(android.os.Message message) {Message msg = new Message((String) message.obj, Message.TYPE_RECEIVED);msgList.add(msg);//当有新消息时,调用适配器notifyItemInserted通知列表有新的数据插入,刷新RecyclerViewmsgAdapter.notifyItemInserted(msgList.size() - 1);//将RecyclerView定位到最后一行,保证可以看到最新消息msgRecyclerView.scrollToPosition(msgList.size() - 1);return true;}});}

【聊天界面的图片使用的是【.9.png】图片】

Android Studio-个推-实现简单聊天(三)相关推荐

  1. 《Android Studio开发实战》学习(三)- 展示图片

    <Android Studio开发实战>学习(三)- 展示图片 背景 问题描述 将图片添加到Android Studio资源中 图像视图ImageView的使用 关闭APP中标题的显示 图 ...

  2. Android Studio实现推箱子小游戏

    项目目录 一.项目概述 二.开发环境 三.详细设计 四.运行演示 五.项目总结 六.源码获取 一.项目概述 推箱子是一款非常受欢迎的益智游戏,游戏的玩法简单,但是需要玩家具备一定的逻辑思维能力和空间感 ...

  3. (超多图)基于Android studio开发的一个简单入门小应用(超级详细!!)(建议收藏)

    基于Android studio开发的一个简单入门小应用 一.前言 二.前期准备 三.开发一个小应用 五.运行应用 一.前言 在暑假期间,我学习JAVA基础,为了能早日实现自己用代码写出一个app的& ...

  4. Android Studio学习记录之简单的页面切换及宫格菜单

    Android Studio学习记录之简单的页面切换及宫格菜单 之前上课听老师讲的一些东西自己其实并没有消化,今天把不懂的都去网上搜了一下,有了一种恍然大悟的感觉,包括很多方方面面的东西.有些东西听说 ...

  5. 用Android Studio设计的一个简单的闹钟APP

    该闹钟是用Android Studio为安卓手机设计的一个简单的闹钟APP 一.介绍系统的设计界面 闹钟的布局文件代码如下 <?xml version="1.0" encod ...

  6. 在 Android Studio 中创建一个简单的 QQ 登录界面

    一,创建一个新的 Android Studio 项目 打开 Android Studio,选择 "Start a new Android Studio project",然后填写应 ...

  7. NDK JNI Android Studio开发与调试DEMO(三)(生成 .so 文件)

    Android Studio NDK 开发与调试(生成 .so 文件) 温馨提示:如果你的 Android Studio 版本在 3.0以上 , 建议你用 cMake /ndk-build 的新姿势进 ...

  8. IDEA(或Android Studio)推送(push)代码报错 unable to read askpass response from ‘C:\Users\Urasaki\AppData\Loc

    推送给代码报错 unable to read askpass response from 'C:\Users\Urasaki\AppData\Local\Google\AndroidStudio202 ...

  9. android studio开发整合资源简单实现android扫一扫功能

    开发安卓app过程中需要集成扫码功能,搜索的时候发现了 http://blog.csdn.net/yuzhiqiang_1993/article/details/52805057 这篇博客文章,写的很 ...

最新文章

  1. 编译Cocos2dx程序 (一)
  2. 面试:说说参数验证 @Validated 和 @Valid 的区别?
  3. haskell,lisp,erlang你们更喜欢哪个?
  4. bat脚本交互输入_测评 | 不使用powershell运行 PowerShell 脚本的工具汇总
  5. 函数①函数声明与表达式
  6. vue 各组件 使用 Demo
  7. Angular自动取消订阅RxJs
  8. ExtJS下页面显示中文乱码问题
  9. 11.6 通信实例与ASCII码
  10. 说说传统的软件销售案例
  11. Springboot+微信小程序自习室管理系统毕业设计源码221535
  12. Day739.GEO经纬度数据结构自定义数据结构 -Redis 核心技术与实战
  13. 并发批量管理500台以上服务器脚本分享(shell版)
  14. (强制)类型转换方法
  15. Halcon小技巧:二维平面根据两个点确定方向向量+三维空间点确定姿态
  16. VB6升级到VB2010之五: 从Unload 升级成Me.Close看VB2010有多类~
  17. 17.6:迪瑞克斯啦算法
  18. 计算机老师任课教师寄语,新学期任课教师寄语
  19. 峰值检波电路的作用和原理_最简单的峰值检波电路
  20. 树莓派最新版系统烧写和网络配置

热门文章

  1. CUDA之Thread、Wrap执行详解
  2. 名帖249 文徵明 行书《滕王阁序》
  3. 爬虫爬取百度图片--python3
  4. PYNQ_Z2从vivado到SDK的PS到PL点灯以及固化流程
  5. 来吧,一个IoT应用设计 1
  6. photoshop基础学习之绘制耐克图标(循序渐进)
  7. Dell戴尔灵越笔记本电脑Inspiron 15 7510原装出厂Windows10系统恢复原厂OEM系统
  8. Oracle(一):我与安装Oracle的那些事
  9. vCenter(PSC)正常更改或重置administrator@vsphere.local用户的密码方法
  10. 小熊读书:如何自我提升——《软技能》