前期準備:
注冊開發者帳号
開發者在內建融雲即時通訊、實時網絡能力前,需前往融雲官方網站注冊建立融雲開發者帳号。App Key這個有用,後面代碼裡我會講到。

下載下傳 SDK
IMKit 內建了會話界面,并且提供了豐富的自定義功能,我們推薦首次接觸融雲的客戶直接使用 IMKit 來快速內建和開發。
IMLib 提供了基礎的通信能力,較輕量,适用于對 UI 有較高訂制需求的開發者,但您需要自己去實作大量的界面和功能。
導入 SDK
以 Module 形式導入剛下載下傳的 IMKit和IMLib。
打開您的工程, File -> New -> Import Module
打開您從官網下載下傳的融雲 SDK,選擇 IMLib。如圖:
添加配置
打開應用的 build.gradle,在 dependencies 中添加相應子產品的依賴。如圖:
注意:是打開 IMLib Module 的 AndroidManifest.xml 檔案,把 meta-data RONG_CLOUD_APP_KEY 的值修改為您自己的 AppKey. 如圖:
<meta-data
android:name="RONG_CLOUD_APP_KEY"
android:value="您的應用 AppKey" />
初始化
建立一個APP 類繼承Application,然後在配置檔案指定name屬性。在 App 主程序初始化,您隻需要實作一句函數實作初始化:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
RongIM.init(this);
}
}
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
擷取 Token 方法
可以檢視官網 擷取 Token的方法。
連接配接伺服器
/**
* <p>連接配接伺服器,在整個應用程式全局,隻需要調用一次,需在 {@link #init(Context)} 之後調用。</p>
* <p>如果調用此接口遇到連接配接失敗,SDK 會自動啟動重連機制進行最多10次重連,分别是1, 2, 4, 8, 16, 32, 64, 128, 256, 512秒後。
* 在這之後如果仍沒有連接配接成功,還會在當檢測到裝置網絡狀态變化時再次進行重連。</p>
*
* @param token 從服務端擷取的使用者身份令牌(Token)。
* @param callback 連接配接回調。
* @return RongIM 用戶端核心類的執行個體。
*/
private void connect(String token) {
if (getApplicationInfo().packageName.equals(App.getCurProcessName(getApplicationContext()))) {
RongIM.connect(token, new RongIMClient.ConnectCallback() {
/**
* Token 錯誤。可以從下面兩點檢查 1. Token 是否過期,如果過期您需要向 App Server 重新請求一個新的 Token
* 2. token 對應的 appKey 和工程裡設定的 appKey 是否一緻
*/
@Override
public void onTokenIncorrect() {
}
/**
* 連接配接融雲成功
* @param userid 目前 token 對應的使用者 id
*/
@Override
public void onSuccess(String userid) {
Log.d("LoginActivity", "--onSuccess" + userid);
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
/**
* 連接配接融雲失敗
* @param errorCode 錯誤碼,可到官網 檢視錯誤碼對應的注釋
*/
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});
}
}
配置會話清單
融雲 IMKit SDK 使用了 Fragment 作為會話清單和會話界面的元件,其優點是支援各種嵌套方式,更符合您的定制化需求。 下面說明如何在 Activity 裡以靜态方式加載融雲 Fragment.
配置布局檔案
這是您的會話清單 Activity 對應的布局檔案:conversationlist.xml。注意 android:name 固定為融雲的 ConversationListFragment。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/conversationlist"
android:name="io.rong.imkit.fragment.ConversationListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
建立 Activity
public class ConversationListActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conversationlist);
}
}
配置 intent-filter:
融雲 SDK 是通過隐式調用的方式來實作界面跳轉的。是以您需要在 AndroidManifest.xml 中,您的會話清單 Activity 下面配置 intent-filter,其中,android:host 是您應用的包名,需要手動修改,其他請保持不變。
<!--會話清單-->
<activity
android:name="io.rong.fast.activity.ConversationListActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="io.rong.fast"
android:pathPrefix="/conversationlist"
android:scheme="rong" />
</intent-filter>
</activity>
源碼demo: http://download.csdn.net/download/csdn_aiyang/9955969
歡迎加入QQ群聊,一起探究學習流行技術,騷客們還等什麼!