Android-IM Instant Messaging: Ring Letter

Posted by CtrlAltDel on Mon, 24 Jun 2019 01:53:34 +0200

Reference resources:

Chat demo based on RIS instant messaging cloud V2 SDK
Android Develops IM Instant Chat Based on Ring Xin SDK (I)
android - Developing Instant Messaging Function and Source Download Using Ring Shin SDK

IM :

Instant Messenger (IM).

Android third-party instant messaging SDK has: Netease Yunxin, Ring Xin and Rong Yun, etc.

Reference resources: Comparison of three IM cloud service products with instant messaging cloud products

Here we use ring letter to do instant messaging demo to achieve two effects:
1. User login.
2. Instant messaging between users

Reference resources: Ring Message-Instant Messaging Framework for Android Development Course

The function of ring letter is similar to that of server. The interaction between A and B users must go through this server.

Framework Integration of EIS

Ring letter instant messaging cloud V2.0

Ring letter instant messaging cloud V3.0

1. Download the sdk Development Kit of CITIC

Ring Shin sdk Download

2. Register to be an EIS developer

Developer registration

3. Create an application program on the platform of EIS to get AppKey

4. Register Users on EIS Platform

5. Create Android studio project and import library files in SDK development kit

5.1 Put the jar file in lib of sdk package into libs of as project and add library (you can see the source code)
5.2 As project src/main, a new directory named jniLibs, and then put the other three folders in the sdk package lib into jniLibs.

6. Adding permissions
7. Configure AppKey
8. Declare the Service and Broadcast Receiver required by SDK

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

    <!-- Required -->
    <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" />

    <application
        android:name=".MyApplication"
        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" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ChatActivity"></activity>

        <!-- Setting up the application of EIS AppKey -->
        <meta-data
            android:name="EASEMOB_APPKEY"
            android:value="1173170609178552#chatdemo" />

        <!-- statement SDK Necessary service SDK Core functions-->
        <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" />

        <!-- statement SDK Necessary receiver -->
        <receiver android:name="com.hyphenate.chat.EMMonitorReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
            <!-- Optional filter -->
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

9. SDK initialization

package com.example.administrator.myapplication;

import android.app.Application;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;

/**
 * Created by Administrator on 2017/6/9.
 */

public class MyApplication extends Application {

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

        //Configure ring letter sdk
        EMOptions emOptions = new EMOptions();
        emOptions.setAutoLogin(false);

        //Initialize ring letter sdk
        EMClient.getInstance().init(this, emOptions);
    }
}

10. Login

Interface:

<?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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.example.administrator.myapplication.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <EditText
        android:id="@+id/et_name_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:hint="enter one user name" />

    <EditText
        android:id="@+id/et_pw_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:hint="Please input a password"
        android:inputType="textPassword" />


    <Button
        android:id="@+id/btn_login_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:paddingLeft="40dp"
        android:paddingRight="40dp"
        android:text="Sign in" />
</LinearLayout>

Code:

package com.example.administrator.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.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.et_name_login)
    EditText etNameLogin;
    @BindView(R.id.et_pw_login)
    EditText etPwLogin;
    @BindView(R.id.btn_login_login)
    Button btnLoginLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        btnLoginLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Execute login function
                EMClient.getInstance().login(etNameLogin.getText().toString(),//logon name
                        etPwLogin.getText().toString(),//Login password
                        new EMCallBack() {//Subthread execution, unable to update UI
                            @Override
                            public void onSuccess() {
                                //Successful login
                                startActivity(new Intent(MainActivity.this, ChatActivity.class));
                                finish();
                            }

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

                            }

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

                            }
                        });


            }
        });


    }
}












11. Send messages and listen for message acquisition

Layout:

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

    <TextView
        android:id="@+id/tv"
        android:text="0000"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/et_name_chat"
        android:background="@color/colorAccent" />

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Send out" />

    <EditText
        android:id="@+id/et_name_chat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/et_content_chat"
        android:layout_toLeftOf="@+id/btn_send"
        android:hint="Please enter the person you want to send" />

    <EditText
        android:id="@+id/et_content_chat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn_send"
        android:layout_toLeftOf="@+id/btn_send"
        android:hint="Please enter what you want to send." />


</RelativeLayout>

Code:

package com.example.administrator.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/9.
 */

public class ChatActivity extends AppCompatActivity {

    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.btn_send)
    Button btnSend;
    @BindView(R.id.et_content_chat)
    EditText etContentChat;
    @BindView(R.id.et_name_chat)
    EditText etNameChat;
    private MyEMMessageListener ml;

    private Handler handler = new Handler();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_chat);
        ButterKnife.bind(this);

        //send message
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create message object: parameter: message content, recipient
                EMMessage txtSendMessage = EMMessage.createTxtSendMessage(etContentChat.getText().toString(),
                        etNameChat.getText().toString());
                //send message
                EMClient.getInstance().chatManager().sendMessage(txtSendMessage);
            }
        });

        ml = new MyEMMessageListener();
        EMClient.getInstance().chatManager().addMessageListener(ml);
    }

    /*
    * Message monitoring
    */
    class MyEMMessageListener implements EMMessageListener {

        @Override
        public void onMessageReceived(List<EMMessage> list) {//Execution in this method subthread

            //Parsing messages
            for (EMMessage node : list) {
                final String from = node.getFrom();//sender
                final EMMessage.Type type = node.getType();//Message type

                switch (type) {
                    case TXT://Text content
                        EMTextMessageBody body = (EMTextMessageBody) node.getBody();
                        final String message = body.getMessage();

                        //Deliver the changes to the main thread to update the UI
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                tv.append(from + "Say to me:" + message);
                            }
                        });

                        break;
                    default:
                        break;
                }
            }
        }

        @Override
        public void onCmdMessageReceived(List<EMMessage> list) {

        }

        @Override
        public void onMessageRead(List<EMMessage> list) {

        }

        @Override
        public void onMessageDelivered(List<EMMessage> list) {

        }

        @Override
        public void onMessageChanged(EMMessage emMessage, Object o) {

        }
    }

    //Cancel listening to avoid memory leaks
    @Override
    protected void onDestroy() {
        super.onDestroy();

        EMClient.getInstance().chatManager().removeMessageListener(ml);
    }
}

Topics: Android SDK ButterKnife xml