




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Yangtze University College of Arts and Science學(xué)生課程設(shè)計報告系 部: 機電與信息工程系 專 業(yè): 計算機科學(xué)與技術(shù) 班 級: 計科5131 姓 名: 來超 學(xué) 號: 201241136 課程名稱: 移動平臺數(shù)據(jù)庫綜合開發(fā) 指導(dǎo)教師: 王 騰 實習(xí)時間:2016年6月20日至2016年6月27日android即時通訊軟件摘要在網(wǎng)絡(luò)越來越發(fā)達(dá)的今天,人們對網(wǎng)絡(luò)的依賴越來越多,越來越離不開網(wǎng)絡(luò),由此而產(chǎn)生的聊天工具越來越多,例如,國外的ICQ、國內(nèi)騰訊公司開發(fā)的OICQ。基于Java網(wǎng)絡(luò)編程的強大功能,本次課程設(shè)計使用Java編寫一個運行在androi
2、d2.2版本以上的即時通訊軟件。一般來說,聊天工具大多數(shù)由客戶端程序和服務(wù)器程序外加服務(wù)器端用于存放客戶數(shù)據(jù)的數(shù)據(jù)庫組成。Java提供的多線程功能用多線程可完成一點對多點的聊天,數(shù)據(jù)庫管理系統(tǒng)用SQLlite來訪問數(shù)據(jù)庫的。本系統(tǒng)建立在JAVA平臺上,系統(tǒng)的設(shè)計使用了面向?qū)ο蠹夹g(shù)和面向?qū)ο蟮脑O(shè)計原則。系統(tǒng)采用C/S結(jié)構(gòu),客戶端與客戶端以及客戶端與服務(wù)器端之間傳送消息。使用JAVA語言編寫,開發(fā)工具采用AS。在智能手機中,Android平臺約占75市場份額。安卓系統(tǒng)的廣泛應(yīng)用,使得Android手機應(yīng)用程序的數(shù)量快速增長,即時通訊APP的應(yīng)用范圍也更加廣泛。關(guān)鍵字:多線程;客戶機/服務(wù)器 ;JA
3、VA;AS ;Android手機應(yīng)用1.開發(fā)背景Android的英文翻譯是指“機器人”,而Android這個詞語作為操作系統(tǒng)的名稱出現(xiàn),是谷歌在2007年發(fā)布了應(yīng)用在手機平臺上的操作系統(tǒng),自此Android作為手機操作系統(tǒng)出現(xiàn)在人們的視野中。同時由于Android的操作系統(tǒng)的開源性和可開發(fā)性使得以Android操作系統(tǒng)的應(yīng)用軟件日益增多。Android操作系統(tǒng)被越來越多的人接受和使用。目前的Android聊天軟件基本分為2種架構(gòu)形式:(1)C/S架構(gòu):采取客戶端/服務(wù)器對等傳輸?shù)男问?,用戶方主要使用客戶端軟件,使用前用戶通過網(wǎng)上下載客戶端軟件,再安裝到手機上進(jìn)行使用,市場上流行的C/S架構(gòu)的聊
4、天軟件有以下幾種:QQ、微信、微博、YY、MSN等;(2)B/S架構(gòu):瀏覽器/服務(wù)端對等傳輸?shù)男问?,這種形式下的即時通信工具,一般都是依賴于互聯(lián)網(wǎng),作為用戶無需下載客戶端,用戶一般在瀏覽往網(wǎng)上網(wǎng)頁時瀏覽器會提供一個類似于客戶端的界面使用戶可以發(fā)送消息,此架構(gòu)一般應(yīng)用在數(shù)據(jù)流比較大的電商網(wǎng)站,如WebQQ等。2.軟件需求分析2.1軟件開發(fā)的工具和環(huán)境1.開發(fā)環(huán)境:AS2.開發(fā)語言:JAVA3.模擬機平臺:AVD4.手機運行平臺:Android2.1以及上版本開發(fā)軟件5.所需插件:JDK2.2安裝JDK和SDK先從網(wǎng)站上找到JDK1.6(JREaloneisnotsufficient)版本并下載,
5、下載完畢后將它安裝在PC機上并在PC機上設(shè)置好有關(guān)的環(huán)境變量,將JAVA_HOME,path等幾個設(shè)置為適用于Java開發(fā)的環(huán)境變量,最后將其導(dǎo)入你的JDK文件路徑SDK安裝:直接從相關(guān)網(wǎng)站下載好SDK壓縮包,然后打開Eclipse軟件,在其中找到對應(yīng)路徑將其導(dǎo)入就可以。3、原理與技術(shù)要點分析3.1 登錄功能概要Ø 點擊button按鈕 Ø Ø 服務(wù)器驗證登錄賬號 Ø Ø 跳轉(zhuǎn)進(jìn)入好友列表界面登錄流程圖:進(jìn)入輸入賬號及密碼 失 判斷賬號密碼 服務(wù)器驗證敗 是否正確登錄成功 1.1登錄功能流程圖3.2聊天功能概要Ø 用戶點擊聯(lián)系人列表中
6、的一個好友時,進(jìn)入聊天界面 Ø Ø 在輸入框輸入信息,點擊發(fā)送按鈕,發(fā)送信息 Ø Ø 好友回復(fù)消息是在該聊天頁面顯示 Ø 當(dāng)有好友發(fā)消息過來時,系統(tǒng)會通過Toast提示 用戶聊天模塊總體流程圖:1.2用戶聊天模塊總體流程圖3.3技術(shù)要點分析3.3.1概念結(jié)構(gòu)設(shè)計 3.3.2邏輯結(jié)構(gòu)設(shè)計1) 用戶信息(用戶賬號,用戶密碼,昵稱,頭像) public class QQMessage extends Protocal public String type = QQMessageType.MS
7、G_TYPE_CHAT_P2P; / 數(shù)據(jù)類型 public long from = 0; / 發(fā)送者 account public String fromNick = ""/ QQ名 public int fromAvatar = 1; / 頭像 public long to = 0; / 接收者 account public String content = "" / 消息內(nèi)容(hi) public String sendTime = MyTime.getTime(); / 發(fā)送時間2) 會話信息(好友賬號,好友頭像,發(fā)送/接收消息時間,消息內(nèi)容)p
8、ublic class QQMessageType public static final String MSG_TYPE_REGISTER = "register" / 注冊 public static final String MSG_TYPE_LOGIN = "login"/ 登錄 public static final String MSG_TYPE_LOGINOUT = "loginout"/ 登出 public static final String MSG_TYPE_CHAT_P2P = "chatp2p&qu
9、ot;/ 聊天 public static final String MSG_TYPE_CHAT_ROOM = "chatroom"/ 群聊 public static final String MSG_TYPE_OFFLINE = "offline"/ 下線 public static final String MSG_TYPE_SUCCESS = "success"/ 成功 public static final String MSG_TYPE_BUDDYLIST = "buddylist"/ 好友列表 pub
10、lic static final String MSG_TYPE_FAILURE = "failure"/ 失敗4詳細(xì)設(shè)計4.1 用戶聊天模塊4.1.1 ChatActivity 類public class ChatActivity extends Activity ListView listView; TextView title,send_nick,recv_nick; EditText input; String six_space=" " String two_space=" " private String toNick;
11、private Long account; ImApp app; private ChatMessageAdapter adapter; List<QQMessage> list =new ArrayList<>(); /內(nèi)容觀察者:觀察uri中是否出現(xiàn)數(shù)據(jù)庫被修改的信號 private ContentObserver observer=new ContentObserver( new Handler() /觀察到發(fā)生變化后,所做的動作 /android2.2 Override public void onChange(boolean selfChange) super
12、.onChange(selfChange); requery(); /android4.0以上 Override public void onChange(boolean selfChange, Uri uri) super.onChange(selfChange, uri); requery(); ; private void requery() /System.out.println("數(shù)據(jù)發(fā)生變化,取出數(shù)據(jù)刷新聊天界面"); list.clear(); querySetofChatWithOne(account+"",list); if(adapt
13、er!=null) adapter.notifyDataSetChanged(); if(list.size()>0) listView.setSelection(list.size()-1); Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); listView= (ListView) findViewById(R.id.listview_chat); title= (
14、TextView) findViewById(R.id.tv_title); input= (EditText) findViewById(R.id.edt_input); send_nick=(TextView)findViewById(R.id.tv_send_nick); recv_nick=(TextView)findViewById(R.id.tv_recv_nick); /注冊內(nèi)容觀察者 Uri uri=Uri.parse("content:/"+ QQContactDBDao.class.getSimpleName(); / getContentResolve
15、r().registerContentObserver(地址:信號,子路徑是否有效,指定一個觀察者); this.getContentResolver().registerContentObserver(uri,true,observer); app= (ImApp) getApplication(); Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); account=bundle.getLong("account"); toNick=bundle.getString("ni
16、ck"); title.setText("與 " +toNick +"聊天中"); querySetofChatWithOne(account+"",list); adapter=new ChatMessageAdapter(this,list); listView.setAdapter(adapter); if(list.size()-1>0) listView.setSelection(list.size()-1); /接收消息 /1.messageType=p2p 2.app.account=msg.to 3.m
17、sg,account->toast public void clickSend(View v) String messageBody=input.getText().toString().trim(); if("".equals(messageBody) Toast.makeText(getBaseContext(),"發(fā)送消息不為空",Toast.LENGTH_SHORT).show(); return; input.setText(""); final QQMessage msg=new QQMessage(); msg.t
18、ype= QQMessageType.MSG_TYPE_CHAT_P2P; msg.content=messageBody; msg.from=app.getAccount(); msg.to=account; msg.fromNick="人族" /發(fā)送干兩件事情 /1:把消息顯示到聊天記錄區(qū) list.add(msg); /刷新list if(adapter!=null) adapter.notifyDataSetChanged(); /從最后一條開始顯示 if(list.size()>0) listView.setSelection(list.size()-1);
19、 /2:發(fā)送給聊天對象 ThreadUtils.runInThread(new Runnable() Override public void run() try app.getCoreService().sendMessage(msg); catch (Exception e) e.printStackTrace(); ); / 把與當(dāng)前好友聊天記錄查詢出來, / 并顯示到與當(dāng)前好友的聊天界面 private void querySetofChatWithOne(String session_id,List<QQMessage>msgList) List<com.examp
20、le.meiyu.qq_im.dao.QQContactDB> dbmsgList=app.getQqContactDBDao().queryRaw("where SESSION_ID = ? order by SENDTIME ASC",session_id); for(com.example.meiyu.qq_im.dao.QQContactDB item:dbmsgList) QQMessage msg = new QQMessage(); msg.type = QQMessageType.MSG_TYPE_CHAT_P2P; msg.content = ite
21、m.getContent(); msg.from = item.getFrom(); msg.to = item.getTo(); msg.fromNick = item.getFromNick(); msgList.add(msg); Override protected void onDestroy() super.onDestroy(); getContentResolver().unregisterContentObserver(observer); 4.1.2 SplashActivity 類public class SplashActivity extends Activity O
22、verride protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); ThreadUtils.runInThread(new Runnable() Override public void run() try Thread.sleep(3000); /至少3秒,下限 startActivity(new Intent(getBaseContext(),LoginActivity.class);
23、finish(); catch (InterruptedException e) e.printStackTrace(); ); 4.1.3 activity_login.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent"> <TableL
24、ayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"> <!-圖標(biāo)-> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:src="drawable/ic_launcher_qq&quo
25、t;/> <!-賬號-> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Q
26、Q賬號" android:textSize="25sp"/> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:singleLine="true" android:id="+id/account" android:text="159"/> </TableRow> <!-密碼-> <TableRow an
27、droid:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 碼" android:textSize="25sp&
28、quot;/> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:singleLine="true" android:id="+id/psw" android:inputType="textPassword" android:text="test"/> </TableRow> <!-按鈕-> <Button an
29、droid:onClick="login" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登錄" android:gravity="center" android:textSize="25sp"/> </TableLayout></Relati
30、veLayout>4.1.4 activity_chat.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="1&quo
31、t;><!-好友標(biāo)簽-> <TextView android:id="+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="#FFFFFF" android:background="#0099ff" android:gravity="cen
32、ter"/> <!-聊天記錄-> <ListView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="+id/listview_chat" android:cacheColorHint="android:color/transparent" android:listSelector="android
33、:color/transparent" android:divider="android:color/transparent" android:dividerHeight="1dp"> </ListView> <!-輸入聊天-> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="40dp"&
34、gt; <EditText android:id="+id/edt_input" android:layout_weight="6" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:onClick="clickSend" android:layout_width="wrap_content" android:layout_height=&qu
35、ot;wrap_content" android:layout_weight="1" android:text="發(fā)送"/> </LinearLayout></LinearLayout>4.2 客戶端模塊4.2.1 ContactActivity 類public class ContactActivity extends Activity ListView listView; ContactAdapter adapter; ImApp app; /好友列表 List<QQContact> list =n
36、ew ArrayList<>(); /每一個activity必備的監(jiān)聽器 IOnMessageReceiveListener listener=new IOnMessageReceiveListener() Override public void onReceive(final QQMessage msg) ThreadUtils.runUnThread(new Runnable() Override public void run() if(QQMessageType.MSG_TYPE_BUDDYLIST.equals(msg.type) String json=msg.con
37、tent; Gson gson=new Gson(); /反序列化 QQContactList tempList=gson.fromJson(json,QQContactList.class); /刷新好友列表 /首先設(shè)置列表值 list.clear(); list.addAll(tempList.buddyList); /其次再刷新列表 if(adapter!=null) adapter.notifyDataSetChanged(); ); ; Override protected void onCreate(Bundle savedInstanceState) super.onCreate
38、(savedInstanceState); setContentView(R.layout.activity_contact); listView= (ListView) findViewById(R.id.listview_contact); app= (ImApp) getApplication(); app.getCommunicaCore().addOnMessageReceiveListener(listener); /處理好友列表 /先取出(app) String json=app.getBuddyListJson(); System.out.println(json); /再處理
39、(從json反序列化成對象) Gson gson=new Gson(); QQContactList tempList=gson.fromJson(json,QQContactList.class); /把上面完成的對象加入到list里面去 list.addAll(tempList.buddyList); /綁定適配器 adapter =new ContactAdapter(this,list); listView.setAdapter(adapter); / 添加item的click監(jiān)聽器 listView.setOnItemClickListener(new ListView.OnItem
40、ClickListener() Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) / 獲取數(shù)據(jù) QQContact contact = list.get(position); Intent intent = new Intent(getBaseContext(),ChatActivity.class); intent.putExtra("account",contact.account); intent.putExtra("
41、nick",contact.nick); startActivity(intent); ); protected void onDestroy() super.onDestroy(); app.getCommunicaCore().removeOnMessageReceiveListener(listener); 4.2.2 LoginActivity 類public class LoginActivity extends Activity EditText account,psw; String username,password; QQCommunicationCore comm
42、unicaCore; Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); account= (EditText) findViewById(R.id.account); psw= (EditText) findViewById(R.id.psw); /網(wǎng)絡(luò)鏈接 ThreadUtils.runInThread(new Runnable() Override public voi
43、d run() try communicaCore=new QQCommunicationCore("192.168.1.126",20001); communicaCore.addOnMessageReceiveListener(listener); communicaCore.connection(); catch (Exception e) e.printStackTrace(); ); /被回調(diào) 本activity處理消息的核心 IOnMessageReceiveListener listener=new IOnMessageReceiveListener() Ov
44、erride public void onReceive(final QQMessage msg) ThreadUtils.runUnThread(new Runnable() Override public void run() System.out.println(msg.toXML(); if(QQMessageType.MSG_TYPE_BUDDYLIST.equals(msg.type) ImApp app= (ImApp) getApplication(); /保存賬號 app.setAccount(Long.parseLong( account.getText().toStrin
45、g().trim(); /保存連接(被保存過的連接叫長連接) app.setCommunicaCore(communicaCore); /保存好友列表 (好友列表在content) app.setBuddyListJson(msg.content); Toast.makeText(getBaseContext(),"登錄成功", Toast.LENGTH_LONG).show(); /啟動聯(lián)系人界面 startActivity(new Intent(getBaseContext(),ContactActivity.class); /啟動核心服務(wù)(啟動式) startServ
46、ice(new Intent(getBaseContext(),CoreService.class); finish(); else Toast.makeText(getBaseContext(),"登陸失敗",Toast.LENGTH_LONG).show(); ); ; /只發(fā)送消息,不處理消息 public void login(View v) username=account.getText().toString().trim(); password=psw.getText().toString(); ThreadUtils.runInThread(new Runn
47、able() Override public void run() try QQMessage msg=new QQMessage(); msg.type= QQMessageType.MSG_TYPE_LOGIN; msg.content=username+"#"+password; communicaCore.sendMessage(msg); catch (Exception e) e.printStackTrace(); ); Override protected void onDestroy() super.onDestroy(); communicaCore.r
48、emoveOnMessageReceiveListener(listener); 4.2.3 activity_contact.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:textSize="30sp" android:text
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 打造安全社區(qū)的基本原則計劃
- 足球訓(xùn)練科技的發(fā)展與團(tuán)隊配合、個人技巧的進(jìn)步
- 跨文化背景下的節(jié)日慶祝習(xí)慣比較研究
- 零售業(yè)資產(chǎn)證券化的策略與實踐
- 跨區(qū)域醫(yī)療資源分配與醫(yī)養(yǎng)服務(wù)網(wǎng)絡(luò)構(gòu)建
- 質(zhì)量提升關(guān)鍵點之一-對標(biāo)行業(yè)標(biāo)桿的血檢儀器如精準(zhǔn)度保持措施詳解
- 廣西2025年01月廣西壯族自治區(qū)衛(wèi)生健康對外交流合作中心2025年招考工作人員筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 2025年安徽淮海實業(yè)發(fā)展集團(tuán)有限公司校園招聘筆試參考題庫附帶答案詳解
- 跨文化背景下電商平臺的發(fā)展策略
- 四年級數(shù)學(xué)下冊折線統(tǒng)計圖教案蘇教版
- 大學(xué)生安全知識班會
- 課件圍術(shù)期下肢深靜脈血栓的預(yù)防與護(hù)理
- 2025年菏澤家政職業(yè)學(xué)院單招職業(yè)技能測試題庫完美版
- 《電力變壓器》課件
- 初級鐵路線路工技能鑒定考試題庫
- 2025年度建筑垃圾運輸與再生資源回收一體化合同樣本
- 2024新人教版英語七下單詞默寫表(開學(xué)版)
- (2025)輔警招聘公安基礎(chǔ)知識必刷題庫及參考答案
- 農(nóng)業(yè)機械設(shè)備維護(hù)與質(zhì)量保障措施
- 基于圖像處理的CAD圖紙比對算法
- 人教版六年級下冊數(shù)學(xué)第二單元百分?jǐn)?shù)(二)綜合練習(xí)卷-(附答案)
評論
0/150
提交評論