Development notes-Android instant messaging APP Based on XMPP (2) and xmppandroid

Source: Internet
Author: User

Development notes-Android instant messaging APP Based on XMPP (2) and xmppandroid

After a few days, the application login and registration sections have been improved. Of course, this is just a personal feeling.

Today,All are dry goods!

First,No large piece of code; Second,Not found on the InternetFinally, let youSave development time! This is why the second serialization will be sent several days later.

Since there are no large pieces of code, the General login and registration processes are not mentioned here. Baidu Google searches a lot and is basically usable. Some notes

[1] background services:

The official recommendation is to use background services such as START_STICKY. The official statement on why to use this service is clear:


This is consistent with the explanation on the Android Developer website. It should be noted that in order to take care of the Bug of version 4.4.2, some processing needs to be done in the program.

Reference from: https://github.com/Flowdalic/asmack/wiki/Should-applications-using-aSmack-use-foreground-Services%3F above

[2] reconnection:

The new version of aSamck4.0.6 can be reconnected by disconnection, which is a little different from the previous version.

[3] user login judgment:

You can use the XMPPConnection object to obtain the connection status. The details are as follows:

XmppConnection. isConnected (): Boolean value indicates whether to connect to the server (Note: users may not log on to the server at this time );

XmppConnection. isAuthenticated (): A boolean value indicates whether the logon is successful (that is, the user name and password have been verified). In this case, the logon is successful and the connection is established with the server.

[4] interaction with the server:

Use a thread, Handler. Remember! Even in the Service.

[5] debugging tips:

To view the interaction content (xml) with the server, you can set ConnectionConfiguration before connecting to the server:

SmackConfiguration.DEBUG_ENABLED = true;connectionConfiguration = new ConnectionConfiguration(PrefUtil.getServerIP(context), PrefUtil.getServerPort(context));connectionConfiguration.setReconnectionAllowed(true);connectionConfiguration.setSendPresence(true);connectionConfiguration.setRosterLoadedAtLogin(true);connectionConfiguration.setSecurityMode(SecurityMode.disabled);connectionConfiguration.setDebuggerEnabled(BuildConfig.DEBUG);xmppConnection = new XMPPTCPConnection(connectionConfiguration);xmppConnection.connect();
ConnectionConfiguration is the ConnectionConfiguration object, and PrefUtil is the preference tool class.

[6] cannot be reached during registration?

In version 4.0.6, The aSmack package seems to be unable to setUsername and setPassword for the Registry class used for Registration. What should I do? Paste the code below:

Registration registration = new Registration();registration.setType(IQ.Type.SET);registration.setTo(xmppConnection.getServiceName());Map<String, String> attributes = new HashMap<String, String>();attributes.put("username", username);attributes.put("password", password);registration.setAttributes(attributes);PacketFilter filter = new AndFilter(new PacketIDFilter(registration.getPacketID()), new PacketTypeFilter(IQ.class));PacketCollector collector = xmppConnection.createPacketCollector(filter);xmppConnection.sendPacket(registration);IQ result = (IQ) collector.nextResult(xmppConnection.getPacketReplyTimeout());collector.cancel();if (result == null || result.getType() == IQ.Type.ERROR) {DebugUtil.Log("An error occured during registion, maybe the same username");return false;} else {DebugUtil.Log("Registion has OK!");return true;}
As shown above, use HashMap to store the user name and password, and use it as the Attributes. set to the registration object and the registration is successful.

[7] switch users?

When the application needs to switch to the current login account, it is necessary to call the disconnect () method in XMPPConnection to disconnect from the server (it seems that there is no separate account cancellation method), and then connect () Again (), then login ().


Finally, let's talk about the project progress:

Currently, the logon, registration, and personal information modification functions are completed. You can log on to multiple accounts, save the passwords of each account (if required), and log on automatically. Because the application provides manual server configuration, you can switch to another server before registering a user; the preceding server configurations can be modified on the logon interface and saved to the preference. When the application is started, the system checks whether the configurations have been configured. The personal information is modified just now. You have manually tested the settings, however, since no strict tests have been conducted, we should not mention them first.

Open Source Path: https: // wh1990xiao2005@bitbucket.org/wh1990xiao2005/anychat. git



Related Article

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.