适用于Android开发的简单聊天软件

适用于android 开发。是一个简单的聊天软件,包括知识点,各个控件的运用(ExpandableListView,ViewPager,Spinner,LinearLayout,RelativeLayot),自定义的ViaImageView(自定义xml属性),sql 的写入,读取等操作。

1. ViaImageView.java   

package com.farina.farinaimagelib;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by FarinaZhang on 2016/5/25.
 */
public class ViaImageView extends ImageView {
 private int shapeType=-1;
 private int borderWidth=0; //the image 's border width
 private int borderColor= Color.BLACK; //the image 's border color
 private boolean useDefaultStyle =false; //draw a common imageview
 private static final Xfermode MASK_XFERMODE;
 private int AnimType=-1; //动画类型
 private int mWidth; //图片的宽度
 private int mHeight; //图片的高度

 static {
 PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;
 MASK_XFERMODE = new PorterDuffXfermode(localMode);
 }

 public static enum Shape {circle,triangle,star,hexagonal,rectangle};

 public ViaImageView(Context context){
 super(context);
 init(context, null, 0);
 }
 public ViaImageView(Context context, AttributeSet attrs){
 super(context, attrs);
 init(context, attrs, 0);
 }
 public ViaImageView(Context context,AttributeSet attrs,int defStyle){
 super(context, attrs, defStyle);
 init(context, attrs, defStyle);
 }

 public void init(Context context,AttributeSet attrs,int defStyle){

 if(attrs!=null){
  TypedArray a = getContext().obtainStyledAttributes(
   attrs,R.styleable.viaImage, defStyle, 0);
  shapeType = (int)a.getInteger(R.styleable.viaImage_shapeType,shapeType);
  borderWidth=(int)a.getDimension(R.styleable.viaImage_borderWidth,borderWidth);
  borderColor = a.getColor(R.styleable.viaImage_borderColor, borderColor);

  a.recycle();

 }
 }

 /**
 *外部接口
 *设置ImageView的属性,包括形状,边框颜色,边框宽度
 **/
 public void setImageViewProperty(int shapeType,int width,int color){
 this.shapeType = shapeType;
 this.borderWidth = width;
 this.borderColor = color;

 postInvalidate();
 }

 public void setImageViewShape(int shapeType){
 this.shapeType = shapeType;

 postInvalidate();
 }

 public void setImageBorderWidth(int width){
 this.borderWidth = width;
 postInvalidate();
 }
 public void setImageBorderColor(int color){
 this.borderColor = color;
 postInvalidate();
 }

 /** 外部接口
 * 设置是否以默认格式绘制,普通格式
 */
 public void setDrawCommonImage(boolean isCommon){
 this.useDefaultStyle=isCommon;
 }

 @Override
 protected void onDraw(Canvas canvas){
 if(useDefaultStyle){
  super.onDraw(canvas);
  return;
 }
 final Drawable localDrawable=getDrawable();
 if(localDrawable==null)return;
 if(localDrawable instanceof NinePatchDrawable)return;

 mWidth=getWidth();
 mHeight=getHeight();

 int layer=canvas.saveLayer(0.0f,0.0f,mWidth,mHeight,null,Canvas.ALL_SAVE_FLAG);
 localDrawable.setBounds(0,0,mWidth,mHeight);
 /*将drawable绑定到bitmap(this.mask)上面(drawable 只能通过bitmap显示出来)*/
 localDrawable.draw(canvas);

 createShapeBitmap(canvas);

 /*将画布复制到layer上*/
 canvas.restoreToCount(layer);

 if(borderWidth!=0){
  drawBorder(canvas);
 }

 }
 private void createShapeBitmap(Canvas canvas){
 Paint mypaint;

 mypaint = new Paint();
 mypaint.setFilterBitmap(false);
 mypaint.setAntiAlias(true);
 mypaint.setXfermode(MASK_XFERMODE);

  Bitmap.Config localConfig=Bitmap.Config.ARGB_8888;
  Bitmap localBitmap=Bitmap.createBitmap(mWidth,mHeight,localConfig);
  Canvas localCanvas=new Canvas(localBitmap);
  Paint localPaint = new Paint();
  localPaint.setAntiAlias(true);
  int padding=borderWidth;
  int radius = ((mWidth>mHeight)?mHeight:mWidth)/2-padding;

  switch(shapeType){
  case 0://圆形;
  {
   localCanvas.drawCircle(mWidth/2,mHeight/2, radius, localPaint);
   break;
  }
  case 1://三角形;
  {
   Path path = new Path();
   path.moveTo(padding, padding);
   path.lineTo(mWidth / 2, (float)(mWidth*Math.cos(degree2Radian(30))-padding));
   path.lineTo(mWidth-padding, padding);

   path.close();
   localCanvas.drawPath(path, localPaint);
   break;
  }
  case 2://五角星形;
  {
   Path path = new Path();
   float radian = degree2Radian(36);// 36为五角星的角度
   float radius_in = (float) (radius * Math.sin(radian / 2) / Math
    .cos(radian)); // 中间五边形的半径

   path.moveTo((float) (radius * Math.cos(radian / 2)), 0);// 此点为多边形的起点
   path.lineTo((float) (radius * Math.cos(radian / 2) + radius_in
     * Math.sin(radian)),
    (float) (radius - radius * Math.sin(radian / 2)));
   path.lineTo((float) (radius * Math.cos(radian / 2) * 2),
    (float) (radius - radius * Math.sin(radian / 2)));
   path.lineTo((float) (radius * Math.cos(radian / 2) + radius_in
     * Math.cos(radian / 2)),
    (float) (radius + radius_in * Math.sin(radian / 2)));
   path.lineTo(
    (float) (radius * Math.cos(radian / 2) + radius
     * Math.sin(radian)), (float) (radius + radius
     * Math.cos(radian)));
   path.lineTo((float) (radius * Math.cos(radian / 2)),
    (float) (radius + radius_in));
   path.lineTo(
    (float) (radius * Math.cos(radian / 2) - radius
     * Math.sin(radian)), (float) (radius + radius
     * Math.cos(radian)));
   path.lineTo((float) (radius * Math.cos(radian / 2) - radius_in
     * Math.cos(radian / 2)),
    (float) (radius + radius_in * Math.sin(radian / 2)));
   path.lineTo(0, (float) (radius - radius * Math.sin(radian / 2)));
   path.lineTo((float) (radius * Math.cos(radian / 2) - radius_in
     * Math.sin(radian)),
    (float) (radius - radius * Math.sin(radian / 2)));

   path.close();// 使这些点构成封闭的多边形
   localCanvas.drawPath(path, localPaint);
   break;
  }
  case 3://正六边形;
  {
   Path path= new Path();
   float radian = degree2Radian(30);
   path.moveTo((float)(radius*Math.sin(radian)), 0);
   path.lineTo((float)(radius*Math.sin(radian)+radius), 0);
   path.lineTo((float)(2*radius), (float)(radius*Math.cos(radian)) );
   path.lineTo((float)(radius*Math.sin(radian)+radius),(float)(2*(radius*Math.cos(radian))));
   path.lineTo((float)(radius*Math.sin(radian)),(float)(2*(radius*Math.cos(radian))));
   path.lineTo(0,(float)(radius*Math.cos(radian)));
   path.close();
   localCanvas.drawPath(path,localPaint);
   break;
  }
  default://方形
  {
   int realWidth = (mWidth>mHeight)?mHeight:mWidth;
   int x = padding +(mWidth-realWidth)/2;
   int y = padding +(mHeight-realWidth)/2;
   RectF localRectF=new RectF(x, y, realWidth-padding*2, realWidth-padding*2);
   localCanvas.drawRect(localRectF, localPaint);
   break;
  }
  }

 /*将bitmap 画到canvas上*/
 canvas.drawBitmap(localBitmap,0.0f,0.0f,mypaint);
 }

