步骤:
一,从环信官网注册获得appkey(没有key 无法接入环信服务器)
二, 添加依赖
build.gradle(project)
allprojects { repositories { google() jcenter() maven { url "https://raw.githubusercontent.com/HyphenateInc/Hyphenate-SDK-Android/master/repository" }//环信sdk } }
build.gradle.gradle(App)
api 'com.google.android.gms:play-services-gcm:15.0.1'//环信sdk api 'com.hyphenate:hyphenate-sdk:3.3.0'//环信sdk
三,AndroidMainifest文件
权限
android:name="android.permission.VIBRATE" /> android:name="android.permission.INTERNET" /> android:name="android.permission.RECORD_AUDIO" /> android:name="android.permission.CAMERA" /> android:name="android.permission.ACCESS_NETWORK_STATE" /> android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> android:name="android.permission.ACCESS_FINE_LOCATION" /> android:name="android.permission.GET_TASKS" /> android:name="android.permission.ACCESS_WIFI_STATE" /> android:name="android.permission.CHANGE_WIFI_STATE" /> android:name="android.permission.WAKE_LOCK" /> android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> android:name="android.permission.READ_PHONE_STATE" /> android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
android:name="你的项目application"//在里面初始化环信sdk >
android:name="EASEMOB_APPKEY" android:value="申请的appkey" />
android:name="com.hyphenate.chat.EMChatService"
android:exported="true" />
android:name="com.hyphenate.chat.EMJobService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE" />
android:name="com.hyphenate.chat.EMMonitorReceiver">
android:name="android.intent.action.PACKAGE_REMOVED" />
android:scheme="package" />
android:name="android.intent.action.BOOT_COMPLETED" />
android:name="android.intent.action.USER_PRESENT" />
四,application中初始化(application是程序的入口):定义一个类继承application 并将
android:name="你的项目application"//在里面初始化环信sdk
@Override public void onCreate() { super.onCreate(); mContext=this; //LitePal.initialize(this);//初始化litepal //Executor.getInstance().init(this); EMOptions options = new EMOptions(); options.setAcceptInvitationAlways(false);// 默认添加好友时,是不需要验证的,改成需要验证 options.setAutoAcceptGroupInvitation(false);//是否自动加入群聊 options.setAcceptInvitationAlways(false);//是否自动加入单聊 EMClient.getInstance().init(this, options);//初始化 // Model.getInstance().init(this);//全局监听,放在环信服务器初始化后面不然报空指针问题 }
这样环信sdk就集成完毕了
五,在application中可以做很多事,比如获得全局上下文(context),通过key获得任何一个activity,等等
private static Map,Activity> activitys=new HashMap<>();
public static void addDestoryActivity(Activity activity, String string){ activitys.put(string,activity); }
public static Activity getActivity(String string){ Setkry2= activitys.keySet(); Activity activity=new Activity(); for (String ket2:kry2){ activitys.get(ket2); activity=activitys.get(ket2); } return activity; }