Android云信的接入 简单功能实现

导入依赖

compile 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’

权限

<!-- 访问网络状态-->
<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"/>

<!-- 如果需要实时音视频通话模块,下面的权限也是必须的。否则,可以不加 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
 <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />

<!-- SDK 权限申明, 第三方 APP 接入时,请将 com.netease.nim.demo 替换为自己的包名 -->
<!-- 和下面的 uses-permission 一起加入到你的 AndroidManifest 文件中。 -->
<permission
    android:name="com.netease.nim.demo.permission.RECEIVE_MSG"
    android:protectionLevel="signature"/>
<!-- 接收 SDK 消息广播权限, 第三方 APP 接入时,请将 com.netease.nim.demo 替换为自己的包名 -->
 <uses-permission android:name="com.netease.nim.demo.permission.RECEIVE_MSG"/>

application

    <!-- 云信后台服务,请使用独立进程。 -->
    <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 -->
    <!-- android:authorities="{包名}.ipc.provider", 请将com.netease.nim.demo替换为自己的包名 -->
    <provider
        android:name="com.netease.nimlib.ipc.NIMContentProvider"
        android:authorities="com.netease.nim.demo.ipc.provider"
        android:exported="false"
        android:process=":core" />

初始化

public class NimApplication extends Application {

/**
     * 注意:每个进程都会创建自己的Application 然后调用onCreate() 方法,
     * 如果用户有自己的逻辑需要写在Application#onCreate()(还有Application的其他方法)中,一定要注意判断进程,不能把业务逻辑写在core进程,
     * 理论上,core进程的Application#onCreate()(还有Application的其他方法)只能做与im sdk 相关的工作
     */
    public void onCreate() {
        // ... your codes

        // SDK初始化(启动后台服务,若已经存在用户登录信息, SDK 将完成自动登录)
        NIMClient.init(this, loginInfo(), options());

        // ... your codes
        if (NIMUtil.isMainProcess(this)) {
            // 注意:以下操作必须在主进程中进行
            // 1、UI相关初始化操作
            // 2、相关Service调用
        }
    }

    // 如果返回值为 null,则全部使用默认参数。
    private SDKOptions options() {
        SDKOptions options = new SDKOptions();

        // 如果将新消息通知提醒托管给 SDK 完成,需要添加以下配置。否则无需设置。
        StatusBarNotificationConfig config = new StatusBarNotificationConfig();
        config.notificationEntrance = WelcomeActivity.class; // 点击通知栏跳转到该Activity
        config.notificationSmallIconId = R.drawable.ic_stat_notify_msg;
        // 呼吸灯配置
        config.ledARGB = Color.GREEN;
        config.ledOnMs = 1000;
        config.ledOffMs = 1500;
        // 通知铃声的uri字符串
        config.notificationSound = "android.resource://com.netease.nim.demo/raw/msg";
        options.statusBarNotificationConfig = config;

        // 配置保存图片,文件,log 等数据的目录
        // 如果 options 中没有设置这个值,SDK 会使用采用默认路径作为 SDK 的数据目录。
        // 该目录目前包含 log, file, image, audio, video, thumb 这6个目录。
        String sdkPath = getAppCacheDir(context) + "/nim"; // 可以不设置,那么将采用默认路径
        // 如果第三方 APP 需要缓存清理功能, 清理这个目录下面个子目录的内容即可。
        options.sdkStorageRootPath = sdkPath;

        // 配置是否需要预下载附件缩略图,默认为 true
        options.preloadAttach = true;

        // 配置附件缩略图的尺寸大小。表示向服务器请求缩略图文件的大小
        // 该值一般应根据屏幕尺寸来确定, 默认值为 Screen.width / 2
        options.thumbnailSize = ${Screen.width} / 2;

        // 用户资料提供者, 目前主要用于提供用户资料,用于新消息通知栏中显示消息来源的头像和昵称
        options.userInfoProvider = new UserInfoProvider() {
             @Override
             public UserInfo getUserInfo(String account) {
                 return null;
             }

             @Override
             public int getDefaultIconResId() {
                 return R.drawable.avatar_def;
             }

             @Override
             public Bitmap getTeamIcon(String tid) {
                 return null;
             }

             @Override
             public Bitmap getAvatarForMessageNotifier(String account) {
                  return null;
             }

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

    // 如果已经存在用户登录信息,返回LoginInfo,否则返回null即可
    private LoginInfo loginInfo() {
        return null;
    }

    /**
     * 配置 APP 保存图片/语音/文件/log等数据的目录
     * 这里示例用SD卡的应用扩展存储目录
     */
    static String getAppCacheDir(Context context) {
        String storageRootPath = null;
        try {
            // SD卡应用扩展存储区(APP卸载后,该目录下被清除,用户也可以在设置界面中手动清除),请根据APP对数据缓存的重要性及生命周期来决定是否采用此缓存目录.
            // 该存储区在API 19以上不需要写权限,即可配置 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
            if (context.getExternalCacheDir() != null) {
                storageRootPath = context.getExternalCacheDir().getCanonicalPath();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(storageRootPath)) {
            // SD卡应用公共存储区(APP卸载后,该目录不会被清除,下载安装APP后,缓存数据依然可以被加载。SDK默认使用此目录),该存储区域需要写权限!
            storageRootPath = Environment.getExternalStorageDirectory() + "/" + DemoCache.getContext().getPackageName();
        }

        return storageRootPath;
    }
};

报错或用不着的地方删除即可 多看官方继承文档中的参数意思
链接: https://dev.yunxin.163.com/docs/product/IM即时通讯/SDK开发集成/Android开发集成/初始化

登录

登录布局界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#F6F6F6">

    <LinearLayout
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:gravity="center">

        <ImageView
            android:src="@mipmap/redface"
            android:layout_width="160dp"
            android:layout_height="160dp" />
        <EditText
            android:id="@+id/ef_zh"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:hint="账号"/>
        <EditText
            android:id="@+id/ef_mm"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:hint="密码"/>
        <Button
            android:id="@+id/btn_dl"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="登录"/>

    </LinearLayout>

</LinearLayout>

Java代码

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText ef_zh;
    private EditText ef_mm;
    private Button btn_dl;

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

    public void doLogin(LoginInfo info) {
        RequestCallback<LoginInfo> callback =
                new RequestCallback<LoginInfo>() {
                    @Override
                    public void onSuccess(LoginInfo param) {
                        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onFailed(int code) {

                    }

                    @Override
                    public void onException(Throwable exception) {

                    }
                    // 可以在此保存LoginInfo到本地,下次启动APP做自动登录用
                };
        NIMClient.getService(AuthService.class).login(info)
                .setCallback(callback);
    }

    private void initView() {
        ef_zh = (EditText) findViewById(R.id.ef_zh);
        ef_mm = (EditText) findViewById(R.id.ef_mm);
        btn_dl = (Button) findViewById(R.id.btn_dl);

        btn_dl.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dl:
                doLogin(new LoginInfo(ef_zh.getText().toString(),ef_mm.getText().toString()));   //填入自己的注册账号即可
                break;
        }
    }
}

单聊

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:orientation="horizontal">

        <ListView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/m2lv1"
            android:layout_weight="8">

        </ListView>
        <ListView
            android:id="@+id/m2lv2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"></ListView>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/m2et1"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/m2btn1"
            android:text="发送消息"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/m2btn2"
            android:text="添加好友"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="展示好友"
            android:id="@+id/m2btn3"/>

    </LinearLayout>

</LinearLayout>

java代码

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.netease.nimlib.sdk.InvocationFuture;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.friend.FriendService;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.MsgServiceObserve;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    // 该帐号为示例,请先注册
    String account = "测试";
    // 以单聊类型为例
    SessionTypeEnum sessionType = SessionTypeEnum.P2P;
    String text;
    private EditText m2et1;
    private Button m2btn1,m2btn2,m2btn3;
    ListView m2lv1,m2lv2;
    final ArrayList<M2Bean> arrayList =  new ArrayList<>();
    String peopleid;
    String s;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        m2lv1 = findViewById(R.id.m2lv1);
        m2lv2 = findViewById(R.id.m2lv2);

        initView();


        Observer<List<IMMessage>> incomingMessageObserver =
                new Observer<List<IMMessage>>() {
                    @Override
                    public void onEvent(List<IMMessage> messages) {
                        // 处理新收到的消息,为了上传处理方便,SDK 保证参数 messages 全部来自同一个聊天对象。
                        String s = messages.get(0).getContent().toString();
                        Toast.makeText(Main2Activity.this, ""+s, Toast.LENGTH_SHORT).show();
                        M2Bean m2Bean = new M2Bean(R.mipmap.j2, s,true);
                        arrayList.add(m2Bean);
                        M2Adapter m2Adapter = new M2Adapter(Main2Activity.this,arrayList);
                        m2lv1.setAdapter(m2Adapter);
                    }
                };
        NIMClient.getService(MsgServiceObserve.class)
                .observeReceiveMessage(incomingMessageObserver, true);


    }

