Android Network programming-thread and Handler (iii)

Source: Internet
Author: User

Client: thread and handler

Mainactivity.java

package com.example.justyu.login_demo;import android.app.activity;import  android.app.progressdialog;import android.os.handler;import android.os.message;import  Android.support.v7.app.actionbaractivity;import android.os.bundle;import android.view.view;import  android.widget.Button;import android.widget.EditText;import android.widget.Toast;import  Com.example.justyu.login_demo. Sercice.serviceexception;import com.example.justyu.login_demo. Sercice.userservice;import com.example.justyu.login_demo. sercice.userserviceimp;import java.lang.ref.weakreference;public class mainactivity  extends actionbaractivity implements view.onclicklistener{    private  edittext loginname,loginpassword;    private button loginbutton;     private  static final int login_success=1;     private&Nbsp; static final string  msg_login_error= "Login failed! ";     private  static final string  msg_login_success=" landed successfully! ";     public   static final string  msg_login_failed=" Login name or password error!! ";     private static progressdialog dialog;    //interface Implementation of   &NBSP;&NBSP;&NBSP;&NBSP;PRIVATE&NBSP;USERSERVICE&NBSP;USERSERVICE=NEW&NBSP;USERSERVICEIMP ();      @Override     protected void oncreate (bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         setcontentview (R.layout.activity_main);         loginname= (EditText) This.findviewbyid (r.id.edittext);         loginpassword= (EditText) This.findviewbyid (R.ID.EDITTEXT2);         loginbutton= (Button) This.findviewbyid ( R.id.button);         loginbutton.setonclicklistener (this);     }   //Click on the login button   after the action      @Override      Public void onclick (VIEW&NBSP;V)  {       final string  name=loginname.gettext (). toString (). Trim ();        final string  password=loginpassword.gettext (). toString (). Trim ();         /**          *  Login Verification           */        if (dialog==null) {             dialog=new progressdialog (mainactivity.this);             dialog.settitle ("Wait");             dialog.setmessage ("Log in .....") ");             dialog.setcancelable (True);             dialog.show ();         }        //Sub-thread          thread thread=new thread (new runnable ()  {              @Override              public void run ()  {                 try{                   userservice.userlogin (Name,password);                    //Send Message                     Handler.sendemptymessage (login_success);             }//Business logic Anomaly                  catch  (serviceexception e) {                     e.printstacktrace ();                     //Send Message                      message  msg=new message ();                     bundle data=new bundle ();                     data.putserializable ("Error_msg", E.getMessage ());                      msg.setdata (data);                     //passed the 0           .          handler.sendmessage (msg);                 }                 catch  (exception e) {                      E.printstacktrace ();                     //sending messages                      message msg=new message ();                     bundle data=new bundle ();                      Data.putserializable ("Error_msg", Msg_login_error);                     msg.setdata (data);                     //passed the 0  .                    Handler.sendmessage (msg);                }             }         });         thread.start ();    }     private void showtip (STRING&NBSP;STR) {         Toast.maketext (This,str,toast.length_short). Show ();    }    /**      *  communication between the main thread and the sub-threads  Handler     */    private static class myhandler extends handler{         private final WeakReference<Activity> myActivity;         public myhandler (mainactivity activity) {             myActivity=new WeakReference<Activity> (Activity);         }         @Override          public void handlemessage (message msg)  {             //can quote Mainactivity's              //  method Reference ((mainactivity) Myactivity.get ()) .             if (dialog!=null) {                 dialog.dismiss ();             }            int flag=msg.what;             switch  (flag) {                 case 0:                     string errormsg= (String) Msg.getdata (). Getserializable ("error_msg");                      ((mainactivity) Myactivity.get ()). Showtip (ERRORMSG);                      break;                 case login_success:                      ((mainactivity) Myactivity.get ()). Showtip (msg_login_success);                      break;                default:                     break;             }        }     }    private myhandler handler=new myhandler (this);}

Userservice.java

Package Com.example.justyu.login_demo. Sercice;


/**

* Created by Justyu on 2015/4/9.

*/

Public interface UserService {

public void Userlogin (String loginname,string Loginpassword) throws Exception;



}


Userserviceimp.java

Package com.example.justyu.login_demo. Sercice;import android.util.log;import com.example.justyu.login_demo. mainactivity;/** * created by justyu on 2015/4/9. */public class  userserviceimp implements userservice {     @Override      public void userlogin (String loginname, string loginpassword)  throws  EXCEPTION&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.D ("Yu", LoginName);     &NBSP;&NBSP;&NBSP;&NBSP;LOG.D ("Yu", Loginpassword);         thread.sleep         //and server-side interaction          if (Loginname.equals ("1") &&loginpassword.equals ("1") {         }else{         throw  new serviceexception ( Mainactivity.msg_login_failed);         }    }} 


Android Network programming-thread and Handler (iii)

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.