基于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

相关文章

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl