星空网 > 软件开发 > 操作系统

Android应用开发常用方法封装(一)

Android开发过程中,有很多东西都是常常用到的,为了提高效率,将常用的方法做个记录。

 

1.在网路编程中,如果还没建立套接字就使用发送write,会出现异常,封装后没问题了(若发送byte[]型自己更改参数类型):

public static boolean sendMsg(OutputStream outs,String str){


  boolean isConnect=false;

  if(outs!=null)             
  try {
    outs.write(str.getBytes());
    outs.flush();
    isConnect=true;
  } catch (IOException e) {
    isConnect=false;
  }

  return isConnect;
}

 

2.在接收图片消息时,通常先接收大小,再接收内容,而且内容可能分多次接收,我们可以封装这样一个类:

public class MyUtil {
  public static byte[] read(BufferedInputStream bin, int size, int max) {
    byte[] image = new byte[size];
    int hasRead = 0;
    while (true) {
      if (max > size - hasRead) {
        max = size - hasRead;    //剩下的字节不及max,则剩下的字节赋值为max
      }


      try {
        hasRead = hasRead + bin.read(image, hasRead, max);  //累计读取的字节
       } catch (IOException e) {
        e.printStackTrace();
      }


      if (hasRead == size) {
        break;
      }
    }
    return image;
  }
}

 




原标题:Android应用开发常用方法封装(一)

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

lazada安卓下载:https://www.goluckyvip.com/tag/85716.html
lazada下载安卓:https://www.goluckyvip.com/tag/85717.html
泰版lazada下载:https://www.goluckyvip.com/tag/85718.html
lazada买家端下载:https://www.goluckyvip.com/tag/85719.html
lazada下载买家版:https://www.goluckyvip.com/tag/85720.html
lazada商家版下载:https://www.goluckyvip.com/tag/85721.html
Twitter和Meta的付费认证服务值得购买吗?:https://www.kjdsnews.com/a/1836404.html
零售晚报:携程发布318成绩单 近800万商家GMV增长101% :https://www.kjdsnews.com/a/1836405.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流