First of all, I wish you a happy New year, bloggers here to worship the million! And immediately to the Lantern Festival, Shun wish you all Happy Lantern Festival!
Last time we talked about registration and login coding skills, this time we'll talk about the skill of adding friends.
"Search Users":
XMPP protocol provides us with a perfect friend search function, and through the Asmack library, can easily realize the fuzzy search function. To ensure the versatility of the application. When searching, we'd better do it as follows:
Usersearchmanager USM = new Usersearchmanager (xmppconnection); Form searchform = Usm.getsearchform ("search." + xmppconnection.getservicename ()); Form answerform = Searchform.createanswerform (); Answerform.setanswer ("Username", true); Answerform.setanswer (" Search ", searchcontent); Reporteddata data = Usm.getsearchresults (Answerform, "search." + xmppconnection.getservicename ());d ata.getrows ();
Note: Because we need to use "search." +[server name] as a parameter to make our app more generic. Instead, the Xmppconnection object provides the server name, so set the above parameter to:
"Search." + xmppconnection.getservicename ()
The need for user lookups on any XMPP server is possible.
"About Avatar":
Since our program eventually runs on Android devices, we want to control the memory more carefully in case of memory overflow. I set the upload size of the avatar in the program to 200*200,80% compression quality, PNG format. and put it in the database as a BLOB type field to save, right when cached. Of course, after each login will contact the server to get the latest avatar and friends information.
Key picture processing at this:
/** * Converts the image file under the development path to a specified long-width byte data * * @param bitmappath * @param width * @param height * @return */public static Bi TMap Resizebitmap (String bitmappath, int width, int height) {Options options = new options (); options.injustdecodebounds = True Bitmapfactory.decodefile (Bitmappath, options); options.insamplesize = Calcsamplesize (options, width, height); o Ptions.injustdecodebounds = False;return bitmapfactory.decodefile (bitmappath, options);} Bitmap byte[]public Static byte[] Bitmaptobytes (Bitmap Bitmap) {Bytearrayoutputstream Bytearrayoutputstream = new Bytearrayoutputstream (); Bitmap.compress (Bitmap.CompressFormat.PNG, bytearrayoutputstream); return Bytearrayoutputstream.tobytearray ();} calculate samplesizeprivate static int calcsamplesize (options options, int width, int height) {int returndata = 1;if (options.o Utheight > Height | | Options.outwidth > width) {int heightratio = Math.Round ((float) options.outheight/(float) height); int widthRatio = Ma Th.round (float) options.outwidTh/(float) width); returndata = heightratio > WidthRatio? Heightratio:widthratio;} return returndata;}Finally remind everybody a sentence: Do not forget in OnDestroy () recycle use of bitmap yo! In addition, when reading the user vcard, it is important to remember to optimize the display of the picture, otherwise, the PC-side set a large picture as the avatar, mobile phone one side of a login may be directly on the oom.
"Database Configuration":
Here is a reminder to the reader: All values stored in the database must be transcoded! On the one hand for data security, in fact, more on the other hand is to better program robustness. Imagine: If the user name of an account is group, sort, order, directly dumbfounded.
My method is to just take the string byte[], converted to 16 binary, as a string into the database. This avoids the problem that the database operation error crashes. Of course, for critical chat log data, you can also choose to encrypt the storage and then decrypt the view.
"Keyboard Optimizer":
Android:windowsoftinputmode= "Statehidden|adjustresize"
Add the above configuration to the Activity node, on the one hand, you can adjust the layout when the keyboard appears, on the other hand, automatically hide the keyboard. is a good user experience, much better than the default display strategy.
Development notes-The XMPP-based Android Instant messaging app (iii)