1. 程式人生 > >整合環信sdk步驟(即時通訊)

整合環信sdk步驟(即時通訊)

步驟:

一,從環信官網註冊獲得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檔案

許可權

<!-- 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.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" />
<applivation  android:name="你的專案application"//在裡面初始化環信sdk  >
<meta-data
android:name="EASEMOB_APPKEY" android:value="申請的appkey" />
<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" />
<!-- 宣告SDK所需的receiver -->
<receiver android:name="com.hyphenate.chat.EMMonitorReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
    </intent-filter>
<!-- 可選filter -->
<intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.USER_PRESENT" />
    </intent-filter>
</receiver>
</application>

四,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<String,Activity> activitys=new HashMap<>();
public static void addDestoryActivity(Activity activity, String string){
   activitys.put(string,activity);
}
public  static  Activity getActivity(String string){
        Set<String> kry2=activitys.keySet();
Activity activity=new Activity();
        for (String ket2:kry2){
              activitys.get(ket2);
activity=activitys.get(ket2);
}
        return activity;
}