    public  IMMessage createTextMessage(String account, SessionTypeEnum sessionType, final String text) {

        IMMessage textMessage = MessageBuilder.createTextMessage(account, sessionType, text);

        InvocationFuture<Void> voidInvocationFuture = NIMClient.getService(MsgService.class).sendMessage(textMessage, true);

        voidInvocationFuture.setCallback(new RequestCallback<Void>() {
            @Override
            public void onSuccess(Void param) {
                Log.d("消息","发送成功");
            }

            @Override
            public void onFailed(int code) {

                Log.d("消息","发送失败:"+code);

            }

            @Override
            public void onException(Throwable exception) {
                Log.d("消息","好友不在线");
            }
        });
        return textMessage;
    }

    private void initView() {
        m2et1 = (EditText) findViewById(R.id.m2et1);
        m2btn1 = (Button) findViewById(R.id.m2btn1);
        m2btn2 = (Button) findViewById(R.id.m2btn2);
        m2btn3 = (Button) findViewById(R.id.m2btn3);

        m2btn1.setOnClickListener(this);
        m2btn2.setOnClickListener(this);
        m2btn3.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.m2btn1:

//                if(peopleid==null){
//                    Toast.makeText(this, "未选择联系人!", Toast.LENGTH_SHORT).show();
//                }
                 s = m2et1.getText().toString();
                createTextMessage("123456", sessionType, s);
                M2Bean m2Bean = new M2Bean(R.mipmap.j1, text,false);
                arrayList.add(m2Bean);
                M2Adapter m2Adapter = new M2Adapter(Main2Activity.this,arrayList);
                m2lv1.setAdapter(m2Adapter);
                m2et1.setText("");
                break;
            case R.id.m2btn2:
                NIMClient.getService(FriendService.class).ackAddFriendRequest(m2et1.getText().toString(), true).setCallback(new RequestCallback<Void>() {
                    @Override
                    public void onSuccess(Void param) {
                        Toast.makeText(Main2Activity.this, "添加成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailed(int code) {
                        Toast.makeText(Main2Activity.this, ""+code, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onException(Throwable exception) {
                        Toast.makeText(Main2Activity.this, ""+exception.getMessage().toString(), Toast.LENGTH_SHORT).show();
                    }
                });
                m2et1.setText("");
                break;
            case R.id.m2btn3:
                List<String> friends = NIMClient.getService(FriendService.class).getFriendAccounts();
                ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(Main2Activity.this, R.layout.support_simple_spinner_dropdown_item, friends);
                m2lv2.setAdapter(stringArrayAdapter);
                break;
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值