23、Android之使用 Google API 的 GTalk
生活随笔
收集整理的這篇文章主要介紹了
23、Android之使用 Google API 的 GTalk
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用 Google ?API 的 GTalk ? ?
關鍵技能 ?& ?概念
● ?執行一個 Google ?API 包裝
● ?為 Google 存取配置 XMPP 開發環境設置
● ?執行 View.OnClickListener( ?) ?方法
第九章為你介紹了 Google ?API。你創建了一個影響 Google ?API 和 Google 地圖
的活動。因為 API 的易用和靈活性,可以快速的在 Google 地圖上顯示用戶的當
前位置。同樣,你還學習到了如何使用很少量的相關代碼來熟練操控地圖。
Google ?API 包括了不僅是進入 Google 地圖的功能。在上一章中,你使用了很大
的 API 中很小的一個部分。對于 Google ?API 的基本包裝是 com.google。從這個
基礎中,Google ?API 包含了允許你創建操控 GTalk(Google 的 聊 天 服 務 ),Google
日歷,Google 文檔,Google 電子表格和 Google 服務等等的活動的權力。
當我看是寫這本書時,這個版本的 Android ?SDK 是 m3-rc22。寫完書時,Google
不提倡這些包裝中的一些,但是還是留在 SDK 中。有顯示 Google ?日歷,Google
電子表格和 Google 服務仍舊需要升級,遺憾的是,在 m5-rc15 版本的 SDK 中還
處于未完成的狀態。為了避免混亂,Google 還移除了任何與這些包裝相關聯的
幫助文件。因此,本章的重點是在最新發布的 Android ?SDK 中工作很好的 GTalk。
在本章中,將會構造一個小的,利用 Android ?SDK 的 GTalk 包裝的活動。當活動
完成,你將可以利用手機發送并接受信息到/從另外一個 GTalk 用戶。
注意
在第一個 Google ?API 的反復中,處理 GTalk 的包裝是一個非常廣泛的 XMPP 包裝 。
(XMPP 是很多聊天平臺的基礎協議,包括 GTalk 和 Jabber)。使用最新版的 SDK,
初始的 XMPP 包裝為反映 GTalk 的特性而加強并重命名。要開始,用 Eclipse 創
建一個新項目,并且命名 GoogleAPI。
為 GTalk 配置 Android 模擬器
在開始為本項目寫代碼之前,你需要在 Android 模擬器中調整開發環境設定,
XMPP 設定。
在項目打開的狀態下,需要離開常規的程序一會兒。如果你熟悉 GTalk,你知道
只有登錄 Google 帳戶以后才可以使用這個產品。因此,你必須要采取特別的步
驟來確保你的設備(本例是 Android 模擬器)可以登錄你的 Google 帳戶,這樣,
可以確保發送和接受信息。136
導航到 AndroidSDK/tools 文件夾并且啟動模擬器。你可以從 Eclipse 開發環境
中啟動它,但是那樣會需要同時啟動還沒有寫代碼的活動。為了節約時間,手動
啟動模擬器。
模擬器啟動后,點擊所有的快捷方式(All ?shortcut)。找到 Dev ?Tools 條目并
且啟動它。你將會看到和下面類似的圖(略)。
滾動 Dev ?Tools 菜單直到看見 XMPP 設置。選擇 XMPP 設定。
注意
當你打開 XMPP 設定,活動的名稱是 GTalk 設定。這個可能是個表象說明 Google
將在 Google ?API 包裝中留下這個包裝。命名顯而易見的斷開可能是從不同 SDK
版本之間改變而剩下的。
活動應當讀取賬戶:<None>,如圖所示(略)。這表明了設備中沒有儲存登錄信息 。
你需要為 Google 帳戶增加登錄信息來允許活動來有權使用 Google 的服務器。
點擊增加帳戶來顯示一個屏幕。輸入用戶名和密碼之后,點擊登錄。Android 模
擬 器 應 當 試 著 去 驗 證 你 的 信 息 。 當 模 擬 器 嘗 試 驗 證 信 息 時 , 它 顯 示
“Authenticating”信息。
警告
取決于你的連接和你是否有一個調試器和模擬器相連接。你可能會看到
“Authenticating”信息一會兒。如果你的帳戶幾分鐘后無法被驗證,重新啟動
模擬器再試試。
一旦你的信息被驗證,你應當看到下圖(略)。注意,這里沒有返回按鈕,點擊
模擬器中的 Home 鍵返回到主屏幕。
現在模擬器配置好了,并且項目也被設置好,可以開始為活動寫代碼了。
在 Android 中執行 GTalk
在 Android ? ? ? ? Android ? ? ? ? Android ? ? ? ? Android 中執行 GTalk ? ? GTalk ? ? GTalk ? ? GTalk 第十章(2) ?(2) ?(2) ?(2)
在本節中將使用 Google ?API 來創建一個使用 GTalk 的活動。這個活動會從 GTalk
網絡發送并接收信息,在屏幕上保存它們,并在通知條中顯示。活動可以和其它
GTalk 用戶通信,而不管他們是在使用 Android 手機或者電腦上的 GTalk。
下一部分將從如何創建應用程序的布局開始。第一步為活動的編碼工作就是增加
布局到 GoogleAPI.xml 中。
在 GoogleAPI.xml 中創建活動的布局
本活動由一些 Views 組成。需要一個 ListView 來顯示需要發送的文本信息。同
樣需要兩個 EditText,用于接收者的地址和信息,還有一個發送的 Button。
首先設置一個 id 是 messageList 的 ListView,如下所示。在本布局中,將使用
一個新的屬性,android:scrollbars。設置這個屬性為立式會讓你可以在信息列
表中滾動。137
把 ListView 放到主要布局標簽中。在 ListView 布局的下方,放置一個 EditText
的布局,如下。這個 EditText 保留你會發信息的收信人地址。
這個 EditText 視圖應該沒有什么特殊之處。
最后,再創建一個水平的布局來保留信息內容——EditText 和發送按鈕:
這 個 布 局 會 以 線 性 的 方 式 放 置 視 圖 , 所 以 它 們 相 互 排 成 一 排 。 把 這 個
LinearLayout 放置進主要的 LinearLayout。最后,GoogleAPI.xml 文件將會如
下:
<ListView
android:id="@+id/messageList"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:scrollbars="vertical"
android:layout_weight="1"
android:drawSelectorOnTop="false" ?/>
<EditText
android:id="@+id/messageTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:minWidth="250dp"
android:scrollHorizontally="true" ?/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/messageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:minWidth="250dp"
android:scrollHorizontally="true" ?/>
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send ?Msg">
</Button>
</LinearLayout>138
為 GoogleAPI.java 增加包裝
布局文件完成后,有一些新的包裝需要增加到 GoogleAPI.java 文件中。第一個
<?xml ?version="1.0" ?encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/messageList"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:scrollbars="vertical"
android:layout_weight="1"
android:drawSelectorOnTop="false" ?/>
<EditText
android:id="@+id/messageTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:minWidth="250dp"
android:scrollHorizontally="true" ?/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
246 ?Android: ?A ?Programmer’s ?Guide
android:layout_height="wrap_content">
<EditText
android:id="@+id/messageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:minWidth="250dp"
android:scrollHorizontally="true" ?/>
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send ?Msg">
</Button>
</LinearLayout>
</LinearLayout>139
必須導入的包裝應該和增加到布局中的 Views 相對應。因此,必須為 EditText,
ListView,ListAdapter,和按鈕導入包裝:
同樣需要導入的是 Google ?API 和 GTalk 打交道的包裝:
其它需要的包裝有 intent,ServiceConnection, ?Color, ?和 ?Im. ?完整的列表如
下:
如你所見,本活動所需的包裝不多。而且,你會發現,發送和接受信息所需要的
代碼非常的少。現在,需要執行一個 onClickListnerner 來允許代碼。
執行 View.OnClickListener
需要修改 GoogleAPI 的類來執行 View.OnClickListener。當任何按鈕被點擊,
這將允許你從活動的主類來呼叫 onClick()方法。通常,這種執行 onClick()方
法是有效的:只有當你在一個活動中有一組按鈕,并且以一種方式來出處理所有
的 onClick 呼叫。但是,我感覺你仍舊需要看看這個方式是如何工作的,那樣你
就可以在將來用于自己的代碼中。記住,展示這個方式是因為在很多情況下,它
可以是非常有用的工具
在活動中執行常規變量是本書中另外一個未曾談到的內容。你需要在活動中建立
一些常規變量,這樣就可以以很多方法來使用:
import ?android.widget.EditText;
import ?android.widget.ListView;
import ?android.widget.ListAdapter;
import ?android.widget.Button;
import ?com.google.android.gtalkservice.IGTalkSession;
import ?com.google.android.gtalkservice.IGTalkService;
import ?com.google.android.gtalkservice.GTalkServiceConstants;
import ?com.google.android.gtalkservice.IChatSession;
import ?android.content.ComponentName;
import ?android.content.Intent;
import ?android.content.ServiceConnection;
import ?android.database.Cursor;
Chapter ?10: ?Using ?the ?Google ?API ?with ?GTalk ?247
248 ?Android: ?A ?Programmer’s ?Guide
import ?android.os.Bundle;
import ?android.os.DeadObjectException;
import ?android.os.IBinder;
import ?android.provider.Im;
import ?android.graphics.Color;
import ?android.view.View;
import ?android.widget.SimpleCursorAdapter;
public ?class ?GoogleAPI ?extends ?Activity ?implements ?View.OnClickListener ?{
}140
在 onCreate()方法中,你將執行正常的初始化。應當賦值布局到活動并且設置
IGTalkSession 為 null。同樣,在活動中增加點小樂趣,改變 ListView 的背景
色為灰色。
提示
因為你是從類中執行 View.OnClickListener 的,可以設置 Send ?Button 的
OnClickListener()方式到其中。最后在 onCreate()方法內執行是綁定你的服
務。這個過程創建創建需要使用的連接,由 Google 帳戶使用,傳遞 GTalk 信息:
在上面的 bindService 聲明中,傳遞到 setComponent()方法的一個參數就是連
接。這個變量表示一個 ServiceConnection 執行 onServiceConnected()和
onServiceDisconnected()方法。下面的代碼構造約束前面 bindService 聲明
的連接:
在 onServiceConnected()方法中,你建立了一個使用 IGTalkService.Stub 的
EditText ?messageText;
ListView ?messageList;
IGTalkSession ?myIGTalkSession;
EditText ?messageTo;
Button ?sendButton;
myIGTalkSession ?= ?null;
messageText ?= ?(EditText) ?findViewById(R.id.messageText);
messageList ?= ?(ListView) ?findViewById(R.id.messageList);
messageTo ?= ?(EditText) ?findViewById(R.id.messageTo);
sendButton ?= ?(Button) ?findViewById(R.id.btnSend);
sendButton.setOnClickListener(this);
messageList.setBackgroundColor(Color.GRAY ?);
this.bindService(new
Intent().setComponent(GTalkServiceConstants.GTALK_SERVICE_COMPONENT),
connection, ?0);
private ?ServiceConnection ?connection ?= ?new ?ServiceConnection() ?{
public ?void ?onServiceConnected(ComponentName ?name, ?IBinder ?service) ?{
try ?{
myIGTalkSession ?=
IGTalkService.Stub.asInterface(service).getDefaultSession();
} ?catch ?(DeadObjectException ?e) ?{
myIGTalkSession ?= ?null;
}
}
public ?void ?onServiceDisconnected(ComponentName ?name) ?{
myIGTalkSession ?= ?null;
}
};141
片段。如果這個過程失敗,你需要再次把這個片段設為 null。現在,可以為類
的 onClick 事件創建代碼。在每一個 onClick 事件中你應當執行一些行動:
1. ?為任何的信息檢查數據庫。
2. ?從查詢的結果中創建一個 ListAdapter 并顯示到 ListView 中。
3. ?創建一個 ChatSession 到 EditView 中的地址并發送你的信息文本。
注意
Android 服務器包括 SQLite 數據庫,你可以使用來保留任何你認為需要放入的
活動相關條目和任何的定制數據。這個數據庫在第十一章做深入的介紹。
下面的代碼為你和接受者之間發生的信息查詢數據庫:
使用 下 面 的 代 碼 來 從 查 詢 結 果 創 建 一 個 ListAdapter 并且 賦 值 接 收 器 到
ListView。在前一個活動,你已經使用了一個類似的過程,所以,對你應該不陌
生。
信息可以顯示了,最后的步驟是發送你的信息。下面的代碼用定義的 messageTO
address 創建一個 IchatSession。這個信息文本然后被從這里傳遞到接受者。
所有的東西在一起,完成后的 GoogleAPI.java 文件如下:
Cursor ?cursor ?= ?managedQuery(Im.Messages.CONTENT_URI, ?null,
"contact=\'" ?+ ?messageTo.getText().toString() ?+ ?"\'", ?null, ?null);
ListAdapter ?adapter ?= ?new ?SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, ?cursor,
new ?String[]{Im.MessagesColumns.BODY},
new ?int[]{android.R.id.text1});
this.messageList.setAdapter(adapter);
try ?{
IChatSession ?chatSession;
chatSession ?=
myIGTalkSession.createChatSession(messageTo.getText().toString(););
chatSession.sendTextMessage(messageText.getText().toString());
} ?catch ?(DeadObjectException ?ex) ?{
myIGTalkSession ?= ?null;
}
package ?android_programmers_guide.GoogleAPI;
import ?android.app.Activity;
import ?android.content.ComponentName;
import ?android.content.Intent;
import ?android.content.ServiceConnection;
import ?android.database.Cursor;
import ?android.os.Bundle;142
import ?android.os.DeadObjectException;
import ?android.os.IBinder;
import ?android.provider.Im;
import ?android.graphics.Color;
import ?android.view.View;
import ?android.widget.EditText;
import ?android.widget.ListView;
import ?android.widget.ListAdapter;
import ?android.widget.Button;
import ?android.widget.SimpleCursorAdapter;
import ?com.google.android.gtalkservice.IGTalkSession;
import ?com.google.android.gtalkservice.IGTalkService;
import ?com.google.android.gtalkservice.GTalkServiceConstants;
import ?com.google.android.gtalkservice.IChatSession;
public ?class ?GoogleAPI ?extends ?Activity ?implements ?View.OnClickListener ?{
EditText ?messageText;
ListView ?messageList;
IGTalkSession ?myIGTalkSession;
EditText ?messageTo;
Button ?mSend;
@Override
public ?void ?onCreate(Bundle ?icicle) ?{
super.onCreate(icicle);
setContentView(R.layout.main);
myIGTalkSession ?= ?null;
messageText ?= ?(EditText) ?findViewById(R.id.messageText);
messageList ?= ?(ListView) ?findViewById(R.id.messageList);
messageTo ?= ?(EditText) ?findViewById(R.id.messageTo);
mSend ?= ?(Button) ?findViewById(R.id.btnSend);
mSend.setOnClickListener(this);
messageList.setBackgroundColor(Color.GRAY ?);
Chapter ?10: ?Using ?the ?Google ?API ?with ?GTalk ?251
this.bindService(new
Intent().setComponent(GTalkServiceConstants.GTALK_SERVICE_COMPONENT),
connection, ?0);
}
private ?ServiceConnection ?connection ?= ?new ?ServiceConnection() ?{
public ?void ?onServiceConnected(ComponentName ?name, ?IBinder ?service) ?{
try ?{
myIGTalkSession ?=
IGTalkService.Stub.asInterface(service).getDefaultSession();
} ?catch ?(DeadObjectException ?e) ?{
myIGTalkSession ?= ?null;
}143
編譯并運行 GoogleAPI
編譯并運行 GoogleAPI GoogleAPI GoogleAPI GoogleAPI 第十章(3) ?(3) ?(3) ?(3)
現在,在模擬器中運行 GoogleAPI。如何你的連接成功,你應當能看見下面的屏
幕。
要測試活動,我發送“hello”信息給 ?androidprogrammersguide@gmail.com,
顯示如下:
}
public ?void ?onServiceDisconnected(ComponentName ?name) ?{
myIGTalkSession ?= ?null;
}
};
public ?void ?onClick(View ?view) ?{
Cursor ?cursor ?= ?managedQuery(Im.Messages.CONTENT_URI, ?null,
"contact=\'" ?+ ?messageTo.getText().toString() ?+ ?"\'",
null, ?null);
ListAdapter ?adapter ?= ?new ?SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, ?cursor,
new ?String[]{Im.MessagesColumns.BODY},
new ?int[]{android.R.id.text1});
this.messageList.setAdapter(adapter);
try ?{
IChatSession ?chatSession;
chatSession ?=
myIGTalkSession.createChatSession(messageTo.getText().toString());
chatSession.sendTextMessage(messageText.getText().toString());
} ?catch ?(DeadObjectException ?ex) ?{
myIGTalkSession ?= ?null;
}
}
}144
下一個插圖,你將看到點擊發送按鈕后,移動我發送的信息到 ListView。
當我作為 androidprogrammersguide 登錄,我發現信息確實到了預想的收件人,
如下:
我回復“Greetings!”,要查看這個,看下面的兩個圖。注意活動屏幕頂部的信
息條。緊接的圖,你可以看到發送者的信息被顯示。下一章,將創建最后一個應用程序,你會在 Google 地圖上使用 SQLite 數據庫和
Google 地圖 Overlays 來繪制數據記錄。這些是非常有力的技術來提升 Android
超越其它的移動操作系統。
試試這個:為 GoogleAPI 活動增加設置特性
試試這個:為 GoogleAPI GoogleAPI GoogleAPI GoogleAPI 活動增加設置特性 ?第十章(4) ?(4) ?(4) ?(4)
編輯 GoogleAPI 活動來包括一個設置特性。使用第八章的 AndroidViews 活動作
為向導,增加一個可以改變應用程序布局屬性按鈕到 GoogleAPI 活動。這里是一
些提示關于如何設置按鈕:
● ?改變信息列表的字體
● ?改變信息列表的字體顏色,使得發送和接受相反。
● ?改變信息列表的背景色。
問專家
Q:GTalk ?API 可用于其它基于 XMPP 的聊天客戶端嗎?
A: ?對于這個的回答還不清晰。m3-rc22 版本的 SDK 包括一個 XMPP ?API 而 M5-15
SDK 只含了 GTalk ?API。有可能這兩者在將來發布的 Android ?SDK 綁在一起。那
樣,GTalk ?API 可以被用來與其它基于 XMPP 聊天客戶端的程序通信。
總結
以上是生活随笔為你收集整理的23、Android之使用 Google API 的 GTalk的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UE4学习记录(2)—— ESC游戏菜单
- 下一篇: word无法启动转换器html32,三种