Android聊天軟件開發(基於網易雲IM即時通訊)——環境搭建(一)

去網易雲註冊賬號並登陸

https://app.yunxin.163.com/index?clueFrom=nim&from=nim#/create

 添加依賴

ndk {
    //設置支持的SO庫架構
    abiFilters "armeabi-v7a", "x86","arm64-v8a","x86_64"
}
implementation fileTree(dir: 'libs', include: '*.jar')
// 添加依賴。注意,版本號必須一致。
// 基礎功能 (必需)
implementation 'com.netease.nimlib:basesdk:6.1.1'
// 音視頻和互動白板服務需要
implementation 'com.netease.nimlib:nrtc:6.1.1'
// 音視頻需要
implementation 'com.netease.nimlib:avchat:6.1.1'
// 聊天室需要
implementation 'com.netease.nimlib:chatroom:6.1.1'
// 互動白板服務需要
implementation 'com.netease.nimlib:rts:6.1.1'
// 全文檢索服務需要
implementation 'com.netease.nimlib:lucene:6.1.1'
// 小米、華爲、魅族、fcm 推送
implementation 'com.netease.nimlib:push:6.1.1'

添加完依賴,同步一下

如果添加依賴失敗,可參考下面解決方案

AndroidManifest.xml配置 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="heath.com.chat" >

    <!-- 權限聲明 -->
    <!-- 訪問網絡狀態-->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!-- 控制呼吸燈,振動器等,用於新消息提醒 -->
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <!-- 外置存儲存取權限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- 8.0 系統需要-->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <!-- 多媒體相關 -->
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <!-- SDK 權限申明, 第三方 APP 接入時,請將 heath.com.chat 替換爲自己的包名 -->
    <permission
        android:name="heath.com.chat.permission.RECEIVE_MSG"
        android:protectionLevel="signature"/>
    <!-- 接收 SDK 消息廣播權限, 第三方 APP 接入時,請將 heath.com.chat 替換爲自己的包名 -->
    <uses-permission android:name="heath.com.chat.permission.RECEIVE_MSG"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- APP key, 可以在這裏設置,也可以在 SDKOptions 中提供。
            如果 SDKOptions 中提供了,取 SDKOptions 中的值。 -->
        <!--本項目是在這裏進行的配置-->
        <meta-data
            android:name="com.netease.nim.appKey"
            android:value="這裏填寫上面拿到的值" />

        <!-- 雲信後臺服務,請使用獨立進程。 -->
        <service
            android:name="com.netease.nimlib.service.NimService"
            android:process=":core"/>

        <!-- 雲信後臺輔助服務 -->
        <service
            android:name="com.netease.nimlib.service.NimService$Aux"
            android:process=":core"/>

        <!-- 雲信後臺輔助服務 -->
        <service
            android:name="com.netease.nimlib.job.NIMJobService"
            android:exported="true"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:process=":core"/>

        <!-- 雲信監視系統啓動和網絡變化的廣播接收器,保持和 NimService 同一進程 -->
        <receiver android:name="com.netease.nimlib.service.NimReceiver"
            android:process=":core"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
        </receiver>

        <!-- 雲信進程間通信 Receiver -->
        <receiver android:name="com.netease.nimlib.service.ResponseReceiver"/>

        <!-- 雲信進程間通信service -->
        <service android:name="com.netease.nimlib.service.ResponseService"/>

        <!-- 雲信進程間通信provider -->
        <!-- 雲信進程間通信provider -->
        <!-- android:authorities="{包名}.ipc.provider", 請將heath.com.chat替換爲自己的包名 -->
        <provider
            android:name="com.netease.nimlib.ipc.NIMContentProvider"
            android:authorities="heath.com.chat.ipc.provider"
            android:exported="false"
            android:process=":core" />
    </application>

</manifest>

初始化

