register
First go to Rongyun official website to register a development account address here
download
Then download the Rongyun development SDK address here
The Demo source code is an example demo provided by the official, without modification, you can successfully view the related functions of Rongyun by running it directly
SDk is divided into two parts: Rongyun IM interface component (imkitht) and Rongyun IM communication capability library (imlib)
Create an app
Go to the Rongyun developer platform to create an application, the points that need to be paid attention to on the Android side are:
- Application ID-Package Name here is the package name of the Android project, for example, the package name of the following code is io.rong.imkit.demo
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.rong.imkit.demo">
- AppKey-App Key here is the value corresponding to the App Key that needs to be declared in the manifest file. The development environment (test environment) is used in the development phase. After the development is completed, it should be switched to the production environment (online environment). A simple example is as follows
<meta-data android:name="RONG_CLOUD_APP_KEY" android:value="e7x8xycsx6flq" />
Project configuration
Add a declaration for APP_KEY
If you use imkit for development, modify all its android:host values to the package name of the development application
If you use Android Studio to develop, remember to add a reference to the so file
Write code
- Initialization, it is recommended to put it in the onCreate method of Application
RongIM.init(this);
To get the Token, the support of the developer's server is required here. If it is only for testing and development, you can go to the Rongyun Developer Platform and select API Debugging-User Services-Get Token for debugging and development
Connect to the Rongyun device, after getting the Token, you can connect to the Rongyun server
// 此处直接 hardcode 给 token 赋值,请替换为您自己的 Token。
String token = "mKmyKqTSf7aNDinwAFMnz7NXKILeV3X0+CCRBOxmtOApmvQjMathViWrePIfq0GuTu9jEL123ckv4AhfjCAKgQ==";
// 连接融云服务器。
RongIM.connect(token, new RongIMClient.ConnectCallback() {
@Override
public void onSuccess(String s) {
// 此处处理连接成功。
Log.d("Connect:", "Login successfully.");
}
@Override
public void onError(ErrorCode errorCode) {
// 此处处理连接错误。
Log.d("Connect:", "Login failed.");
}
});
- When the server is successfully connected, various chat interfaces of Rongyun can be called.