本篇文章给大家分享的是有关Android中怎么使用Thread和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 static final String MSG_LOGIN_ERROR="登陆失败!"; private static final String MSG_LOGIN_SUCCESS="登陆成功!"; public static final String msg_login_failed="登录名或密码错误!!"; private static ProgressDialog dialog; //接口 的实现 private UserService userService=new 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); } //点击登陆按钮 后进行的操作 @Override public void onClick(View v) { final String Name=loginName.getText().toString().trim(); final String Password=loginPassword.getText().toString().trim(); /** * 登陆验证 */ if(dialog==null){ dialog=new ProgressDialog(MainActivity.this); dialog.setTitle("等待"); dialog.setMessage("登录中。。。。。"); dialog.setCancelable(true); dialog.show(); } //副线程 Thread thread=new Thread(new Runnable() { @Override public void run() { try{ userService.userLogin(Name,Password); //发送消息 handler.sendEmptyMessage(login_success); }//业务逻辑异常 catch (ServiceException e){ e.printStackTrace(); //发送消息 Message msg=new Message(); Bundle data=new Bundle(); data.putSerializable("ERROR_MSG",e.getMessage()); msg.setData(data); //传过去的是0 handler.sendMessage(msg); } catch (Exception e){ e.printStackTrace(); //发送消息 Message msg=new Message(); Bundle data=new Bundle(); data.putSerializable("ERROR_MSG",MSG_LOGIN_ERROR); msg.setData(data); //传过去的是0 handler.sendMessage(msg); } } }); thread.start(); } private void showTip(String str){ Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); } /** * 主线程与副线程之间的通信 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) { //可以引用MainActivity的 // 方法引用((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 { Log.d("Yu",loginName); Log.d("Yu",loginPassword); Thread.sleep(3000); //与服务器端交互 if(loginName.equals("1")&&loginPassword.equals("1")){ }else{ throw new ServiceException(MainActivity.msg_login_failed); } } }
以上就是Android中怎么使用Thread和Handler实现网络编程,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。