Weex Android 接入环信聊天功能(二)会话列表页面

更新日期: 2019-07-07 阅读次数: 17129 分类: weex

环信的文档真是不靠谱,需要不断地查解决方案

Weex Android 接入环信聊天功能, 会话列表页面

Failed to open database '/storage/emulated/0/emlibs/libs/monitor.db'.

6.0的手机系统需要去动态获取权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Android6.0 以上的环境会崩溃,这是因为 Google 出于安全考虑在6.0之后加入动态权限,即敏感操作需要获得用户的授权。

复制环信示例代码的 runtimepermissions 文件夹到项目中,然后在ConversationListActivity 中加入下面的代码

/**
         * 请求所有必要的权限----原理就是获取清单文件中申请的权限
         */
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
//              Toast.makeText(MainActivity.this, "All permissions have been granted", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(String permission) {
                //Toast.makeText(MainActivity.this, "Permission " + permission + " has been denied", Toast.LENGTH_SHORT).show();
            }
        });

Error:(45, 150) error: cannot find symbol method getUserName()

conversation.getUserName()

需要修改成

conversation.conversationId()

原因是环信 SDK 3.3.3 接口名字更新了,呵呵。。。

会话列表显示为白屏

是否跟布局没有挂载点有关?的确如此

getSupportFragmentManager().beginTransaction().add(R.id.container, conversationListFragment).commit();

这说明,Fragment 必须在 layout 中必须对应一个挂载点

实现代码

package com.alibaba.weex;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.weex.runtimepermissions.PermissionsManager;
import com.alibaba.weex.runtimepermissions.PermissionsResultAction;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.ui.EaseConversationListFragment;

public class ConversationListActivity extends FragmentActivity {

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

        /**
         * 请求所有必要的权限----原理就是获取清单文件中申请的权限
         */
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
//              Toast.makeText(MainActivity.this, "All permissions have been granted", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(String permission) {
                //Toast.makeText(MainActivity.this, "Permission " + permission + " has been denied", Toast.LENGTH_SHORT).show();
            }
        });

        EMClient.getInstance().login("test1", "123456", new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                Log.d("main", "登录聊天服务器成功!");
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登录聊天服务器失败!");
            }
        });

        EaseConversationListFragment conversationListFragment = new EaseConversationListFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.container, conversationListFragment).commit();

        conversationListFragment.setConversationListItemClickListener(new EaseConversationListFragment.EaseConversationListItemClickListener() {

            @Override
            public void onListItemClicked(EMConversation conversation) {
                startActivity(new Intent(ConversationListActivity.this, ConversationActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, conversation.conversationId()));
            }
        });
    }
}

微信关注我哦 👍

大象工具微信公众号

我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式