 //画边框
 private void drawBorder(Canvas canvas){
 Paint localPaint = new Paint();
 localPaint.setColor(borderColor);
 localPaint.setStyle(Paint.Style.STROKE);
 localPaint.setStrokeWidth(borderWidth);
 localPaint.setAntiAlias(true);

 int radius=((mWidth>mHeight)?mHeight:mWidth)/2-borderWidth/2;
 int padding = borderWidth/2;
 switch(shapeType){
  case 0://圆形;
  {
  canvas.drawCircle(mWidth/2, mHeight/2,radius,localPaint);
  break;
  }
  case 1://三角形;
  {
  Path path = new Path();
  path.moveTo(padding, padding);
  path.lineTo(mWidth / 2, (float)(mWidth*Math.cos(degree2Radian(30))-padding));
  path.lineTo(mWidth-padding, padding);

  path.close();
  canvas.drawPath(path, localPaint);
  break;
  }
  case 2://五角星形;
  {
  Path path = new Path();
  float radian = degree2Radian(36);// 36为五角星的角度
  float radius_in = (float) (radius * Math.sin(radian / 2) / Math
   .cos(radian)); // 中间五边形的半径

  path.moveTo((float) (radius * Math.cos(radian / 2)), 0);// 此点为多边形的起点
  path.lineTo((float) (radius * Math.cos(radian / 2) + radius_in
    * Math.sin(radian)),
   (float) (radius - radius * Math.sin(radian / 2)));
  path.lineTo((float) (radius * Math.cos(radian / 2) * 2),
   (float) (radius - radius * Math.sin(radian / 2)));
  path.lineTo((float) (radius * Math.cos(radian / 2) + radius_in
    * Math.cos(radian / 2)),
   (float) (radius + radius_in * Math.sin(radian / 2)));
  path.lineTo(
   (float) (radius * Math.cos(radian / 2) + radius
    * Math.sin(radian)), (float) (radius + radius
    * Math.cos(radian)));
  path.lineTo((float) (radius * Math.cos(radian / 2)),
   (float) (radius + radius_in));
  path.lineTo(
   (float) (radius * Math.cos(radian / 2) - radius
    * Math.sin(radian)), (float) (radius + radius
    * Math.cos(radian)));
  path.lineTo((float) (radius * Math.cos(radian / 2) - radius_in
    * Math.cos(radian / 2)),
   (float) (radius + radius_in * Math.sin(radian / 2)));
  path.lineTo(0, (float) (radius - radius * Math.sin(radian / 2)));
  path.lineTo((float) (radius * Math.cos(radian / 2) - radius_in
    * Math.sin(radian)),
   (float) (radius - radius * Math.sin(radian / 2)));

  path.close();// 使这些点构成封闭的多边形
  canvas.drawPath(path, localPaint);
  break;
  }
  case 3://正六边形;
  {
  Path path= new Path();
  float radian = degree2Radian(30);
  path.moveTo((float)(radius*Math.sin(radian)), 0);
  path.lineTo((float)(radius*Math.sin(radian)+radius), 0);
  path.lineTo((float)(2*radius), (float)(radius*Math.cos(radian)) );
  path.lineTo((float)(radius*Math.sin(radian)+radius),(float)(2*(radius*Math.cos(radian))));
  path.lineTo((float)(radius*Math.sin(radian)),(float)(2*(radius*Math.cos(radian))));
  path.lineTo(0,(float)(radius*Math.cos(radian)));
  path.close();
  canvas.drawPath(path,localPaint);
  break;
  }
  default://方形
  {
  int realWidth = (mWidth>mHeight)?mHeight:mWidth-borderWidth;
  int x = padding +(mWidth-realWidth)/2;
  int y = padding +(mHeight-realWidth)/2;
  RectF localRectF=new RectF(x, y, realWidth-padding*2, realWidth-padding*2);
  canvas.drawRect(localRectF, localPaint);
  break;
  }
 }
 }

 //将角度转化为弧度
 private float degree2Radian(int degree) {
 // TODO Auto-generated method stub
 return (float) (Math.PI * degree / 180);
 }

}

2. ContactExpendListAdapter.java

