Effect:
Before development, we need to go to the official website to register: rongyun official website
Simply enter the name and description of the application to create it.
To make the integration easier, we downloaded the SDK from the official website and chose the IMKit package because it contains the components needed for chat.
After successful download in our project, in the form of Module to introduce IMKit, and rely on.
Now that the environment is set up, let’s start our coding:
public class App extends Application {
@Override
public void onCreate() { super.onCreate(); RongIM.init(this); // Initialize, then remember to configure this class in the manifest file. }}Copy the code
Connect to the server in MainActivity.
Due to the testing environment, we directly go to the official website of Rongyun to obtain the token. There is an API debugging function on the left side of my application in the Personal center of Rongyun. Enter an ID for the test randomly and click Submit.
Then use this token to call a method to connect to the server:
private void connect(String token) {
RongIM.connect(token, new RongIMClient.ConnectCallback() {
@Override
public void onTokenIncorrect() {
Log.e("LoginActivity"."--onTokenIncorrect");
}
@Override
public void onSuccess(String userid) {
Log.e("LoginActivity"."--onSuccess--" + userid);
Toast.makeText(MainActivity.this, "Login successful, user:"+ userid, Toast.LENGTH_SHORT).show(); / / the server connection is successful, jump the message list startActivity (new Intent (MainActivity. This, ConversationListActivity. Class)); } @Override public void onError(RongIMClient.ErrorCode errorCode) { Log.e("LoginActivity"."--onError"); }}); }Copy the code
public class ConversationListActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the viewsetContentView(R.layout.subconversationlist);
ConversationListFragment listFragment = (ConversationListFragment) ConversationListFragment.instantiate(this, ConversationListFragment.class.getName());
Uri uri = Uri.parse("rong://" + getApplicationInfo().packageName).buildUpon()
.appendPath("conversationlist")
.appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false")
.appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")
.appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")
.appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")
.appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false") .build(); listFragment.setUri(uri); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Add the Fragment interface to our page. transaction.add(R.id.subconversationlist, listFragment); transaction.commitAllowingStateLoss(); The second parameter represents the user ID of the other party, and the third parameter represents the title of the chat window. In order to facilitate the test of the chat, we need two mobile phones to test, so the user who logs in the first token and the second user"chao"Set up a chat and change it before running the second phone"chao"Token login, then chat here to change the ID of the first one"text". RongIM.getInstance().startPrivateChat(this,"chao"."Chatting"); }}Copy the code
The subconversationList. XML layout contains only one FrameLayout:
<FrameLayout
android:id="@+id/subconversationlist"
android:name="io.rong.imkit.fragment.SubConversationListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Copy the code
At this point, the chat logic is basically processed, and then we can proceed to the final configuration, configure the chat interface:
public class ConversationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conversation);
String sName = getIntent().getData().getQueryParameter("title"); // Get the nicknamesetTitle("And" + sName + "Chatting"); }}Copy the code
Add fragment to chat layout, note that name is fixed:
<fragment
android:id="@+id/conversation"
android:name="io.rong.imkit.fragment.ConversationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Copy the code
RONG_CLOUD_APP_KEY= your own key, Android :host=”chao.example.instantchat” and host content must be changed to your project package name.
<? xml version="1.0" encoding="utf-8"? > <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="chao.example.instantchat">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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=".activity.ConversationListActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="chao.example.instantchat"
android:path="/conversationlist"
android:scheme="rong" />
</intent-filter>
</activity>
<activity
android:name=".ConversationActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="chao.example.instantchat"
android:pathPrefix="/conversation/"
android:scheme="rong" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="chao.example.instantchat.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/rc_file_path" />
</provider>
<meta-data
android:name="RONG_CLOUD_APP_KEY"
android:value="tdrvipkstfwj5" />
<service
android:name="io.rong.imlib.ipc.RongService"
android:exported="true"
android:process=":ipc" />
<service
android:name="io.rong.imlib.ReConnectService"
android:exported="true" />
<receiver
android:name="io.rong.imlib.ConnectChangeReceiver"
android:exported="true" />
<receiver
android:name="io.rong.imlib.HeartbeatReceiver"
android:process=":ipc"/ > <! -- imlib config end --> </application> </manifest>Copy the code
Then run the project. The two apps are packaged and installed on the mobile phone with two tokens respectively, and run as shown in the figure below: