The example of this article is about the method of dealing with the thread in Android. Share to everyone for your reference. The specific methods are as follows:
I now register with a user feature
1. Use ProgressDialog to set the current page to be not operable (leave the return key to exit ProgressDialog)
2. Execute data submission and return with one thread Clientthread
Question: In view of the ProgressDialog operation, the return key operation, should terminate the clientthread operation.
The code is as follows, interested friends can refer to the run, in order to come up with a better solution.
Copy Code code as follows:
Btn_register.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
if (Dobeforesubmit ()) {
try {
LOG.V (TAG, "Clientthread prepare01 ...) : "+thread.interrupted ());
Clienthread=new Thread () {
public void Run () {
try {
LOG.V (TAG, "Clientthread prepare02 ...) : "+thread.interrupted ());
while (! Thread.interrupted ()) {
LOG.V (TAG, "clientthread starting ...) ");
Meapicall meapicall=new Meapicallimpl ();
Me me=new Me ();
Me.setmeemail (Et_register_account.gettext (). toString (). Trim ());
Me.setmepwd (Et_register_pwd.gettext (). toString (). Trim ());
Me.setmename (Et_register_nickname.gettext (). toString (). Trim ());
LOG.V (TAG, "Clientthread data Loaded ... ");
Meapicall.register (Me);
LOG.V (TAG, "Clientthread callback ...) ");
Handler.sendemptymessage (AppConfig.Register.SUCCESS_MSG);
Clienthread.interrupt (); Interrupt Thread in
}
catch (Interruptedexception e) {
Todo:handle exception
LOG.V (TAG, "Clientthread is Interrupt");
}catch (Exception e) {
TODO auto-generated Catch block
LOG.V (TAG, "Exception:" +e.getmessage ());
Handler.sendemptymessage (AppConfig.Register.FAIL_OTHER_MSG);
E.printstacktrace ();
}
};
};
LOG.V (TAG, "ProgressDialog prepare01 ...) :");
Progressdialog=progressdialog.show (Register.this,
"",
"Getting ...",
True
True
New Dialoginterface.oncancellistener () {
@Override
public void OnCancel (Dialoginterface dialog) {
TODO auto-generated Method Stub
try {
Clienthread.interrupt ();
LOG.V (TAG, "Interrupt is success!");
catch (Exception e) {
TODO auto-generated Catch block
LOG.V (TAG, "Interrupt is fail!");
E.printstacktrace ();
}
}
}
);
LOG.V (TAG, "Clientthread prepareing_end ...) ");
Clienthread.start ();
LOG.V (TAG, "Clientthread prepareing_end ...) ");
catch (Exception e) {
TODO auto-generated Catch block
LOG.V (TAG, e.getmessage () + "00");
E.printstacktrace ();
}
}
}
});
I hope this article will help you with your Android program.