package com.farina.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.farina.data.FriendEntity;
import com.farina.data.FriendListData;
import com.farina.farinaimagelib.ViaImageView;
import com.farina.libtest.R;

import java.util.List;

/**
 * Created by FarinaZhang on 2016/7/5.
 */
public class ContactExpendListAdapter extends BaseExpandableListAdapter {
 private Context mContext;
 private List<FriendListData> mListData;

 public ContactExpendListAdapter(Context context, List<FriendListData> data){
 mContext = context;
 mListData = data;
 }
 @Override
 public int getGroupCount(){
 return mListData.size();
 }
 @Override
 public int getChildrenCount(int groupPosition){
 return mListData.get(groupPosition).getFriendChildList().size();
 }
 @Override
 public Object getGroup(int groupPosition){
 return mListData.get(groupPosition).getGroupName();
 }
 @Override
 public Object getChild(int groupPosition, int childPosition){
 return mListData.get(groupPosition).getFriendChildList().get(childPosition);
 }
 @Override
 public long getGroupId(int groupPosition){
 return groupPosition;
 }
 @Override
 public long getChildId(int groupPosition, int childPosition){
 return childPosition;
 }
 @Override
 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent){
 groupHolder viewHolder =null;
 if(convertView == null){
  convertView = LayoutInflater.from(mContext).inflate(R.layout.contact_list_group, null);
  viewHolder = new groupHolder();
  viewHolder.icon = (ImageView)convertView.findViewById(R.id.icon);
  viewHolder.groupName = (TextView)convertView.findViewById(R.id.goup_name);
  convertView.setTag(viewHolder);
 }else{
  viewHolder = (groupHolder)convertView.getTag();
 }

 viewHolder.groupName.setText(mListData.get(groupPosition).getGroupName());
 if(isExpanded){
  viewHolder.icon.setImageResource(R.mipmap.arrow_down);
 }else{
  viewHolder.icon.setImageResource(R.mipmap.arrow_up);
 }

 return convertView;
 }
 @Override
 public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
    View convertView, ViewGroup parent){
 memberHolder viewHolder = null;
 if(convertView == null){
  convertView = LayoutInflater.from(mContext).inflate(R.layout.contact_list_member, null);

  viewHolder= new memberHolder();
  viewHolder.memberImg = (ViaImageView) convertView.findViewById(R.id.member_img);
  viewHolder.memberName = (TextView)convertView.findViewById(R.id.member_name);
  convertView.setTag(viewHolder);
 }else{
  viewHolder =(memberHolder)convertView.getTag();
 }
 FriendEntity entity= mListData.get(groupPosition).getFriendChildList().get(childPosition);
 String icnPath = entity.getIcnPath();
 if(icnPath == null||icnPath.length()<=0) {
  viewHolder.memberImg.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.user_img));
 }
 viewHolder.memberName.setText(entity.getName());
 return convertView;
 }
 @Override
 public boolean isChildSelectable(int groupPosition,
     int childPosition) {
 return true;
 }

 @Override
 public boolean hasStableIds(){
 return true;
 }

 private class memberHolder{
 ViaImageView memberImg;
 TextView memberName;
 }
 private class groupHolder{
 ImageView icon;
 TextView groupName;
 }

}

3. ChatInfoManager.java

package com.farina.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.farina.data.FriendEntity;
import com.farina.data.FriendListData;
import com.farina.data.GroupEntity;
import com.farina.data.MessageEntity;
import com.farina.data.UserEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FarinaZhang on 2016/7/14.
 */
public class ChatInfoManager {
 private Context mContext;
 private ChatDBhelper mDBHelper;

 //table user string
 private final String USER_TABLE_NAME="users";
 private final String USER_ID="id";
 private final String USER_COUNT_NUMBER="countNumber";
 private final String USER_NIKENAME="nikeName";
 private final String USER_ICON_PATH="iconPath";
 private final String USER_PHONE_NUMBER="phoneNumber";
 private final String USER_MAIL_ADDR="mailAddr";
 private final String USER_QQ_NUMBER="qqNumber";
 private final String USER_WEIXIN_NUMBER="weixinNumber";