MyApplication
package heath.com.chat.application;

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.SDKOptions;
import com.netease.nimlib.sdk.StatusBarNotificationConfig;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.mixpush.MixPushConfig;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.uinfo.UserInfoProvider;
import com.netease.nimlib.sdk.uinfo.model.UserInfo;
import com.netease.nimlib.sdk.util.NIMUtil;

public class MyApplication extends Application {


    @Override
    public void onCreate() {
        // SDK初始化(啓動後臺服務,若已經存在用戶登錄信息, SDK 將完成自動登錄)
        NIMClient.init(this, loginInfo(), options());

        // ... your codes
        if (NIMUtil.isMainProcess(this)) {
            // 注意:以下操作必須在主進程中進行
            // 1、UI相關初始化操作
            // 2、相關Service調用
        }
        super.onCreate();
    }

    // 如果返回值爲 null,則全部使用默認參數。
    private SDKOptions options() {
        MixPushConfig mixPushConfig = new MixPushConfig();
        mixPushConfig.hwCertificateName = "C1:DE:25:DB:F4:11:BB:74:15:37:E4:CD:5F:B4:51:EE:DA:F9:82:FC:B0:18:67:EB:88:CC:2C:93:5C:1C:6E:F1";
        SDKOptions options = new SDKOptions();

        // 如果將新消息通知提醒託管給 SDK 完成,需要添加以下配置。否則無需設置。
        StatusBarNotificationConfig config = new StatusBarNotificationConfig();
//        config.notificationEntrance = TabHostActivity.class; // 點擊通知欄跳轉到該Activity
//        config.notificationSmallIconId = R.drawable.logo;
        // 呼吸燈配置
        config.ledARGB = Color.GREEN;
        config.ledOnMs = 1000;
        config.ledOffMs = 1500;
        // 通知鈴聲的uri字符串
        config.notificationSound = "android.resource://com.netease.nim.demo/raw/msg";
        options.statusBarNotificationConfig = config;
        options.mixPushConfig = mixPushConfig;

        // 配置保存圖片,文件,log 等數據的目錄
        // 如果 options 中沒有設置這個值,SDK 會使用採用默認路徑作爲 SDK 的數據目錄。
        // 該目錄目前包含 log, file, image, audio, video, thumb 這6個目錄。
        String sdkPath = Environment.getExternalStorageDirectory() + "/" + getPackageName() + "/nim"; // 可以不設置,那麼將採用默認路徑
        // 如果第三方 APP 需要緩存清理功能, 清理這個目錄下面個子目錄的內容即可。
        options.sdkStorageRootPath = sdkPath;

        // 配置是否需要預下載附件縮略圖,默認爲 true
        options.preloadAttach = true;

        // 配置附件縮略圖的尺寸大小。表示向服務器請求縮略圖文件的大小
        // 該值一般應根據屏幕尺寸來確定, 默認值爲 Screen.width / 2
        options.thumbnailSize = 480 / 2;
        options.userInfoProvider = new UserInfoProvider() {

            @Override
            public UserInfo getUserInfo(String account) {
                return null;
            }

            @Override
            public String getDisplayNameForMessageNotifier(String account, String sessionId, SessionTypeEnum sessionType) {
                return null;
            }

            @Override
            public Bitmap getAvatarForMessageNotifier(SessionTypeEnum sessionType, String sessionId) {
                return null;
            }
        };

        return options;
    }

    // 如果已經存在用戶登錄信息,返回LoginInfo,否則返回null即可
    private LoginInfo loginInfo() {
        return null;
    }

}

