Android instant messaging Development Notes (1) Introduction ~ User Logon, automatic logon, and logout

Source: Internet
Author: User

If you struggle to survive in the code, you still need to write down useful things and taste them later. Every time I write a demo, I will always find my own shortcomings. Maybe I still don't know more norms and common sense. I learned one today and recorded it. Sometimes values need to be transferred during activity jump. Generally, intent can be used for this purpose. However, if a custom type variable is encountered, it cannot be passed. Yesterday I asked a huge question, today, I wrote the code and suddenly became very clear. The correct usage of android's getapplicationcontext is like this! Normally, there are some functions, such as Toast, which can be regarded as a rising position this time. When an activity obtains a context object, it actually uses the activity. this is fine. The returned result is the context object of the activity, and the getapplicationcontext returns the context object of the Application. The source of this function is the eldest brother-Application class of the entire app. The context object composed of this class has the same length of life as the app. Therefore, you can customize a class to inherit this class and store some variables that need to be used throughout the app. For example: // Declare public ApplicationHelper appState; // appState = (ApplicationHelper) this in the function. getApplicationContext (); public class ApplicationHelper extends Application {public ApplicationHelper getApplicationContext () {return (ApplicationHelper) super. getApplicationContext ();} // various variables and functions to be stored, various writes ~} Next, register the information in Manifest. xml. You only need to add android: name = "***. ApplicationHelper" to the application. In this way, a lot of value transfer problems will be solved. Well, the question below is that I want to develop the android IM by my small team. It is indeed very challenging. For cainiao like me, I can only explore it slowly. The environment used is openfire, And the smack package is introduced, which is still very powerful. There are a lot of introductions on the Internet, so I will not discuss it in detail. Build a good environment, introduce a good package, and start coding honestly ~ The first part is about implementing the user login, automatic login, and logout functions. The function is very simple. It mainly involves the activity of these welcome interfaces. First, a welcome image is displayed, the background reads the sqlite database. If user information is not read, another xml file (login form) is dynamically loaded for user input. Enter the information and click log on to establish a connection with the openfire server for verification. If the login is successful, the user information will be saved to the database. If you can read the user information at the beginning, the login window will not be loaded and you can use this information to complete automatic logon. After successful login, the user interface is displayed. Then, you can log out of the user, click "" ", delete the user information from the database, set the user status to offline, and disconnect the client from the server. The current directory structure is like this... The name is too watery, and there will be more classes in the future. It must be silly to find the pace of things... Currently, the six packages include the adapter, activity, application, server code, some tool classes, and junitest test classes. Sqlite has been learned before, and SQL operations in the project have been encapsulated, which is more convenient. Today, it is mainly related to writing smack. I put all the main variables in the application class for reference. First, configure XMPPservices to copy the code public XMPPConnection XMPPservices () {if (connection = null) {ConnectionConfiguration config; config = new ConnectionConfiguration (IP, PORT ); /** enable security verification */config. setSASLAuthenticationEnabled (false); config. setSecurityMode (ConnectionConfiguration. securityMode. disabled);/** whether to enable debugging * // config. setDebuggerEnabled (true); return connection = new XMPPConnection (config);} else return connection;} copy the code about some XMPP connection, login, and logout methods, the copied code public class ClientConService {XMPPConnection connection; public ClientConService () {} public ClientConService (XMPPConnection connection) {this. connection = connection;} public boolean login (String user, String password) {try {/** establish a connection */connection. connect ();/** log on */connection. login (user, password); return true;} catch (Exception e) {e. printStackTrace (); return false ;}} public boolean disconnectAccount () {try {// here, you need to change the login status to "offline" before disconnecting, otherwise, Presence presence = new Presence (Presence. type. unavailable); connection. sendPacket (presence); connection. disconnect (); return true;} catch (Exception e) {e. printStackTrace (); return false ;}}}

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.