 //table friends string
 private final String FRIEND_TABLE_NAME="friends";
 private final String FRIEND_ID="id";
 private final String FRIEND_HOST_ID="hostId";
 private final String FRIEND_GROUP_ID="groupId";
 private final String FRIEND_NAME="friendName";
 private final String FRIEND_ICON_PATH="iconPath";
 private final String FRIEND_BELIVE="beLive";

 //table groups string
 private final String GROUP_TABLE_NAME="groups";
 private final String GROUP_ID="id";
 private final String GROUP_HOST_ID="hostId";
 private final String GROUP_NAME="groupName";

 //table messages string
 private final String MESSAGE_TABLE_NAME="messages";
 private final String MESSAGE_ID="id";
 private final String MESSAGE_MESSAGE="message";
 private final String MESSAGE_FROM_ID="fromId";
 private final String MESSAGE_TO_ID="toId";
 private final String MESSAGE_READED="readed";
 private final String MESSAGE_TYPE="type";
 private final String MESSAGE_TIME="time";

 public ChatInfoManager(Context context){
 mContext = context;
 mDBHelper = ChatDBhelper.getInstance(mContext);

 }
 public void addUser(UserEntity user){

 SQLiteDatabase db = null;
 try {
  db = mDBHelper.getWritableDatabase();
  ContentValues values = new ContentValues();
  values.put(USER_ID,user.getId());
  values.put(USER_COUNT_NUMBER,user.getCountNumber());
  values.put(USER_NIKENAME,user.getNikeName());
  values.put(USER_ICON_PATH,user.getIconPath());
  values.put(USER_PHONE_NUMBER,user.getPhoneNumber());
  values.put(USER_MAIL_ADDR,user.getMailNumber());
  values.put(USER_QQ_NUMBER,user.getQQNumber());
  values.put(USER_WEIXIN_NUMBER,user.getWeixinNumber());
  db.insert(USER_TABLE_NAME, null, values);

 } catch (Exception e) {
  e.printStackTrace();
  // TODO: handle exception
 } finally {
  db.close();
 }
 }

 public UserEntity getUserData(int index){
 UserEntity user= new UserEntity();
 SQLiteDatabase db =null;
 Cursor cursor = null;

 try {
  db = mDBHelper.getReadableDatabase();
  cursor = db.query(USER_TABLE_NAME, null, USER_ID + "=" + index, null, null, null, null);
  if (cursor != null) {
  user.setId(index);
  user.setCountNumber(cursor.getString(cursor.getColumnIndex(USER_COUNT_NUMBER)));
  user.setNikeName(cursor.getString(cursor.getColumnIndex(USER_NIKENAME)));
  user.setIconPath(cursor.getString(cursor.getColumnIndex(USER_ICON_PATH)));
  user.setPhoneNumber(cursor.getString(cursor.getColumnIndex(USER_PHONE_NUMBER)));
  user.setMailNumber(cursor.getString(cursor.getColumnIndex(USER_MAIL_ADDR)));
  user.setQQNumber(cursor.getString(cursor.getColumnIndex(USER_QQ_NUMBER)));
  user.setWeixinNumber(cursor.getString(cursor.getColumnIndex(USER_WEIXIN_NUMBER)));
  } else {
  return null;
  }
 }catch (Exception e){
  e.printStackTrace();
 }finally{
  db.close();
 }
 return user;
 }
 public List<UserEntity> getUserList(){
 List<UserEntity> list =new ArrayList<UserEntity>();
 SQLiteDatabase db =null;
 Cursor cursor=null;

 try{
  db=mDBHelper.getReadableDatabase();
  cursor=db.query(USER_TABLE_NAME,new String[]{USER_ID,USER_COUNT_NUMBER,USER_NIKENAME,USER_ICON_PATH},null,null,null,null,null);
  if(cursor!=null){
  while (cursor.moveToNext()) {
   UserEntity user= new UserEntity();

   user.setId(cursor.getInt(cursor.getColumnIndex(USER_ID)));
   user.setCountNumber(cursor.getString(cursor.getColumnIndex(USER_COUNT_NUMBER)));
   user.setNikeName(cursor.getString(cursor.getColumnIndex(USER_NIKENAME)));
   user.setIconPath(cursor.getString(cursor.getColumnIndex(USER_ICON_PATH)));
   list.add(user);
  }
  }
 }catch(Exception e){
  e.printStackTrace();
 }finally{
  db.close();
 }
 return list;

 }

