Android 使用集成环信EaseUI

1.先在环信官网下载环信的api
2.找到examples中的easeui,可以把easeui中的simpledemo删掉
3.在自己的工程中导入easeui(File→new→import module 然后找到easeui的位置)
4.把easeui依赖到自己的项目中

效果图

Android 使用集成环信EaseUI_第1张图片

权限

 <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

设置环信的key等

 
        <meta-data
            android:name="EASEMOB_APPKEY"
            android:value="1175161212178599#ci" />
        
        <service
            android:name="com.hyphenate.chat.EMChatService"
            android:exported="true" />
        <service
            android:name="com.hyphenate.chat.EMJobService"
            android:exported="true"
            android:permission="android.permission.BIND_JOB_SERVICE" />
        
        <receiver android:name="com.hyphenate.chat.EMMonitorReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            intent-filter>
            
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            intent-filter>
        receiver>

环信初始化 MyApp

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        EMOptions options = new EMOptions();
        // 默认添加好友时,是不需要验证的,改成需要验证
        options.setAcceptInvitationAlways(false);

        EaseUI.getInstance().init(this, options);

    }
}

设置登录,和注册 MainActivity

public class MainActivity extends AppCompatActivity {


    EditText et1, et2;
    Button btn1, btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //登录
                String s1 = et1.getText().toString();
                String s2 = et2.getText().toString();
                if (TextUtils.isEmpty(s1)) {
                    Toast.makeText(getBaseContext(), "账号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(s2)) {
                    Toast.makeText(getBaseContext(), "账号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                //登录
                login(s1, s2);

            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //注册
                String s1 = et1.getText().toString();
                String s2 = et2.getText().toString();
                if (TextUtils.isEmpty(s1)) {
                    Toast.makeText(getBaseContext(), "账号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(s2)) {
                    Toast.makeText(getBaseContext(), "账号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }


                //注册s
                register(s1, s2);
            }
        });

    }


    private void register(final String s1, final String s2) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().createAccount(s1, s2);
                    Log.e("TalkFragment", "注册成功");
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    Log.e("TalkFragment", "注册失败" + e.getMessage());
                }
            }
        }).start();
    }

    private void login(final String s1, final String s2) {
        EMClient.getInstance().login(s1, s2, new EMCallBack() {
            @Override
            public void onSuccess() {
                startActivity(new Intent(getBaseContext(), TalkActivity.class));
                Log.e("登录", "登录成功");
                EMClient.getInstance().chatManager().loadAllConversations();//会话
                EMClient.getInstance().groupManager().loadAllGroups();//群组
            }

            @Override
            public void onError(int i, String s) {
                Log.d("TalkFragment", i + " " + s.toString());

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });

    }

    /**
     * 退出
     */
    private void logout() {
        EMClient.getInstance().logout(false, new EMCallBack() {
            @Override
            public void onSuccess() {
                startActivity(new Intent(getBaseContext(), MainActivity.class));
                finish();
            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }
}

activity_main.xml


<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"
    android:orientation="vertical"
    android:padding="30dp"
    tools:context="com.example.huantest1.MainActivity">


    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="账号" />

    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="密码" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="61dp"
            android:text="注册" />
    LinearLayout>
LinearLayout>

选择聊天 TalkActivity

public class TalkActivity extends AppCompatActivity {


    private EditText etName;
    private Button btnChat;
    private Button btnOutLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_contact);

        initView();
        myOnClick();
    }

    private void initView() {
        etName = (EditText) findViewById(R.id.etName);
        btnChat = (Button) findViewById(R.id.btnChat);
        btnOutLogin = (Button) findViewById(R.id.btnOutLogin);
    }

    public void myOnClick() {
//      发起聊天
        btnChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startChat();
            }

        });
//      退出登录
        btnOutLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                httpOutLogin();
            }
        });
    }


    /**
     * 开始聊天
     */
    private void startChat() {
        String userId = etName.getText().toString().toString();
        Intent intent = new Intent();
        intent.setClass(this, ChatActivity.class);
//      传递注册的名字
        intent.putExtra(EaseConstant.EXTRA_USER_ID, userId);
//      EMMessage.ChatType.Chat单聊,还有群聊和聊天室
        intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EMMessage.ChatType.Chat);
        startActivity(intent);
    }


    /**
     * 退出登录
     */
    private void httpOutLogin() {
        EMClient.getInstance().logout(false, new EMCallBack() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                startActivity(new Intent(getBaseContext(), MainActivity.class));
                finish();

            }

            @Override
            public void onProgress(int progress, String status) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(int code, String message) {
                // TODO Auto-generated method stub
                Logger.t("MainActivity").d("退出登录失败" + code + ">>>" + message);

            }
        });
    }
}

activity_contact.xml


<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"
    android:orientation="vertical"
    >
    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="聊天名称"
        />

    <Button
        android:id="@+id/btnChat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发起聊天" />

    <Button
        android:id="@+id/btnOutLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="退出登录"
        />


LinearLayout>

聊天 ChatActivity

public class ChatActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        initView();
        myOnClick();
    }
    public void initView() {
//      连接easeUi直接封装好的聊天界面
        EaseChatFragment easeChatFragment=new EaseChatFragment();
//      将参数传到聊天界面
        easeChatFragment.setArguments(getIntent().getExtras());
//      加载easeUi封装的聊天界面的fragment
        getSupportFragmentManager().beginTransaction().add(R.id.ec_layout_container,easeChatFragment).commit();

    }
    public void myOnClick() {

    }
}

activity_chat


<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"
    android:orientation="vertical"
    >
    <LinearLayout
        android:id="@+id/ec_layout_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >LinearLayout>

LinearLayout>

你可能感兴趣的:(环信)