基于IntentService的Android登录完整示例

2024-01-18 17:18

本文主要是介绍基于IntentService的Android登录完整示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.后台使用简单的servlet,支持GET或POST。这个servlet最终返回给前台一个字符串flag,值是true或false,表示登录是否成功。

/**
* Copyright(C) 2014
*
* 模块名称:     
* 子模块名称:   
*
* 备注:
*
* 修改历史:
* 2014-1-26	1.0		liwei5946@gmail.com		新建
*/
package cn.edu.hbcit.minicms.controller;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import cn.edu.hbcit.minicms.dao.Login;
import cn.edu.hbcit.minicms.util.PasswordEncodeBean;/*** 登录控制类* 简要说明:* @author liwei5946@gmail.com* @version 1.00  2014-1-26下午07:21:13	新建*/public class LoginServlet extends HttpServlet {protected final static Logger log = Logger.getLogger(LoginServlet.class.getName());/*** Constructor of the object.*/public LoginServlet() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();Boolean flag = false;Login login = new Login();PasswordEncodeBean encode = new PasswordEncodeBean();String userName = request.getParameter("un");String password = request.getParameter("pw");flag = login.login(userName, encode.MD5Encode(password));log.debug("登录结果是:" + flag);out.print(flag);out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}

2.在ADT中新建一个android工程,建立两个Activity,分别作为登录界面和登录成功界面。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><TextViewandroid:id="@+id/mainContent"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="TextView" /><EditTextandroid:id="@+id/userName"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10" android:inputType="text" />"<EditTextandroid:id="@+id/password"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10"android:inputType="textPassword" /><Buttonandroid:id="@+id/myButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Submit" /></LinearLayout>
activity_result.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".ResultActivity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /></RelativeLayout>

3.HTTP的访问公共类,用于处理GET和POST请求

package edu.hbcit.testandroid.util;import java.util.ArrayList;
import java.util.List;
import java.util.Map;import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;import android.util.Log;public class HttpUtil
{// 创建HttpClient对象public static HttpClient httpClient = new DefaultHttpClient();public static final String BASE_URL = "http://192.168.1.101:8080/server/"; //注意不要用127.0.0.1/*** * @param url 发送请求的URL* @return 服务器响应字符串* @throws Exception*/public static String getRequest(String url)throws Exception{// 创建HttpGet对象。HttpGet get = new HttpGet(url);// 发送GET请求HttpResponse httpResponse = httpClient.execute(get);// 如果服务器成功地返回响应if (httpResponse.getStatusLine().getStatusCode() == 200){// 获取服务器响应字符串String result = EntityUtils.toString(httpResponse.getEntity());return result;}else{Log.d("服务器响应代码", (new Integer(httpResponse.getStatusLine().getStatusCode())).toString());return null;}}/*** * @param url 发送请求的URL* @param params 请求参数* @return 服务器响应字符串* @throws Exception*/public static String postRequest(String url, Map<String ,String> rawParams)throws Exception{// 创建HttpPost对象。HttpPost post = new HttpPost(url);// 如果传递参数个数比较多的话可以对传递的参数进行封装List<NameValuePair> params = new ArrayList<NameValuePair>();for(String key : rawParams.keySet()){//封装请求参数params.add(new BasicNameValuePair(key , rawParams.get(key)));}// 设置请求参数post.setEntity(new UrlEncodedFormEntity(params, "gbk"));// 发送POST请求HttpResponse httpResponse = httpClient.execute(post);// 如果服务器成功地返回响应if (httpResponse.getStatusLine().getStatusCode() == 200){// 获取服务器响应字符串String result = EntityUtils.toString(httpResponse.getEntity());return result;}return null;}
}

4.IntentService服务,用于在后台以队列方式处理耗时操作。