 public void addFriend(FriendEntity friend){
 SQLiteDatabase db=null;

 try{
  db=mDBHelper.getWritableDatabase();
  if(db==null)return ;

  ContentValues values = new ContentValues();
  values.put(FRIEND_ID,friend.getId());
  values.put(FRIEND_HOST_ID,friend.getHostId());
  values.put(FRIEND_GROUP_ID,friend.getgroupId());
  values.put(FRIEND_NAME,friend.getName());
  values.put(FRIEND_ICON_PATH,friend.getIcnPath());
  values.put(FRIEND_BELIVE,friend.getBeLive());

  db.insert(FRIEND_TABLE_NAME,null,values);
 }catch(Exception e){
  e.printStackTrace();
 }finally {
  db.close();
 }
 }

 public List<FriendListData> getFriendList(){
 SQLiteDatabase db=null;
 List<FriendListData> friendList=new ArrayList<FriendListData>();
 List<GroupEntity> groupList= getGroupList();

 try{
  db = mDBHelper.getWritableDatabase();
  if (db == null) return null;
  for(int i=0;i<groupList.size();i++) {
  FriendListData friendGroup= new FriendListData();
  String groupName = groupList.get(i).getName();
  friendGroup.setGroupName(groupName);

  List<FriendEntity> groupChild=new ArrayList<FriendEntity>();

  Cursor cursor = db.query(FRIEND_TABLE_NAME, null, GROUP_NAME+"="+groupName, null, null, FRIEND_ID + "ASC", null);
  while (cursor.moveToNext()) {
   FriendEntity entity = new FriendEntity();
   entity.setId(cursor.getInt(cursor.getColumnIndex(FRIEND_ID)));
   entity.setgroupId(cursor.getInt(cursor.getColumnIndex(FRIEND_GROUP_ID)));
   entity.setHostId(cursor.getInt(cursor.getColumnIndex(FRIEND_HOST_ID)));
   entity.setName(cursor.getString(cursor.getColumnIndex(FRIEND_NAME)));
   entity.setIcnPath(cursor.getString(cursor.getColumnIndex(FRIEND_ICON_PATH)));
   String beLive = cursor.getString(cursor.getColumnIndex(FRIEND_BELIVE));
   entity.setBeLive(beLive.equals("1"));
   groupChild.add(entity);
  }
  friendGroup.setFriendChildList(groupChild);
  friendList.add(friendGroup);
  }

 }catch(Exception e){
  e.printStackTrace();
 }finally {
  db.close();
 }
 return friendList;
 }

 public List<GroupEntity> getGroupList(){
 List<GroupEntity> groupList = null;
 SQLiteDatabase db=null;

 try{
  db= mDBHelper.getReadableDatabase();
  Cursor cursor =db.query(GROUP_TABLE_NAME,null,null,null,null,null,null);
  while(cursor.moveToNext()){
  GroupEntity entity =new GroupEntity();
  entity.setId(cursor.getInt(cursor.getColumnIndex(GROUP_ID)));
  entity.setHostId(cursor.getInt(cursor.getColumnIndex(GROUP_HOST_ID)));
  entity.setGroupName(cursor.getString(cursor.getColumnIndex(GROUP_NAME)));

  groupList.add(entity);
  }
 }catch(Exception e){
  e.printStackTrace();
 }finally{
  db.close();
 }
 return groupList;
 }

