安徽工程大学毕业设计(论文)
参考文献
[1] 靳岩. Android开发入门与实战[M]. 北京: 人民邮电出版社, 2009 [2] 李刚. 疯狂Android讲义[M]. 北京:电子工业出版社, 2012
[3] Gartner, Inc.: Available on http://www.gartner.com.Accessed on March 3, 2009
[4] 余志龙编著。Google Android SDK开发范例大全[M]. 北京:人民邮电出版社,2009-07-01
[5] 赵亮,张维. 基于Android技术的界面设计与研究[D] . 徐州:徐州建筑职业技术学院, 2001
[6] 邝孔无,王晓敏. 信息系统分析与设计(第三版)[M]. 清华大学出版社, 2006:23-26 [7] KEN ARNOLD,JAMES GOSLING,DAVID HOLMES: JAVA PROGRAMMING LANGUAGE [M],POSTS & TELECOM PRESS,2006:231-235
[8] JOHN LEWIS: JAVA SOFTWARE SOLUTIONS[M]. PUBLISHING HOUSE OF ELECTRONICS INDUSTRY,2007:134-140
[9] 何玉洁. 数据库原理与应用教程[M]. 机械工业出版社,2005.12
[10] 亢勇,陈自力,李鹏,路平. 面向对象的软件测试[J]. 测试技术学报 [11] 王俊标. 精通Java Web开发[M]. 人民邮电出版社, 2007
[12] 肖云鹏,刘宴兵. Android程序设计教程 [M].清华大学出版社,2013-04 [13] 张荣. Android开发与应用[M].人民邮电出版,2014-02
[14] 王向辉,张国印,赖明珠. Android应用程序开发[M].清华大学出版社,2012-05 [15] Stephen. Untapped Capabilities of 2g [R]. International Journal of Internet and Enterprise Management, 2006
[16] Reto Meie.Professional Android 2 Application Development [M]. Wiley
- 35 -
姚旭斌:安卓聊天室App的设计与实现
附 录
附录A 外文文献及其翻译
What is android
Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The android sdk provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
Features
Application framework: enabling reuse and replacement of components Dalvik virtual machine: optimized for mobile devices
Integrated browser: based on the open source webkit engine
Optimized graphics: powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)
SQLite: for structured data storage
Media support: for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF) ,GSM Telephony (hardware dependent) Bluetooth, EDGE, 3G, and WiFi (hardware dependent) Camera, GPS, compass, and accelerometer (hardware dependent)
Rich development environment: including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE
Android Architecture
The following diagram shows the major components of the Android operating system. Each section is described in more detail below.
Applications
Android will ship with a set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others. All applications are written using the Java programming language.
Application Framework
By providing an open development platform, Android offers developers the ability to build extremely rich and innovative applications. Developers are free to take advantage of the device hardware, access location information, run background services, set alarms, add notifications to the status bar, and much, much more.
Developers have full access to the same framework APIs used by the core applications. The application architecture is designed to simplify the reuse of components; any application can publish its capabilities and any other application may then make use of those capabilities (subject to security constraints enforced by the framework). This same mechanism allows components to be replaced by the user.
Underlying all applications is a set of services and systems, including:
A rich and extensible set of views that can be used to build an application, including lists, grids, text boxes, buttons, and even an embeddable web browser
content providers that enable applications to access data from other applications (such as
- 36 -
安徽工程大学毕业设计(论文)
Contacts), or to share their own data
A resource manager, providing access to non-code resources such as localized strings, graphics, and layout files
A notification manager that enables all applications to display custom alerts in the status bar
An activity manager that manages the lifecycle of applications and provides a common navigation back stack
Libraries
Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework. Some of the core libraries are listed below:
System C library - a BSD-derived implementation of the standard C system library (libc); tuned for embedded Linux-based devices
Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG
Surface Manager - manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple applications
LibWebCore - a modern web browser engine which powers both the Android browser and an embeddable web view
SGL - the underlying 2D graphics engine
3D libraries - an implementation based on OpenGL ES 1.0 APIs; the libraries use either hardware 3D acceleration (where available) or the included, highly optimized 3D software rasterizer
FreeType - bitmap and vector font rendering
SQLite - a powerful and lightweight relational database engine available to all applications
Linux Kernel
Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. The kernel also acts as an abstraction layer between the hardware and the rest of the software stack.
Application Fundamentals
Android applications are written in the Java programming language. The Android SDK tools compile the code—along with any data and resource files—into an Android package, an archive file with an apk suffix. All the code in a single apk file is considered to be one application and is the file that Android-powered devices use to install the application.
Once installed on a device, each Android application lives in its own security sandbox: The Android operating system is a multi-user Linux system in which each application is a different user.
By default, the system assigns each application a unique Linux user ID (the ID is used only by the system and is unknown to the application). The system sets permissions for all the files in an application so that only the user ID assigned to that application can access them.
Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications.
- 37 -
姚旭斌:安卓聊天室App的设计与实现
By default, every application runs in its own Linux process. Android starts the process when any of the application's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other applications.
In this way, the Android system implements the principle of least privilege. That is, each application, by default, has access only to the components that it requires to do its work and no more. This creates a very secure environment in which an application cannot access parts of the system for which it is not given permission.
However, there are ways for an application to share data with other applications and for an application to access system services:
It's possible to arrange for two applications to share the same Linux user ID, in which case they are able to access each other's files. To conserve system resources, applications with the same user ID can also arrange to run in the same Linux process and share the same VM (the applications must also be signed with the same certificate).
An application can request permission to access device data such as the user's contacts, SMS messages, the mountable storage (SD card), camera, Bluetooth, and more. All application permissions must be granted by the user at install time.
That covers the basics regarding how an Android application exists within the system. The rest of this document introduces you to:
The core framework components that define your application.
The manifest file in which you declare components and required device features for your application.
Resources that are separate from the application code and allow your application to gracefully optimize its behavior for a variety of device configurations.
Application Components
Application components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your application's overall behavior.
There are four different types of application components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.
Here are the four types of application components: Activities
An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.
An activity is implemented as a subclass of activity and you can learn more about it in the activity developer guide.
Services
A service is a component that runs in the background to perform long-running operations
- 38 -
安徽工程大学毕业设计(论文)
or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.
Content providers
A content provider manages a shared set of application data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access. Through the content provider, other applications can query or even modify the data (if the content provider allows it). For example, the Android system provides a content provider that manages the user's contact information. As such, any application with the proper permissions can query part of the content provider to read and write information about a particular person.
Content providers are also useful for reading and writing data that is private to your application and not shared. A content provider is implemented as a subclass of and must implement a standard set of APIs that enable other applications to perform transactions. For more information, see the developer guide.
Broadcast receivers
A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers don't display a user interface, they may to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a \to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.
A broadcast receiver is implemented as a subclass of broadcastreceiver and each broadcast is delivered as an itent object. For more information, see the broadcastreceiver class.
A unique aspect of the Android system design is that any application can start another application’s component. For example, if you want the user to capture a photo with the device camera, there's probably another application that does that and your application can use it, instead of developing an activity to capture a photo yourself. You don't need to incorporate or even link to the code from the camera application. Instead, you can simply start the activity in the camera application that captures a photo. When complete, the photo is even returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application.
When the system starts a component, it starts the process for that application (if it's not already running) and instantiates the classes needed for the component. For example, if your application starts the activity in the camera application that captures a photo, that activity runs in the process that belongs to the camera application, not in your application's process.
Because the system runs each application in a separate process with file permissions that restrict access to other applications, your application cannot directly activate a component
- 39 -
安徽工程大学毕业设计(论文)
安卓聊天室App的设计与实现
摘要
07年Google发布Android后,方便快捷的用户体验使得移动即时通讯成为即时通讯市场的新宠。作为互联网时代的新兴产业,即时通讯在整个互联网市场中占据着越来越重要的地位。
本系统是基于Android平台开发的一款手机聊天客户端应用程序。系统基于环信IM服务器,实现了注册登录,会话列表的获取,好友列表的获取,文本信息的即时通讯等功能。本系统主要使用轻量型嵌入式数据库SQLite来存储数据,运用x-utils框架实现对数据库的操作。
本文在分析了当前环境下聊天App的发展前景后,进行了系统的设计和实现,并对系统进行了完整的测试。系统具有界面友好,操作简单,方便实用的特点。
关键词: Android;即时通讯;环信;聊天App;
I
姚旭斌:安卓聊天室App的设计与实现
Design and Implementation of Android Chat room
Abstract
After 2007 Google released Android,because the convenient user experience,mobile IM become the new darling of the instant messaging market.As an emerging industry in the age of the Internet,instant messaging occupy an increasingly important position in the Internet market.
This system is based ring letter IM server,implements registration and login,session to obtain the list,get a list of friends,chat text messages and other functions.The system main use of lightweight embedded database SQLite to store data,the use of x-utils framework to achieve operation of the database.
After analyzing the prospects for development in the current environment to chat App,we carried out the design and implementation of the system,and the system has been fully tested.
Keywords:Android;instant messaging;ring letter;
安徽工程大学毕业设计(论文)
引 言 ................................................................................................................................... - 1 - 第1章 绪论 ....................................................................................................................... - 2 -
1.1课题背景 .......................................................................................................................................... - 2 - 1.2即时通讯市场发展现状 ................................................................................................................... - 2 - 1.2.1 即时通讯市场状况 ................................................................................................................... - 2 - 1.2.2 即时通讯市场细分 ................................................................................................................... - 3 - 1.2.3 即时通讯的前景 ....................................................................................................................... - 3 - 1.3本文主要工作 .................................................................................................................................. - 4 - 1.4论文的组织结构 .............................................................................................................................. - 4 -
第2章 ANDROID的架构分析 ....................................................................................... - 5 -
2.1 ANDROID特征................................................................................................................................... - 5 - 2.2 ANDROID的架构 ............................................................................................................................... - 6 - 2.3 ANDROID的工作机制 ....................................................................................................................... - 7 - 2.3.1 Activity ....................................................................................................................................... - 7 - 2.3.2 Service ........................................................................................................................................ - 8 - 2.3.3 Content Provider ........................................................................................................................ - 9 - 2.3.4 Broadcast Receiver .................................................................................................................... - 9 - 2.4 ANDROID和其它手机操作系统的比较 ........................................................................................... - 9 - 2.5 本章小结 ........................................................................................................................................ - 10 -
第3章 即时通讯系统 ....................................................................................................... - 11 -
3.1 系统概述 ........................................................................................................................................ - 11 - 3.2 即时通讯简介 ................................................................................................................................ - 11 - 3.2 环信简介 ........................................................................................................................................ - 12 - 3.3 系统架构 ........................................................................................................................................ - 13 - 3.4 网络通信 ........................................................................................................................................ - 13 - 2.3.4 网络协议 ................................................................................................................................ - 13 - 2.3.4 GPRS和Intenet的连接 .......................................................................................................... - 16 - 3.5 ANDROID SDK 介绍 ....................................................................................................................... - 16 - 3.6 本章小结 ........................................................................................................................................ - 18 -
第4章 系统的设计与实现 ............................................................................................... - 19 -
4.1 系统项目结构设计 ........................................................................................................................ - 20 - 4.2 系统客户端的设计与实现 ............................................................................................................ - 21 - 4.2.1 用户界面 ................................................................................................................................ - 21 - 4.2.2 数据存储 ................................................................................................................................ - 22 - 4.3 即时通讯流程描述 ........................................................................................................................ - 24 - 4.3.1 判断网络是否连接 ................................................................................................................. - 24 - 4.3.2 用户的登录和登出 ................................................................................................................. - 24 - 4.3.3 会话列表的读取 ..................................................................................................................... - 26 - 4.3.4 好友列表的读取 ..................................................................................................................... - 27 - 4.3.5 发送和接收消息 ..................................................................................................................... - 27 - 4.4 本章小结 ........................................................................................................................................ - 29 -
第5章 系统的部署及测试 ............................................................................................... - 30 -
5.1系统部署 ........................................................................................................................................ - 30 - 5.2系统测试 ........................................................................................................................................ - 30 -
III
姚旭斌:安卓聊天室App的设计与实现
5.3本章小结 ........................................................................................................................................ - 32 -
结论与展望 ......................................................................................................................... - 33 - 致 谢 ................................................................................................................................. - 34 - 参考文献 ............................................................................................................................. - 35 - 附 录 ............................................................................................................................. - 36 -
附录A 外文文献及其翻译 ................................................................................................................. - 36 - 附录B 主要参考文献的题录及摘要 ................................................................................................. - 45 -
安徽工程大学毕业设计(论文)
图标清单
表4- 1 user表结构........................................................................................................................... - 22 - 表4- 2 name表结构......................................................................................................................... - 23 -
图2- 1 分层架构图 ........................................................................................................................... - 6 - 图2-2 工作流程图 ............................................................................................................................ - 7 - 图2-3 Activity的生命周期 ............................................................................................................... - 7 - 图2-4 Android系统在市场的占有率(2014—2016) ................................................................... - 9 -
图3- 1 环信SDK 开发框架 .......................................................................................................... - 16 - 图3- 2 SDK模块化 ......................................................................................................................... - 17 -
图4- 1 系统整体流程图 ................................................................................................................. - 19 - 图4- 2 代码结构图1 ...................................................................................................................... - 20 - 图4- 3 代码结构图2 ...................................................................................................................... - 21 - 图4- 4 用户实体-联系图 ................................................................................................................ - 23 - 图4- 5 登录模块 ............................................................................................................................. - 24 - 图4- 6 登录界面效果图 ................................................................................................................. - 25 -
图5-1注册界面 ............................................................................................................................... - 30 - 图5-2 服务器用户列表 .................................................................................................................. - 30 - 图5-3 会话列表界面 ...................................................................................................................... - 31 - 图5-4 好友列表界面 ...................................................................................................................... - 31 - 图5- 5 jack聊天页面 ....................................................................................................................... - 32 - 图5- 6 tom聊天页面 ....................................................................................................................... - 32 -
V
姚旭斌:安卓聊天室App的设计与实现
第5章 系统的部署及测试
本系统采用C/S架构,服务器采用环信云服务器,客户端则是基于Android平台上开发。对于数据存储来说,客户端采用Android自带的SQLite数据库来储存用户信息和设置信息。Android客户端通过连接服务器即可实现Android客户端之间的通信。 5.1系统部署
Android客户端安装在真机上测试,机型:vivo x6D,Android版本:5.1。 5.2系统测试
启动Android客户端,主要从用户的注册登录,会话列表的获取,好友列表的获取, 消息的发送和接收和设置几个方面进行测试。 1. 用户的注册和登录: 测试要求:
在注册界面输入“jack”注册信息,并且要求密码以“*”显示,注册成功后,能在服务器的用户列表中看到注册的用户名,对已登录用户高亮显示。 测试结果:
图5-1注册界面
图5-2 服务器用户列表
- 30 -
安徽工程大学毕业设计(论文)
2.获取会话列表和好友列表 测试要求
客户端向服务起发送请求。获取会话列表和好友列表,在页面显示结果。 测试结果:
图5-3 会话列表界面
图5-4 好友列表界面
3.消息的发送和接收 测试要求:
Android用户jack和Android用户tom之间进行即时通讯,互相发送文字信息,通讯双方能够准确接收到对方的信息。 测试结果:
- 31 -
姚旭斌:安卓聊天室App的设计与实现
图5- 5 jack聊天页面
图5- 6 tom聊天页面
5.3本章小结
本章首先对系统的部署环境做了介绍,然后从注册登录,获取会话列表和好友列表,消息的发送和接收几个方面对系统经行测试,对每一项测试都给出了测试要求,并以该要求进行测试给出了测试结果。
- 32 -
安徽工程大学毕业设计(论文)
结论与展望
本文所研究的是基于Android平台的即时通讯系统,首先分析了研究背景,介绍了A-ndroid操作系统及当前即时通讯的发展前景。利用数据的直观性,从多种角度说明本文的研究意义。经分析,得出以下结论:
1.国内即时通讯市场按照用户群体分,大致可分为个人用户,企业用户和群体用户三大类。
2.当前国内市场个人用户群体几乎已被QQ,微信等这类大型企业所开发的通讯软件所垄断,而企业用户和群体用户还存在较大的发展前景。
3.针对当前市场环境,应根据企业的需要,快速集成开发相应的即时通讯系统。 本文对整个系统的实现流程做了详细介绍,并对所开发的系统进行了测试,因个人能力和时间有限,还有很多不足需要优化,许多问题需要解决。现对未来可以进行的工作做出以下几点展望:
1.本文所实现的即时通讯客户端是基于Android平台上进行开发的,目前只能进行文字信息的即时通讯,未实现图片信息,语音信息和文件的即时通讯。为解决这一问题,还有待对环信SDK文档的进一步研究。
2.本系统所有的消息记录均从服务器端获取,在没有网络的情况下,无法看到历史消息,可以通过SQLite数据库来保存历史消息。
3.本系统的界面设计还不是很完善,未能解析Android的精髓,对Android平台进一步的研究,能够使系统的设计方面更加快捷和完善。
相信随着研究的进一步深入,这些问题会逐步得到解决。相信基于Android平台的即时通讯系统会有很好的发展前景。
- 33 -
姚旭斌:安卓聊天室App的设计与实现
致 谢
经过这一学期的努力与付出,终于按期完成毕业设计和毕业论文的撰写。在这里我要感谢指导老师唐肝翌老师,是他在我毕业设计阶段给我提供帮助,从选题的开始,就给出宝贵意见,可以自己自己根据情况选题,然后和唐肝翌老师进行商讨、确定。开题报告的时候,又给我们上传资料和模版,从而给我们省下很多时间。在毕业设计期间遇到问题时,又在百忙之中抽出时间,提供思路,给我解决问题,并且给我推荐一些相关的资料。我得以顺利的完成毕业设计开发工作,老师渊博的知识、敏锐的思路和实事求是的工作作风给我留下了深刻的印象,这将使得我终身受益,谨此向老师表示衷心的感谢。
同时,我也想再此感谢在校的老师们,荏苒大学四年时光,谢谢你们对我的谆谆教导和无私的帮助。我也想感谢,在我毕业设计过程中,帮助我的同学,谢谢你们这几年对我的帮助理解,我想时间不会消磨我们之间的同窗友情,它只会随着时光的流逝,弥足珍贵。在实际接触到项目之后,我才感觉到基础知识的重要性,在实际开发中,我们不仅要结合大学里所学的经验,也要关注当今流行的技术点,从而能够在以后的工作中更好的胜任工作,有所成就,实现自身的价值。
作者: 年 月 日
- 34 -
姚旭斌:安卓聊天室App的设计与实现
4.1 系统项目结构设计
项目代码结构,如图所示:
图4- 2 代码结构图1
- 20 -
安徽工程大学毕业设计(论文)
图4- 3 代码结构图2
4.2 系统客户端的设计与实现
客户端采用的是MVC模式,即模型层,视图层和控制层。用一种业务逻辑、数据、界面显示分离的方法组织代码。有助于管理复杂的应用程序,因为您可以在一个时间内专门关注一个方面。例如,您可以在不依赖业务逻辑的情况下专注于视图设计。同时也让应用程序的测试更加容易。
4.2.1 用户界面
Android用户界面可以定义为XML中的布局资源或程序动态创建。Eclipse的Android开发插件包含了一个很方便的用于设计和预览布局资源的布局资源设计器。这个工具包括两个标签视图:布局视图允许你预览在不同的屏幕下以及对于每一个方向控件会如何展现;XML视图告诉你资源的XML定义。设计程序用户界面最方便且可维护的方式是
- 21 -
姚旭斌:安卓聊天室App的设计与实现
创建XML布局资源。这个访法极大地简化了UI设计过程,将许多用户界面控件的静态产物和布局,以及控件属性定义移动XML中,代替了写代码。它适应了UI设计师(更关心布局)和开发者(了解Java和实现应用程序功能)潜在的区别。开发者依然可以在必要的时候动态的改变屏幕内容。复杂控件,像ListView或GridView,通常用程序动态地处理数据。
在屏幕上显示一个布局资源只需要有包括onCreate()的一行代码就可以搞定。如果布局资源存放在/res/layout/main.xml文件,代码可能是:
setContentView(R.layout.main);用程序动态定义布局。 你也可以用程序创建用户界面组件。为了易组织和可维护性,仅在特殊时候这样做,而不是在一般情况下。不是直接使用setContentView()方法来加载布局资源,你必须创建屏幕内容然后向setContentView()方法提供包含所有要显示的子控件内容的父布局对象。
4.2.2 数据存储
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。利用xUtils框架进行数据库操作。
User表:用来存储用户的用户名和密码信息。
表4- 1 user表结构 字段名 id username realname voice shake sound 类型 int char char int int int 描述 Primery 用户名 备注 声音 震动 语音 - 22 -
安徽工程大学毕业设计(论文)
图4- 4 用户实体-联系图
Name表:用来储存当前用户的用户名。
表4- 2 name表结构
字段名 id name 类型 int char 描述 Primery 当前用户名 数据库相关核心代码如下: //初始化
DaoConfig config=new DaoConfig(getApplicationContext()); config.setDbName(\config.setDbVersion(1); DbUtils db=DbUtils.create(config); try { db.createTableIfNotExist(User.class); db.createTableIfNotExist(Myname.class); } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- 23 -
姚旭斌:安卓聊天室App的设计与实现
4.3 即时通讯流程描述
整个系统流程可分为:用户登录登出—>会话列表读取—>好友列表读取—>消息的发送和接收。如下是即时通讯流程描述中主要核心功能代码。 4.3.1 判断网络是否连接
启动App时,我们需要判断当晚网络状态。当我们要在程序中监听网络状态时,只要以下几个步骤即可:
1.定义一个Receiver重载其中的onReceive函数,在其中完成所需要的功能,如根据WIFI和GPRS是否断开来改变空间的外观
2.在适当的地方注册Receiver,可以在程序中注册,在onCreate中调用。 3.在适当时取消注册Receiver,可以在程序中取消,在onDestroye中调用。 4.3.2 用户的登录和登出
图4- 5 登录模块
首先是布局文件,建立一个线性布局,在布局中添加子布局和控件,效果如下图所示,接着调用setContentView(R.layout.activity_login)方法将其显示在界面上。
- 24 -
安徽工程大学毕业设计(论文)
图4- 6 登录界面效果图
客户端成功连接服务器后,首先判断用户名,密码是否为空,如果都不为空则向服务器发送登录请求,否则提示用户输入相应项。服务器收到请求,对用户进行身份验证,如果通过验证,则发送成功回应,登录成功后需要调用 EMGroupManager.getInstance().l-oadAllGroups(); 和EMChatManager.getInstance().loadAllConversations();。否则,发送失败回应。以下是核心功能代码:
//调用回调函数,传入用户名和密码,进行登录操作
EMChatManager.getInstance().login(username, password, new EMCallBack());
- 25 -
姚旭斌:安卓聊天室App的设计与实现
//若登录成功,则返回onSuccess
public void onSuccess(){ runOnUiThread(new Runnable() { public void run() { EMGroupManager.getInstance().loadAllGroups(); EMChatManager.getInstance().loadAllConversations(); } }
//若登录失败,则返回onError
public void onError(int code,String message){ }
4.3.3 会话列表的读取
首先获取当前所有会话,调用getAllConversations()方法得到一个Hashtable。循环H-ashtable得到一个value的列表lvlist;循环这个lvlist,得到会话最后一条信息的时间time,将time加入tlist中,并且将tlist按照自然数大小升序排列。循环lvlist和tlist,比较lvlist中会话对象最后一条信息的时间和tlist的值是否相符,若一样则将该对象加入列表result,否则,继续循环。最后将得到的result列表进行降序排列。将排列后的list加入到Adapter中,最后加载这个Adapter即可完成获取会话列表的操作。
Hashtable - 26 - 安徽工程大学毕业设计(论文) Collections.reverse(result); adapter = new ListViewAdapter(result, getActivity()); listview.setAdapter(adapter); 在Adapter中,需要将获得的list显示到界面组件中,调用conversation.getLastMes-sage().getBody().toString().substring(5, conversation.getLastMessage().getBody().toString().length() - 1)来获取会话最后一条信息的内容,调用conversation.getLastMessage().getMs-gTime()来获取最后一条信息的发送时间。该时间是以毫秒显示的,需要主动转换。当最后一条信息是当前用户接收时,则调用conversation.getLastMessage().getFrom()来获得会话对象的用户名。当最后一条信息为当前用户发送时,则调用conversation.getLastMe-ssage().getTo()来获取会话对象的用户名。 4.3.4 好友列表的读取 好友列表的获取跟前面讲的会话列表获取有些类似,都是将获取到的信息放入到listview中,再通过加载Adapter来显示。通过调用EMContactManager.getInstance().getContactUserNames()来获取当前用户的所有好友用户名,将其加入到列表中。给listView添加单击事件,点击创建一个与该用户的会话信息。添加一个长按事件,触发时跳出dialog,询问用户下一步操作。若选择修改信息,跳转到修改备注页面,修改好备注后,刷新listview,使得用户名变为备注信息。 4.3.5 发送和接收消息 登录过后,向服务器发送请求,将会话列表显示在会话列表界面,好友列表显示在好友列表界面,通过单击好友列表中的某一个成员,新建一个会话,便可以向该好友发送会话消息。调用环信SDK的sendMessage接口,通过回调函数EMCallBack判断消息是否成功发送。对结果进行操作。注册一个广播,通过广播接收新消息,当有新消息进入时,通过id来获取message对象。若果该对象为当前会话消息,则通知listView刷新数据。如果该对象不是当前会话消息,则新建一个会话对象,通知listView刷新。使用属性“t-o”表示消息的接受者,使用属性“from”表示消息的发送者,使用属性“body”表示消息内容,使用属性“type”表示消息类型。 HORT).show(); 发送消息: EMChatManager.getInstance().sendMessage(message, new EMCallBack(){ @Override public void onError(int arg0, String arg1) { // TODO Auto-generated method stub runOnUiThread(new Runnable(){ public void run(){ Toast.makeText(ChatActivity.this, \发送失败\Toast.LENGTH_S } } }); - 27 - 姚旭斌:安卓聊天室App的设计与实现 @Override public void onProgress(int arg0, String arg1) { // TODO Auto-generated method stub } @Override public void onSuccess() { // TODO Auto-generated method stub runOnUiThread(new Runnable(){ public void run(){ Toast.makeText(ChatActivity.this, \发送成功\Toast.LENGTH_SHORT).show(); } }); }}); }else{ Toast.makeText(getApplicationContext(), \不能发送空消息\Toast.LENGTH_SHORT).show(); } } }); } 接收消息: String msgId = intent.getStringExtra(\ //发送方 String username = intent.getStringExtra(\ // 收到这个广播的时候,message已经在db和内存里了,可以通过id获取mesage对象 EMMessage message = EMChatManager.getInstance().getMessage(msgId); EMConversation conversation = EMChatManager.getInstance().getConversation(username); if (!username.equals(username)) { // 消息不是发给当前会话,return return; } conversation.addMessage(message); dataAdapter.notifyDataSetChanged(); listView.setAdapter(dataAdapter); listView.setSelection(listView.getCount()-1); - 28 - 安徽工程大学毕业设计(论文) 4.4 本章小结 本系统才用环信服务器为通讯平台,因此本章只对Adroid客户端进行了详细的设计和实现。通过对用户界面,数据存储及通讯流程的详细描述来介绍系统的功能。其中,通过对网络的判断用户的登录登出,会话列表获取,好友列表获取,消息发送和接收等功能的设计叙述了一个完整的即时通讯流程。 - 29 - 姚旭斌:安卓聊天室App的设计与实现 from another application. The Android system, however, can. So, to activate a component in another application, you must deliver a message to the system that specifies your intent to start a particular component. The system then activates the component for you. Activating Components Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent. Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your application or another. An intent is created with an intentobject, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively. For activities and services, an intent defines the action to perform (for example, to \or \something) and may specify the URI of the data to act on (among other things that the component being started might need to know). For example, an intent might convey a request for an activity to show an image or to open a web page. In some cases, you can start an activity to receive a result, in which case, the activity also returns the result in an intent(for example, you can issue an intent to let the user pick a personal contact and have it returned to you—the return intent includes a URI pointing to the chosen contact). For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates \ The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a content resolver The content resolver handles all direct transactions with the content provider so that the component that's performing transactions with the provider doesn't need to and instead calls methods on the content object. This leaves a layer of abstraction between the content provider and the component requesting information (for security). There are separate methods for activating each type of component: The Manifest File Before the Android system can start an application component, the system must know that the component exists by reading the application's AndroidManifest.xml file (the \the root of the application project directory. The manifest does a number of things in addition to declaring the application's components, such as: Identify any user permissions the application requires, such as Internet access or read-access to the user's contacts. Declare the minimum API Level required by the application, based on which APIs the application uses. Declare hardware and software features used or required by the application, such as a camera, bluetooth services, or a multitouch screen. API libraries the application needs to be linked against (other than the Android framework APIs), such as the Google Maps library. Declaring component capabilities - 40 - 安徽工程大学毕业设计(论文) As discussed above, in Activating Components, you can use an Intent to start activities, services, and broadcast receivers. You can do so by explicitly naming the target component (using the component class name) in the intent. However, the real power of intents lies in the concept of intent actions. With intent actions, you simply describe the type of action you want to perform (and optionally, the data upon which you’d like to perform the action) and allow the system to find a component on the device that can perform the action and start it. If there are multiple components that can perform the action described by the intent, then the user selects which one to use. The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other applications on the device. When you declare a component in your application's manifest, you can optionally include intent filters that declare the capabilities of the component so it can respond to intents from other applications. You can declare an intent filter for your component by adding an For example, an email application with an activity for composing a new email might declare an intent filter in its manifest entry to respond to \intents (in order to send email). An activity in your application can then create an intent with the “send” action (ACTION_SEND), which the system matches to the email application’s “send” activity and launches it when you invoke the intent with startActivity(). For more about creating intent filters, see the Intents and Intent Filters document. Declaring application requirements There are a variety of devices powered by Android and not all of them provide the same features and capabilities. In order to prevent your application from being installed on devices that lack features needed by your application, it's important that you clearly define a profile for the types of devices your application supports by declaring device and software requirements in your manifest file. Most of these declarations are informational only and the system does not read them, but external services such as Google Play do read them in order to provide filtering for users when they search for applications from their device. For example, if your application requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declare these as requirements in your manifest file. That way, devices that do not have a camera and have an Android version lower than 2.1 cannot install your application from Google Play. However, you can also declare that your application uses the camera, but does not require it. In that case, your application must perform a check at runtime to determine if the device has a camera and disable any features that use the camera if one is not available. Here are some of the important device characteristics that you should consider as you design and develop your application: Screen size and density In order to categorize devices by their screen type, Android defines two characteristics for each device: screen size (the physical dimensions of the screen) and screen density (the physical density of the pixels on the screen, or dpi—dots per inch). To simplify all the different types of screen configurations, the Android system generalizes them into select groups that make them easier to target. - 41 - 姚旭斌:安卓聊天室App的设计与实现 The screen sizes are: small, normal, large, and extra large. The screen densities are: low density, medium density, high density, and extra high density. By default, your application is compatible with all screen sizes and densities, because the Android system makes the appropriate adjustments to your UI layout and image resources. However, you should create specialized layouts for certain screen sizes and provide specialized images for certain densities, using alternative layout resources, and by declaring in your manifest exactly which screen sizes your application supports with the 译文 什么是Android? Android是一个用于移动设备的软件,包括操作系统,中间件和关键应用程序栈。 Android SDK 提供必要的工具和API在Android平台上开始使用Java编程语言开发应用 应用框架 通过提供一个开放的开发平台,为Android的开发人员提供了极其丰富和创新应用能力扩展。开发商考虑到设备的硬件优势,访问位置信息,后台服务,集报警,运行状态栏添加通知等。开发者具有完全访问核心应用程序架构,旨在简化组件重写应用。同样的机制允许组件被用户替换。 所有应用程序的背后,是一套服务和系统,包括: 一组丰富的可扩展的系统,可以用来建立一个应用程序,包括列表,网格,文本框,按钮,甚至可嵌入网页浏览器,提供内容访问数据,从其他应用程序(如通讯录)共享自己的数据。一个资源管理器,提供非代码资源,如本地化字符串,图形,布局和文件的访问,一个通知管理,使所有应用的状态栏显示自定义的警报,活动管理器管理应用程序的生命周期,提供了一个共同导航和BackStack的库。 Android包含一套C / C ++库使用Android系统的各个组件下面列出了一些核心库通过Android应用程序框架,以开发这些功能都开放给开发者: 系统C库 :一个标准C系统库(libc)的BSD派生的库,调整为基于Linux的嵌入式设备。 媒体库 :基于PacketVideo的OpenCORE;许多流行的音频录制和播放支持视频格式库,以及静态图像文件,包括MPEG4,H.264标准,支持MP3,AAC,AMR,JPG格式,和PNG LibWebCore :一个现代化的网页浏览器引擎的Android浏览器和一个可嵌入的Web视图 3D库 :基于OpenGL ES 1.0 API的执行库,可以使用硬件3D加速(如果可用),高度优化的3D软件光栅 FreeType :位图和矢量字体渲染 SQLite的 :为所有应用程序提供一个强大的和轻量级的关系数据库引擎 Android运行 Android包括一套核心库,提供大部分Java编程语言核心库。 每一个Android应用程序运行在它自己的进程中,与自己的Dalvik虚拟机对应。Dalvik被设计,使设备可以有效地运行多个虚拟机,虚拟机执行文件Dalvik可执行(.dex) - 42 - 安徽工程大学毕业设计(论文) 的格式文件,进行优化,最小的内存占用。 Linux内核 Android依赖Linux 2.6版本为核心的系统服务,如安全,内存管理,进程管理,网络堆栈和驱动模型。内核层也作为硬件和软件堆栈的其余部分之间的抽象层。 应用基础 Android应用程序都用Java编程语言所编写。Android SDK工具编译的代码编译成Android包,生成apk。apk文件被认为是一个应用程序,是Android的安装应用程序的文件。一旦安装在设备上,每个Android应用住在自己的安全沙箱。 Android操作系统是一个多用户的Linux系统,系统中的每个应用程序是一个不同的软件。默认情况下,系统为每个应用分配一个唯一的Linux用户ID。该系统设置在应用程序中的所有文件的权限只分配给该应用程序的用户ID,以便可以访问它们。每个进程都有其自己的虚拟机(VM),以便从其他应用程序隔离运行的应用程序的代码。默认情况下,每一个应用程序运行在自己的Linux进程。Android的任何应用程序,需要执行启动过程,也需要执行关闭过程。也就是说,每个应用程序,默认情况下,只能访问到它需要做的工作,并没有更多的访问权限。这将创建一个非常安全的环境中,应用程序不能访问它不获准部分的系统。然而,有一个应用程序与其他应用程序共享数据和应用程序来访问系统服务的方式:安排为两个应用程序共享相同的Linux用户ID,在这种情况下,他们能够互相访问对方的文件。为了节省系统资源,具有相同的用户ID的应用程序也可以安排在相同的Linux进程运行,并共享相同的虚拟机(应用程序也必须使用同一证书签名)。应用程序可以请求访问用户的通讯录,短信,装入存储(SD卡),摄像头,蓝牙,以及更多的设备,如数据的权限。用户在安装时的所有应用程序必须被授予权限。涵盖了一个Android应用程序如何在系统中存在的基础。 应用程序组件 应用程序组件是一个Android应用程序的重要组件。每个组件是一个不同的角度,通过该系统可以进入您的应用程序。不是所有的组件是实际的用户和一些互相依赖切入点,但是每一个组件作为自己的实体存在,并发挥特定的角色,每个组件都是独特的建筑块,有助于确定您的应用程序的整体行为。有四种不同类型的应用程序组件。每个类型提供不同的目的,并有一个独特的生命周期定义如何创建和销毁组件。这里有四种类型的应用程序组件: 活动 一个活动代表一个单一的用户界面。例如,电子邮件应用程序可能有一个活动,显示了一个新的电子邮件列表,另一个活动,写电子邮件,并阅读邮件的另一个活动。虽然这些活动,在一起成为用户体验中的电子邮件应用程序,但是每个活动都是独立于其他。正因为如此,不同的应用程序可以启动任何一个这些活动(如电子邮件应用程序,允许它)。例如,一个摄像头应用程序可以为用户在启动的电子邮件应用程序中的活动,写新邮件,分享图片。 服务 一个服务是一个组件,在后台运行,执行长时间运行的操作或远程进程执行工作。一个服务不提供用户界面。例如,服务可能会在后台播放音乐的同时,用户在不同的应用程序,或者它可能获取网络上的数据,而不阻塞用户交互的活动。如活动的另一个组成部分,可以启动该服务,并允许它运行或绑定到它与它交互。 内容提供 一个内容提供是应用程序数据的共享。你可以将存储在文件系统中的数据,一个 - 43 - 姚旭斌:安卓聊天室App的设计与实现 SQLite数据库中的数据,在网络上,或任何其他持久性存储位置,您的应用程序都可以访问。通过内容提,其他应用程序可以查询甚至修改内容提供商的数据(如果允许)。例如,Android系统提供了一个内容供应用户的联系人信息。因此,任何具有适当权限的应用程序可以查询的内容提供商(如部分ContactsContract.Data)读取和写入一个特定的人的信息。 广播接收机 一个广播接收器是一个全系统广播通知的组件。例如宣布该屏幕已关闭,电池低,或图片被抓获。应用程序也可以启动广播,例如,让我们知道,一些数据已被下载到设备,可供他们使用其他应用程序。虽然广播接收机不显示用户界面,他们可能会创建一个状态栏通知 广播事件发生时提醒用户。不过,更普遍,广播接收器是一个“网关”其他组件和打算做的工作非常少量。例如,它可能会启动服务来执行基于事件的一些工作。 广播接收机实现BroadcastReceiver作为一个子类 ,每个广播作为交付的意向对象。 独特的Android系统设计的一个方面,使任何应用程序可以启动另一个应用程序的组件。例如,如果你希望用户与设备上的照相机捕捉的照片,还有可能和您的应用程序可以使用它的另一个应用程序,而不是发展自己拍摄照片的活动。 在系统启动时一个组件,它启动该应用程序的进程(如果它尚未运行)和实例化组件所需的类。例如,如果您的应用程序开始在捕捉照片的摄像头应用程序的活动,该活动的摄像头应用程序,在您的应用程序的过程中不属于在这个过程中运行。因此,不同于其他大多数系统上的应用程序,Android应用程序不具有一个单一的入口点(例如,有没有 main ()函数)。 manifest文件 Android系统可以启动一个应用程序组件,系统必须知道,通过阅读应用程序的AndroidManifest.xml文件可以知道的组件存在。您的应用程序必须使用在这个文件中声明的组件,它必须是在应用项目目录的根。清单做了的事情,除了宣布应用程序的组件,如:确定任何用户的权限,如因特网接入或只读访问用户的联系人应用程序需要。申报的基于API的应用程序使用的应用程序所需的最低空气污染水平。声明由一个摄像头,蓝牙服务,或者是多点触摸屏的应用,如使用或要求的硬件和软件功能。API库链接(Android框架API的除外),如谷歌地图图书馆,应用程序需要。和声明组件活动,服务和内容提供商,包括源,但不申报清单是不可见的系统,因此,不能运行。然而,广播接收机可以宣布在清单或在代码中动态创建(为BroadcastReceiver对象)和注册系统由调用registerReceiver()。 - 44 - 百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典综合文库基于安卓的聊天App的设计与实现毕业论文在线全文阅读。
相关推荐: