一、首先,进入融云官网www.rongcloud.cn,注册一个账号,这个不用说,你们就懂的。
二、进入控制台,新建一个项目,这个不详细讲,可以看一下他们的api,重点是代码部分。
这样就可以查看APPKey 何APP Secret 其它暂时可以不用看,我们本次的目标只是实现项目的搭建,及获取token,连聊天的界面都不会有的。
下载融云的IMKit 包含聊天UI和聊天核心控件
接下来,主要配置好AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chat.simplechat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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_COARSE_LOCATION" />
<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.CAMERA" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:name=".SCApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light" >
<activity
android:name="com.chat.simplechat.activity.MainActivity"
android:label="@string/app_name" >
activity>
<activity
android:name=".activity.SplashActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Start" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<service
android:name="io.rong.push.core.PushRegistrationService"
android:exported="false" >
service>
<service
android:name="io.rong.push.core.MessageHandleService"
android:exported="true" >
service>
<service
android:name="io.rong.push.PushService"
android:exported="false"
android:process="io.rong.push" >
service>
<receiver
android:name="io.rong.push.PushReceiver"
android:process="io.rong.push" >
<intent-filter>
<action android:name="io.rong.push.intent.action.HEART_BEAT" />
intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
intent-filter>
receiver>
<meta-data
android:name="RONG_CLOUD_APP_KEY"
android:value="pkfcgjstfp8j8" />
<service
android:name="io.rong.imlib.ipc.RongService"
android:exported="true"
android:process=":ipc" />
<service android:name="io.rong.imlib.ReConnectService" />
<receiver android:name="io.rong.imlib.ConnectChangeReceiver" />
<receiver
android:name="io.rong.imlib.HeartbeatReceiver"
android:process=":ipc" />
application>
manifest>
然后自己继承一个Application ,在里面实现初始化
package com.chat.simplechat;
import org.xutils.x;
import io.rong.imkit.RongIM;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.widget.Toast;
public class SCApplication extends Application {
private static Context content;
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
content = getApplicationContext();
/**
* OnCreate 会被多个进程重入,这段保护代码,确保只有您需要使用 RongIM 的进程和 Push 进程执行了 init。
* io.rong.push 为融云 push 进程名称,不可修改。
*/
if (getApplicationInfo().packageName
.equals(getCurProcessName(getApplicationContext()))
|| "io.rong.push"
.equals(getCurProcessName(getApplicationContext()))) {
/**
* IMKit SDK调用第一步 初始化
*/
RongIM.init(this);
}
}
public static Context getObjectContext() {
return content;
}
/**
* 获得当前进程的名字
*
* @param context
* @return 进程号
*/
public static String getCurProcessName(Context context) {
int pid = android.os.Process.myPid();
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
.getRunningAppProcesses()) {
if (appProcess.pid == pid) {
return appProcess.processName;
}
}
return null;
}
}
然后记得把继承的APPlication的名字配置到AndroidManifest.xml。
到目前,我们已经把项目搭建好了,那么还有很重要的就是获取Token和连接服务器,只把核心代码贴出来,会把源码放在最后,可以下载查看详细代码。
public static void getToken(final String id, final String username) {
RequestParams params = new RequestParams(
"https://api.cn.ronghub.com/user/getToken.json");
addHeader(params);
params.addBodyParameter("userId", id);
params.addBodyParameter("name", username);
x.http().post(params, new CommonCallback() {
@Override
public void onCancelled(CancelledException arg0) {
}
@Override
public void onError(Throwable arg0, boolean arg1) {
EBmessage eb = new EBmessage();
eb.setStatus(false);
eb.setMessage(arg0.toString());
eb.setFrom("getToken");
EventBus.getDefault().post(eb);
}
@Override
public void onFinished() {
}
@Override
public void onSuccess(String s) {
TokenMod mod = new Gson().fromJson(s, TokenMod.class);
Editor editor = getEditor();
editor.putString("username", username);
editor.putString("userpass", mod.getUserId());
editor.putString("token", mod.getToken());
editor.commit();
EBmessage eb = new EBmessage();
eb.setStatus(true);
eb.setMessage(mod.getToken());
eb.setFrom("getToken");
EventBus.getDefault().post(eb);
}
});
}
/**
* 建立与融云服务器的连接
*
* @param token
*/
public static void connect(String token, Context context) {
if (context.getApplicationInfo().packageName.equals(SCApplication
.getCurProcessName(context.getApplicationContext()))) {
/**
* IMKit SDK调用第二步,建立与服务器的连接
*/
RongIM.connect(token, new RongIMClient.ConnectCallback() {
/**
* Token 错误,在线上环境下主要是因为 Token 已经过期,您需要向 App Server 重新请求一个新的
* Token
*/
@Override
public void onTokenIncorrect() {
Log.d("LoginActivity", "--onTokenIncorrect");
}
/**
* 连接融云成功
*
* @param userid
* 当前 token
*/
@Override
public void onSuccess(String userid) {
EBmessage eb = new EBmessage();
eb.setStatus(true);
eb.setMessage("success");
eb.setFrom("connect");
EventBus.getDefault().post(eb);
Log.d("LoginActivity", "--onSuccess" + userid);
}
/**
* 连接融云失败
*
* @param errorCode
* 错误码,可到官网 查看错误码对应的注释
*/
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
Log.d("LoginActivity", "--onError" + errorCode);
}
});
}
}
demo下载地址http://download.csdn.net/detail/wangyang00700/9623568