Common methods for Android application development Encapsulation (i)

Source: Internet
Author: User

In the process of Android development, there are a lot of things are often used, in order to improve efficiency, the usual methods to make a record.

1. In network programming, if you have not established a socket to use send write, there will be an exception, the package is no problem (if you send byte[] type to change the parameter type):

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. When receiving a picture message, usually receive the size first, then receive the content, and the content may be received several times, we can encapsulate such a class:

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; ///The remaining bytes are less than Max, then the remaining bytes are assigned to Max
}


try {
Hasread = Hasread + bin.read (image, Hasread, Max); bytes read//accumulated
} catch (IOException e) {
E.printstacktrace ();
}


if (Hasread = = size) {
Break
}
}
return image;
}
}

Common methods for Android application development Encapsulation (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.