package edu.hbcit.testandroid.service;import java.util.HashMap;import edu.hbcit.testandroid.util.HttpUtil;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;public class ConnectService extends IntentService {private static final String ACTION_RECV_MSG = "edu.hbcit.testandroid.intent.action.RECEIVE_MESSAGE";public ConnectService() {super("TestIntentService");// TODO Auto-generated constructor stub}@Overrideprotected void onHandleIntent(Intent intent) {// TODO Auto-generated method stub/*** 经测试,IntentService里面是可以进行耗时的操作的 * IntentService使用队列的方式将请求的Intent加入队列,* 然后开启一个worker thread(线程)来处理队列中的Intent  * 对于异步的startService请求,IntentService会处理完成一个之后再处理第二个  */Boolean flag = false;//通过intent获取主线程传来的用户名和密码字符串String username = intent.getStringExtra("username");String password = intent.getStringExtra("password");flag = doLogin(username, password);Log.d("登录结果", flag.toString());Intent broadcastIntent = new Intent();broadcastIntent.setAction(ACTION_RECV_MSG);  broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);  broadcastIntent.putExtra("result", flag.toString());sendBroadcast(broadcastIntent);}// 定义发送请求的方法private Boolean doLogin(String username, String password){String strFlag = "";// 使用Map封装请求参数HashMap<String, String> map = new HashMap<String, String>();map.put("un", username);map.put("pw", password);// 定义发送请求的URL
// 		String url = HttpUtil.BASE_URL + "LoginServlet?un=" + username + "&pw=" + password;  //GET方式String url = HttpUtil.BASE_URL + "LoginServlet"; //POST方式Log.d("url", url);Log.d("username", username);Log.d("password", password);try {// 发送请求strFlag = HttpUtil.postRequest(url, map);  //POST方式
// 			strFlag = HttpUtil.getRequest(url);  //GET方式Log.d("服务器返回值", strFlag);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}if(strFlag.trim().equals("true")){return true;}else{return false;}}}

5.在AndroidManifest.xml中注册IntentService。注意uses-permission节点,为程序开启访问网络的权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="edu.hbcit.testandroid"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="9"android:targetSdkVersion="14" /><uses-permission android:name="android.permission.INTERNET"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="edu.hbcit.testandroid.activity.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name="edu.hbcit.testandroid.activity.ResultActivity"android:label="@string/title_activity_result" ></activity><service android:name="edu.hbcit.testandroid.service.ConnectService"></service></application></manifest>

6.重头戏Activity登场——需要注意的几个要点

  1. 按钮监听事件中,使用Intent将要传递的值传给service。有点类似于Java Web中的session。
  2. 接收广播类中,同样使用Intent将要传递的值传给下一个Activity。
  3. 在onCreate()中,动态注册接收广播类的实例receiver。
  4. 在接收广播类中,不要使用完毕后忘记注销接收器,否则会报一个Are you missing a call to unregisterReceiver()? 的异常。

package edu.hbcit.testandroid.activity;import java.util.HashMap;import edu.hbcit.testandroid.R;
import edu.hbcit.testandroid.R.id;
import edu.hbcit.testandroid.R.layout;
import edu.hbcit.testandroid.R.menu;
import edu.hbcit.testandroid.service.ConnectService;
import edu.hbcit.testandroid.util.DialogUtil;
import edu.hbcit.testandroid.util.HttpUtil;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity extends Activity {private static final String ACTION_RECV_MSG = "edu.hbcit.testandroid.intent.action.RECEIVE_MESSAGE";private MessageReceiver receiver ;EditText et_username ;EditText et_password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取文本viewTextView tv = (TextView)findViewById(R.id.mainContent);//获取输入框et_username = (EditText)findViewById(R.id.userName);et_password = (EditText)findViewById(R.id.password);//获取按钮Button submit = (Button)findViewById(R.id.myButton);tv.setText("这是Hello World登录示例");//http://192.168.1.101:8080/server/LoginServlet?un=aaa&pw=123456//为按钮绑定事件监听器submit.setOnClickListener(new OnClickListener(){public void onClick(View v){if (validate()){// 如果登录成功Intent msgIntent = new Intent(MainActivity.this, ConnectService.class);msgIntent.putExtra("username", et_username.getText().toString().trim());msgIntent.putExtra("password", et_password.getText().toString().trim());startService(msgIntent);}}});//动态注册receiver  IntentFilter filter = new IntentFilter(ACTION_RECV_MSG);  filter.addCategory(Intent.CATEGORY_DEFAULT);  receiver = new MessageReceiver();  registerReceiver(receiver, filter);  }// 对用户输入的用户名、密码进行校验private boolean validate(){String username = et_username.getText().toString().trim();if (username.equals("")){DialogUtil.showDialog(this, "用户名是必填项!", false);return false;}String pwd = et_password.getText().toString().trim();if (pwd.equals("")){DialogUtil.showDialog(this, "密码是必填项!", false);return false;}return true;}//接收广播类  public class MessageReceiver extends BroadcastReceiver {  @Override  public void onReceive(Context context, Intent intent) {  String message = intent.getStringExtra("result");  Log.d("MessageReceiver", message);// 如果登录成功if (message.equals("true")){// 启动Main ActivityIntent nextIntent = new Intent(MainActivity.this, ResultActivity.class);startActivity(nextIntent);// 结束该Activityfinish();//注销广播接收器context.unregisterReceiver(this);}else{DialogUtil.showDialog(MainActivity.this, "用户名称或者密码错误,请重新输入!", false);}}  }  }

运行效果:

1.登录界面

2.登录失败界面

3.登录成功界面


小李专栏原创文章,转自需注明出处【http://blog.csdn.net/softwave】。

这篇关于基于IntentService的Android登录完整示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/619705

相关文章

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

SpringBoot集成LiteFlow工作流引擎的完整指南

《SpringBoot集成LiteFlow工作流引擎的完整指南》LiteFlow作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择,本文将详细讲解Sp... 目录一、LiteFlow核心优势二、SpringBoot集成实战三、高级特性应用1. 异步并行执行2