//第一步 导jar包 ,并add
//在build.gradle里,android里引用
useLibrary 'org.apache.http.legacy'
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
//第二步 在AndroidManifest里写权限,写环信核心service
//权限
//环信
//第三步 新建MyApplication类继承Application,并在AndroidManifest里通过name调用。
//在MyApplication类里写个onCreate()方法,里面写初始化:
EMOptions options = new EMOptions();
// 默认添加好友时,是不需要验证的,改成需要验证
options.setAcceptInvitationAlways(false);
// 是否自动将消息附件上传到环信服务器,默认为True是使用环信服务器上传下载,如果设为 false,需要开发者自己处理附件消息的上传和下载
options.setAutoTransferMessageAttachments(true);
// 是否自动下载附件类消息的缩略图等,默认为 true 这里和上边这个参数相关联
options.setAutoDownloadThumbnail(true);
//初始化
EMClient.getInstance().init(this, options);
//在做打包混淆时,关闭debug模式,避免消耗不必要的资源
EMClient.getInstance().setDebugMode(true);
//第四步在注册页面,填写账号后,把账号提交到服务器,这里是提交到环信即时通讯你新建应用的后台了。
new Thread(new Runnable() {
@Override
public void run() {
try {
EMClient.getInstance().createAccount(mZhuCeShouJiHaoString, mZhuCeMiMaString);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!Main3Activity.this.isFinishing()) {
Toast.makeText(Main3Activity.this, "注册成功", Toast.LENGTH_SHORT).show();
Intent intent = getIntent();
intent.putExtra("name1", mZhuCeShouJiHaoString);
intent.putExtra("name2", mZhuCeMiMaString);
setResult(20, intent);
finish();
}
}
});
} catch (HyphenateException e) {
e.printStackTrace();
}
}
}).start();
//第五步,登录页面获取到账号,登录上去。
EMClient.getInstance().login(mEd1String,mEd2String,new EMCallBack() {//回调
@Override
public void onSuccess() {
EMClient.getInstance().groupManager().loadAllGroups();
EMClient.getInstance().chatManager().loadAllConversations();
//建立SharedPreferences 库,存放注册好的账号密码,密码其实没用,只判断账号就能进去。
SharedPreferences names = getSharedPreferences("names", 0);
SharedPreferences.Editor edit = names.edit();
edit.putString("z",mEd1String);
edit.putString("m",mEd2String);
edit.commit();
Log.d("main", "登录聊天服务器成功!");
startActivity(new Intent(MainActivity.this,HomeActivity.class));
finish();
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String message) {
Log.d("main", "登录聊天服务器失败!");
}
});
//第六步 在首页onCreate()方法里调用:
int pid = android.os.Process.myPid();
String processAppName = getAppName(pid);
// 如果APP启用了远程的service,此application:onCreate会被调用2次
// 为了防止环信SDK被初始化2次,加此判断会保证SDK被初始化1次
// 默认的APP会在以包名为默认的process name下运行,如果查到的process name不是APP的process name就立即返回
if (processAppName == null ||!processAppName.equalsIgnoreCase(HomeActivity.this.getPackageName())) {
// 则此application::onCreate 是被service 调用的,直接返回
return;
}
EMClient.getInstance().addConnectionListener(new MyConnectionListener());
//然后在onCreate()方法外写这俩方法。
private String getAppName(int pID) {
String processName = null;
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
try {
if (info.pid == pID) {
processName = info.processName;
return processName;
}
} catch (Exception e) {
// Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}
//实现ConnectionListener接口
private class MyConnectionListener implements EMConnectionListener {
@Override
public void onConnected() {
}
@Override
public void onDisconnected(final int error) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(error == EMError.USER_REMOVED){
// 显示帐号已经被移除
}else if (error == EMError.USER_LOGIN_ANOTHER_DEVICE) {
// 显示帐号在其他设备登录
Calendar calendar = Calendar.getInstance();
//建立一个AlertDialog,实现登录被顶了之后,弹出,退出或重新登录。
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setTitle("下线通知");
builder.setMessage("您的账号在" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + "在另一台手机登录了,如非本人操作,建议修改密码");
//点击重新登录获取到SharedPreferences 库里的账号密码 重新登录
builder.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//通过这个SharedPreferences 库得到账号密码,实现重新登录的功能
SharedPreferences name = getSharedPreferences("names", 0);
String name1 = name.getString("z", null);
String pass1 = name.getString("m", null);
EMClient.getInstance().login(name1,pass1,new EMCallBack() {//回调
@Override
public void onSuccess() {
EMClient.getInstance().groupManager().loadAllGroups();
EMClient.getInstance().chatManager().loadAllConversations();
Log.d("main", "登录聊天服务器成功!");
startActivity(new Intent(HomeActivity.this,HomeActivity.class));
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String message) {
Log.d("main", "登录聊天服务器失败!");
}
});
}
});
//退出 退到登录页面
builder.setNeutralButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EMClient.getInstance().logout(true);
Intent intent=new Intent(HomeActivity.this,MainActivity.class);
startActivity(intent);
}
});
//不关闭写法
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}