Android超簡單集成融雲IM單聊

最近項目中需要集成即時通訊,於是就選擇了融雲,按照步驟一步一步來還是很簡單的,我做的只是單聊,(可支持發送文字、表情、圖片、語音)下面記錄一下
先上圖
在這裏插入圖片描述

步驟:

一,去融雲官網註冊賬號然後創建應用,應用創建完成後把自己的App Key記錄下來 我們在配置的時候會用到**

在這裏插入圖片描述

二,第二步下載SDK ,我是直接下載的IMKit,上邊有介紹,能實現基本的功能,可根據自己的需求下載

在這裏插入圖片描述

下載完是這樣的
在這裏插入圖片描述

三,將IMKit和IMLib導入自己的項目中

在這裏插入圖片描述
這樣就導進來了
在這裏插入圖片描述

最好也在App的build.gradle中看一下有沒有引入

在這裏插入圖片描述

四,好了準備工作一切完畢,下面劃重點,初始化融雲。在整個應用程序中,只需要調用一次 init 方法。

public class App extends BaseApplication {
    
    @Override
    public void onCreate() {
        super.onCreate();
      
        RongIM.init(this);
    }

五,打開 IMLib Module 的 AndroidManifest.xml 文件,把RONG_CLOUD_APP_KEY的值修改爲自己的 AppKey. 如圖:

在這裏插入圖片描述

在應用的 App Module 的 AndroidManifest.xml 文件中,添加 FileProvider 相關配置,修改 android:authorities 爲App 的應用的 “ApplicationId”.FileProvider。

在這裏插入圖片描述

六,連接融雲,我是放在程序登錄成功後執行的

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", "融雲連接成功!!!" + userid);
    }
    
    /*連接融雲失敗
     * @param errorCode 錯誤碼,可到官網 查看錯誤碼對應的註釋
     */

    @Override
    public void onError(RongIMClient.ErrorCode errorCode) {
        Log.d("LoginActivity", "融雲連接失敗!!!" + errorCode);
    }
});

七,下面開始配置會話列表先來配置佈局文件 注:name是固定不能修改的

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
        android:layout_marginTop="12dp"/>
</LinearLayout>

在代碼中

  ConversationListFragment listFragment = (ConversationListFragment) ConversationListFragment.instantiate(mContext, ConversationListFragment.class.getName());
Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
        .appendPath("conversationlist")
        .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false")
        .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")
        .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")
        .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")
        .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")
        .build();
listFragment.setUri(uri);
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
//將融雲的Fragment界面加入到我們的頁面。
transaction.add(R.id.conversationlist, listFragment);
transaction.commitAllowingStateLoss();

在AndroidManifest中,融雲 SDK 是通過隱式調用的方式來實現界面跳轉的,您的會話列表 Activity 下面配置 intent-filter,其中,android:host 是您應用的 ApplicationId,需要手動修改,其他請保持不變。

<!-- 會話列表 -->
<activity
    android:name=".activity.New_MainActivity"
    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="替換成您的ApplicationId"
            android:pathPrefix="/conversationlist"
            android:scheme="rong" />
    </intent-filter>
</activity>

八,接下來是新建會話頁面Activity

佈局文件中

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@drawable/bg_doctor_blue"
    android:elevation="3dp"
    >
    <ImageView
        android:id="@+id/back"
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:src="@mipmap/back_white"/>
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="與  對話中"
        android:textSize="16sp"
        android:textColor="@color/bg_white"
        android:gravity="center"
        android:layout_centerInParent="true"/>
</RelativeLayout>

<fragment
    android:id="@+id/conversation"
    android:background="@color/bg_white"
    android:name="io.rong.imkit.fragment.ConversationFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

會話 Activity

 //設置自己的信息
RongIM.getInstance().setCurrentUserInfo(new UserInfo(UserUtils.getUid(), UserUtils.getName(), Uri.parse(UserUtils.getHeader())));
//與我聊天的人的名字
String sName = getIntent().getData().getQueryParameter("title");//獲取暱稱
//與我聊天的人的Uid
String targetId = getIntent().getData().getQueryParameter("targetId");//獲取對方id
title.setText("與" + sName + " 對話中");

配置 intent-filter 在 AndroidManifest.xml 中,會話 Activity 下面配置 intent-filter。 注意請修改 android:host 爲您應用的 ApplicationId,其他保持不變。**

<activity
    android:name=".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="替換成您的ApplicationId"
            android:pathPrefix="/conversation/"
            android:scheme="rong" />
    </intent-filter>
</activity>

寫到這 基本的單聊功能就可以會實現了

九,如何從別的頁面點擊跳轉至會話頁面呢

//跳轉到聊天頁面 傳入對方的id 和 名字
RongIM.getInstance().startPrivateChat(context, TargetUserId, Title);

十,記錄一下按照我們需求要改的地方

1,如何緩存用戶的信息,則通過refreshUserInfoCache(注:如果有好友更換了頭像,需通過refreshUserInfoCache來存儲)

RongIM.getInstance().refreshUserInfoCache(new UserInfo(list.get(Position).getUid() + "", list.get(Position).getName(), Uri.parse(list.get(Position).getAvatar())));

2,融雲默認頭像是方的,如果要改成圓形則需要在IMKit中找到下邊這三個文件

	rc_item_conversation.xml 會話列表 
	rc_item_message.xml 會話頁面 
	rc_item_conversation_member.xml 設置頁面

根據需求替換相應的屬性, 每個文件有兩處需替換

        //圓形
 	app:RCShape="circle"
 	android:scaleType="centerCrop"
 	//方形
 	app:RCShape="square"
	android:scaleType="centerCrop"

3, 如果需要修改聊天氣泡的背景,換成自己的.9圖, 則在IMKit文件夾中替換這些drawable文件

	rc_ic_bubble_right.9.png			rc_ic_bubble_left.9.png
	rc_ic_bubble_no_right.9.png			rc_ic_bubble_no_left.9.png
	rc_ic_bubble_right_file.9.png			rc_ic_bubble_left_file.9.png 

3,如果需要修改會話列表的樣式則在R.layout.rc_item_conversation文件中按需求自定義就可以

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章