Android聊天軟件開(kāi)發(fā)(基于網(wǎng)易云IM即時(shí)通訊)——環(huán)境搭建(一)

去網(wǎng)易云注冊(cè)賬號(hào)并登陸

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

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

添加完依賴,同步一下

image

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

image
image

AndroidManifest.xml配置

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

    <!-- 權(quán)限聲明 -->
    <!-- 訪問(wèn)網(wǎng)絡(luò)狀態(tài)-->
    <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" />
    <!-- 控制呼吸燈,振動(dòng)器等烟瞧,用于新消息提醒 -->
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <!-- 外置存儲(chǔ)存取權(quán)限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- 8.0 系統(tǒng)需要-->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <!-- 多媒體相關(guān) -->
    <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 權(quán)限申明, 第三方 APP 接入時(shí)诗鸭,請(qǐng)將 heath.com.chat 替換為自己的包名 -->
    <permission
        android:name="heath.com.chat.permission.RECEIVE_MSG"
        android:protectionLevel="signature"/>
    <!-- 接收 SDK 消息廣播權(quán)限, 第三方 APP 接入時(shí)参滴,請(qǐng)將 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, 可以在這里設(shè)置强岸,也可以在 SDKOptions 中提供。
            如果 SDKOptions 中提供了砾赔,取 SDKOptions 中的值蝌箍。 -->
        <!--本項(xiàng)目是在這里進(jìn)行的配置-->
        <meta-data
            android:name="com.netease.nim.appKey"
            android:value="這里填寫上面拿到的值" />

        <!-- 云信后臺(tái)服務(wù),請(qǐng)使用獨(dú)立進(jìn)程暴心。 -->
        <service
            android:name="com.netease.nimlib.service.NimService"
            android:process=":core"/>

        <!-- 云信后臺(tái)輔助服務(wù) -->
        <service
            android:name="com.netease.nimlib.service.NimService$Aux"
            android:process=":core"/>

        <!-- 云信后臺(tái)輔助服務(wù) -->
        <service
            android:name="com.netease.nimlib.job.NIMJobService"
            android:exported="true"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:process=":core"/>

        <!-- 云信監(jiān)視系統(tǒng)啟動(dòng)和網(wǎng)絡(luò)變化的廣播接收器妓盲,保持和 NimService 同一進(jìn)程 -->
        <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>

        <!-- 云信進(jìn)程間通信 Receiver -->
        <receiver android:name="com.netease.nimlib.service.ResponseReceiver"/>

        <!-- 云信進(jìn)程間通信service -->
        <service android:name="com.netease.nimlib.service.ResponseService"/>

        <!-- 云信進(jìn)程間通信provider -->
        <!-- 云信進(jìn)程間通信provider -->
        <!-- android:authorities="{包名}.ipc.provider", 請(qǐng)將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>
image

初始化

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初始化(啟動(dòng)后臺(tái)服務(wù),若已經(jīng)存在用戶登錄信息专普, SDK 將完成自動(dòng)登錄)
        NIMClient.init(this, loginInfo(), options());

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

    // 如果返回值為 null檀夹,則全部使用默認(rèn)參數(shù)筋粗。
    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 完成,需要添加以下配置炸渡。否則無(wú)需設(shè)置娜亿。
        StatusBarNotificationConfig config = new StatusBarNotificationConfig();
//        config.notificationEntrance = TabHostActivity.class; // 點(diǎn)擊通知欄跳轉(zhuǎn)到該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 等數(shù)據(jù)的目錄
        // 如果 options 中沒(méi)有設(shè)置這個(gè)值暇唾,SDK 會(huì)使用采用默認(rèn)路徑作為 SDK 的數(shù)據(jù)目錄。
        // 該目錄目前包含 log, file, image, audio, video, thumb 這6個(gè)目錄。
        String sdkPath = Environment.getExternalStorageDirectory() + "/" + getPackageName() + "/nim"; // 可以不設(shè)置策州,那么將采用默認(rèn)路徑
        // 如果第三方 APP 需要緩存清理功能, 清理這個(gè)目錄下面?zhèn)€子目錄的內(nèi)容即可宫仗。
        options.sdkStorageRootPath = sdkPath;

        // 配置是否需要預(yù)下載附件縮略圖够挂,默認(rèn)為 true
        options.preloadAttach = true;

        // 配置附件縮略圖的尺寸大小。表示向服務(wù)器請(qǐng)求縮略圖文件的大小
        // 該值一般應(yīng)根據(jù)屏幕尺寸來(lái)確定藕夫, 默認(rèn)值為 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;
    }

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

}

image.gif

在AndroidManifest.xml添加

image
image.gif

?

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>
image.gif

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, "用戶名或密碼錯(cuò)誤", Toast.LENGTH_SHORT).show();
                        } else if (code == 408) {
                            Toast.makeText(LoginActivity.this, "登錄超時(shí)", Toast.LENGTH_SHORT).show();
                        } else if (code == 415) {
                            Toast.makeText(LoginActivity.this, "未開(kāi)網(wǎng)絡(luò)", Toast.LENGTH_SHORT).show();
                        } else if (code == 416) {
                            Toast.makeText(LoginActivity.this, "連接有誤毅贮,請(qǐng)稍后重試", Toast.LENGTH_SHORT).show();
                        } else if (code == 417) {
                            Toast.makeText(LoginActivity.this, "該賬號(hào)已在另一端登錄", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(LoginActivity.this, "未知錯(cuò)誤办悟,請(qǐng)稍后重試", 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;
        }
    }
}

這里先講手動(dòng)創(chuàng)建賬戶,下一章會(huì)講在線注冊(cè)賬號(hào)

image

項(xiàng)目下載地址:https://download.csdn.net/download/qq_32090185/11122479

上面項(xiàng)目是小demo滩褥,這個(gè)git地址是完整項(xiàng)目:https://github.com/HeathHwn/MicroChat

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末病蛉,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子瑰煎,更是在濱河造成了極大的恐慌铺然,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件酒甸,死亡現(xiàn)場(chǎng)離奇詭異魄健,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)插勤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門沽瘦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人农尖,你說(shuō)我怎么就攤上這事析恋。” “怎么了卤橄?”我有些...
    開(kāi)封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵绿满,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我窟扑,道長(zhǎng)喇颁,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任嚎货,我火速辦了婚禮橘霎,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘殖属。我一直安慰自己姐叁,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著外潜,像睡著了一般原环。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上处窥,一...
    開(kāi)封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天嘱吗,我揣著相機(jī)與錄音,去河邊找鬼滔驾。 笑死谒麦,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的哆致。 我是一名探鬼主播绕德,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼摊阀!你這毒婦竟也來(lái)了耻蛇?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤驹溃,失蹤者是張志新(化名)和其女友劉穎城丧,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體豌鹤,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡亡哄,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了布疙。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蚊惯。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖灵临,靈堂內(nèi)的尸體忽然破棺而出截型,到底是詐尸還是另有隱情,我是刑警寧澤儒溉,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布宦焦,位于F島的核電站,受9級(jí)特大地震影響顿涣,放射性物質(zhì)發(fā)生泄漏波闹。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一涛碑、第九天 我趴在偏房一處隱蔽的房頂上張望精堕。 院中可真熱鬧,春花似錦蒲障、人聲如沸歹篓。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)庄撮。三九已至背捌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間洞斯,已是汗流浹背载萌。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留巡扇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓垮衷,卻偏偏與公主長(zhǎng)得像厅翔,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子搀突,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容