 public void addGroupMember(GroupEntity group){
 SQLiteDatabase db=null;

 try{
  db=mDBHelper.getWritableDatabase();
  ContentValues values=new ContentValues();
  values.put(GROUP_ID,group.getId());
  values.put(GROUP_HOST_ID,group.getHostId());
  values.put(GROUP_NAME,group.getName());
  db.insert(GROUP_TABLE_NAME,null,values);
 }catch(Exception e){
  e.printStackTrace();
 }finally {
  db.close();
 }
 }

 public List<MessageEntity> getMessageList(String loadId){
 List<MessageEntity> list = new ArrayList<MessageEntity>();
 SQLiteDatabase db=null;
 Cursor cursor=null;
 try{
  db=mDBHelper.getReadableDatabase();

  cursor=db.query(MESSAGE_TABLE_NAME,null,
   MESSAGE_FROM_ID+"=?,"+MESSAGE_TO_ID+"=?",
   new String[]{loadId},
   null,
   loadId,MESSAGE_TIME+"DES",null);
  while(cursor.moveToNext()){
  MessageEntity entity =new MessageEntity();
  entity.setId(cursor.getInt(cursor.getColumnIndex(MESSAGE_ID)));
  entity.setFromId(cursor.getInt(cursor.getColumnIndex(MESSAGE_FROM_ID)));
  entity.setToId(cursor.getInt(cursor.getColumnIndex(MESSAGE_TO_ID)));
  entity.setMessageStr(cursor.getString(cursor.getColumnIndex(MESSAGE_MESSAGE)));
  entity.setType(cursor.getString(cursor.getColumnIndex(MESSAGE_TYPE)));
  entity.setTime(cursor.getString(cursor.getColumnIndex(MESSAGE_TIME)));

  list.add(entity);
  }

 }catch (Exception e){
  e.printStackTrace();
 }finally{
  db.close();
 }

 return list;
 }

 public void setMessageItem(MessageEntity message){
 SQLiteDatabase db=null;

 try{
  db=mDBHelper.getWritableDatabase();
  if(db==null)return;

  ContentValues values = new ContentValues();
  values.put(MESSAGE_ID,message.getId());
  values.put(MESSAGE_FROM_ID,message.getFromId());
  values.put(MESSAGE_TO_ID,message.getToId());
  values.put(MESSAGE_MESSAGE,message.getMessageStr());
  values.put(MESSAGE_TYPE,message.getType());
  values.put(MESSAGE_TIME,message.getTime());
  db.insert(MESSAGE_TABLE_NAME,null,values);
 }catch(Exception e){
  e.printStackTrace();
 }finally {
  db.close();
 }
 }

}