在AndroidManifest.xml添加

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    >

    <LinearLayout
        android:id="@+id/ll_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="80dp"
            android:layout_marginRight="15dp"
            android:orientation="vertical">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginBottom="40dp"
                android:contentDescription="@string/tv_icon_des"
                android:src="@drawable/logo" />

            <heath.com.chat.utils.ClearEditText
                android:id="@+id/et_account"
                android:layout_width="fill_parent"
                android:layout_height="60dp"
                android:hint="@string/et_account_hint"
                android:inputType="number"
                android:textColor="@color/white"
                android:textColorHint="@color/gainsboro"
                android:textCursorDrawable="@drawable/cursor_color"
                android:theme="@style/MyEditText" />

            <heath.com.chat.utils.ClearEditText
                android:id="@+id/et_passwords"
                android:layout_width="fill_parent"
                android:layout_height="60dp"
                android:hint="@string/et_passwords_hint"
                android:inputType="textPassword"
                android:textColor="@color/white"
                android:textColorHint="@color/gainsboro"
                android:textCursorDrawable="@drawable/cursor_color"
                android:theme="@style/MyEditText" />

            <Button
                android:id="@+id/btn_login"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:layout_marginTop="15dp"
                android:background="@color/deepskyblue"
                android:text="@string/tv_login"
                android:textColor="@color/white"
                />

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/forget_password"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:text="@string/tv_forget_password"
                    android:textColor="@color/deepskyblue" />

                <TextView
                    android:id="@+id/register"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:text="@string/tv_register1"
                    android:textColor="@color/deepskyblue" />

            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rl_loading"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:background="@drawable/shape_label_clarity_black">

        <com.github.ybq.android.spinkit.SpinKitView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/pb_loading"
            style="@style/SpinKitView.Large.Circle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            app:SpinKit_Color="@color/white" />

        <TextView
            android:id="@+id/tv_loading_text"
            android:layout_below="@id/pb_loading"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            />

    </RelativeLayout>

</FrameLayout>

 LoginActivity

package heath.com.chat;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;

import heath.com.chat.utils.LoadingUtils;


public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtAccount;
    private EditText mEtPassword;
    private Button mBtnLogin;
    protected LoadingUtils loadingUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initListener();
    }

    private void initView() {
        mEtAccount = this.findViewById(R.id.et_account);
        mEtPassword = this.findViewById(R.id.et_passwords);
        mBtnLogin = this.findViewById(R.id.btn_login);
        loadingUtils = new LoadingUtils(LoginActivity.this, "登錄中");
        loadingUtils.creat();
    }

    private void initListener() {
        mBtnLogin.setOnClickListener(this);
    }

    private void loginIM(String account, String token) {
        LoginInfo info = new LoginInfo(account, token); // config...
        RequestCallback<LoginInfo> callback =
                new RequestCallback<LoginInfo>() {

                    @Override
                    public void onException(Throwable arg0) {
                        System.out.println("--------------------------------");
                        System.out.println(arg0);
                    }

                    @Override
                    public void onFailed(int code) {
                        loadingUtils.dismiss();
                        if (code == 302) {
                            Toast.makeText(LoginActivity.this, "用戶名或密碼錯誤", Toast.LENGTH_SHORT).show();
                        } else if (code == 408) {
                            Toast.makeText(LoginActivity.this, "登錄超時", Toast.LENGTH_SHORT).show();
                        } else if (code == 415) {
                            Toast.makeText(LoginActivity.this, "未開網絡", Toast.LENGTH_SHORT).show();
                        } else if (code == 416) {
                            Toast.makeText(LoginActivity.this, "連接有誤,請稍後重試", Toast.LENGTH_SHORT).show();
                        } else if (code == 417) {
                            Toast.makeText(LoginActivity.this, "該賬號已在另一端登錄", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(LoginActivity.this, "未知錯誤,請稍後重試", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onSuccess(LoginInfo loginInfo) {
                        Log.e("TAG", "onSuccess: " + loginInfo + "======================================================");
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }
                };
        NIMClient.getService(AuthService.class).login(info)
                .setCallback(callback);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String account = mEtAccount.getText().toString().toLowerCase();
                String token = mEtPassword.getText().toString();
                loadingUtils.show();
                loginIM(account, token);
                break;
        }
    }
}

這裏先講手動創建賬戶,下一章會講在線註冊賬號

項目下載地址:https://download.csdn.net/download/qq_32090185/11122479

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