源码下载地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • android Socket实现简单聊天功能以及文件传输

    干程序是一件枯燥重复的事,每当感到内心浮躁的时候,我就会找小说来看.我从小就喜爱看武侠小说,一直有着武侠梦.从金庸,古龙,梁羽生系列到凤歌(昆仑),孙晓(英雄志)以及萧鼎的(诛仙)让我领略着不一样的江湖. 如果你有好看的武侠系列小说,给我留言哦.题外话就扯这么多了,接着还是上技术. 看看今天实现的功能效果图: 可以这里使用多台手机进行通讯,我采用的服务器发送消息. 是不是只有发送消息,有些显得太单调了.好,在发送消息的基础上增加文件传输.后期会增加视频,音频的传输,增加表情包.那一起来看看图文消

  • 安卓(Android)聊天机器人实现代码分享

    今天看到一个ios写的图灵机器人,直接去官网(http://www.tuling123.com/openapi/)看了下API接入,太简单了,就一个get请求~于是乎,写了一个Android版本的机器人,没什么技术含量,但是挺好玩的~刚好昨晚看了自己喜欢的秦时明月,嘿嘿,小貔貅,就是我的机器人宠物啦~ 这是一个安卓智能聊天机器人的源码,采用了仿微信的风格设计,调用的是图灵机器人的API,能够实现智能聊天.讲故事.讲笑话.查天气.查公交等丰富的功能. 先给大家展示效果图: 下面是代码片段,想要源码

  • android Socket实现简单聊天小程序

    android Socket实现简单聊天小程序,供大家参考,具体内容如下 服务器端: package org.hwq.echo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public cla

  • Android中基于XMPP协议实现IM聊天程序与多人聊天室

    简单的IM聊天程序 由于项目需要做一个基于XMPP协议的Android通讯软件.故开始研究XMPP. XMPP协议采用的是客户端-服务器架构,所有从一个客户端发到另一个客户端的消息和数据都必须经过XMPP服务器转发,而且支持服务器间DNS的路由,也就是说可以构建服务器集群,使不同的 服务器下的客户端也可以通信,XMPP的前身是一个开源组织制定的网络通信协议--Jabber,XMPP的核心是在网络上分片段发送XML流的协议,这个协议是XMPP的即时通讯指令的传递手段.       为了防止服务器间

  • Android蓝牙通信聊天实现发送和接受功能

    很不错的蓝牙通信demo实现发送和接受功能,就用了两个类就实现了,具体内容如下 说下思路把 主要有两个类 主界面类 和 蓝牙聊天服务类 . 首先创建线程 实际上就是创建BluetoothChatService() (蓝牙聊天服务类) 这个时候把handler 传过去 这样就可以操作UI 界面了,在线程中不断轮询读取蓝牙消息,当主界面点击发送按钮时 调用BluetoothChatService 的发送方法write 方法,这里的write 方法 使用了handler 发送消息,在主界面显示,另一个

  • Android基于socket实现的简单C/S聊天通信功能

    本文实例讲述了Android基于socket实现的简单C/S聊天通信功能.分享给大家供大家参考,具体如下: 主要想法:在客户端上发送一条信息,在后台开辟一个线程充当服务端,收到消息就立即回馈给客户端. 第一步:创建一个继续Activity的SocketClientActity类,包为com.pku.net 编写布局文件socketclient.xml,代码如下: <?xml version="1.0" encoding="utf-8"?> <Lin

  • Android 基于Socket的聊天室实例

    Socket是TCP/IP协议上的一种通信,在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路.一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信. Client A  发信息给 Client B ,  A的信息首先发送信息到服务器Server ,Server接受到信息后再把A的信息广播发送给所有的Clients 首先我们要在服务器建立一个ServerSocket ,ServerSocket对象用于监听来自客户端的Socket连接,如果没有连接,它将一直处于等待

  • android socket聊天室功能实现

    前提概要 笔者很久之前其实就已经学习过了socket,当然也是用socket做过了聊天室,但是觉得此知识点比较一般,并无特别难的技术点,于是也并未深究. 然而近期一个项目中对socket的使用却让笔者感觉socket强大无比,可以实现诸多功能. 个人Socket体验 项目主要有关智能家居,需要实现多台手机同时对灯进行操作(开或者关),主要需要实现以下几点: 1.进入界面时获取所有灯的状态. 2.一台手机改变了灯的状态,其他的手机上可以有所显示. 3.硬件上改变了灯的状态(手动开关灯),所有手机上

  • Android 基于Socket的聊天应用实例(二)

    前言 很久没写BLOG了,之前在写Android聊天室的时候答应过要写一个客户(好友)之间的聊天demo,Android 基于Socket的聊天室已经实现了通过Socket广播形式的通信功能. 以下是我写的一个类似现在多数聊天软件的冒泡聊天APP.全部功能都是自己的想法,对于现在市面上成功的例子是怎么实现的,我还不了解.所以读者可只做参考学习,也可以分享您的案例给我. 功能 一对一聊天,非聊天室 好友列表 好友在线,离线状态(实时更新) 冒泡实时聊天窗口 发送离线信息 基本原理 之前的聊天室原理

  • Android聊天工具基于socket实现

    特简单, 没有数据库, 还没有处理各种异常. 登录:输入用户名点击的登录即可. 发送消息: 特定格式->toUser:message 1. 服务器:保存在线用户 public class Online { private static Online mOnline = null; private LinkedHashMap<String, Socket> mOnlines = new LinkedHashMap<String, Socket>(); private Onlin

随机推荐