1.倒jar包
2.倒jar包后记得加so库
sourceSets {
main {
//jni库的调用会到资源文件夹下libs里面找so文件
jniLibs.srcDirs = ['libs']
}
}
3.清单文件
4.初始化sdk(记得在清单文件中调用)
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
UMConfigure.init(this, "5af967748f4a9d01f50000c2"
, "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
//更多了解初始化接口可以参照文档接下来需要设置各个平台的appkey:
PlatformConfig.setSinaWeibo("3678636621", "483dfe2d5a648656b462f5e72e72bdea", "http://sns.whalecloud.com");
PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
EMOptions options = new EMOptions();
// 默认添加好友时,是不需要验证的,改成需要验证
options.setAcceptInvitationAlways(false);
//初始化
EMClient.getInstance().init(this, options);
//在做打包混淆时,关闭debug模式,避免消耗不必要的资源
EMClient.getInstance().setDebugMode(true);
}
}
5.loginactivity登陆activity
package com.example.sj.laddernetwork;
import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class LoginActivity extends AppCompatActivity {
boolean isExit;
@BindView(R.id.et_login_user)
EditText etLoginUser;
@BindView(R.id.et_login_password)
EditText etLoginPassword;
@BindView(R.id.tv_register)
TextView tvRegister;
@BindView(R.id.iv_qq)
ImageView ivQq;
@BindView(R.id.iv_wx)
ImageView ivWx;
@BindView(R.id.iv_wb)
ImageView ivWb;
private TextView tv_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
//判断是否登录
if(EMClient.getInstance().isLoggedInBefore()){
startActivity(new Intent(LoginActivity.this,ShouActivity.class));
finish();
}else{
}
}
}).start();
initView();
ButterKnife.bind(this);
if (Build.VERSION.SDK_INT >= 23) {
String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(this, mPermissionList, 123);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (!isExit) {
isExit = true;
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
System.exit(0);
}
return false;
} else {
return super.onKeyDown(keyCode, event);
}
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
isExit = false;
}
};
@OnClick({R.id.et_login_user, R.id.et_login_password, R.id.tv_register, R.id.iv_qq, R.id.iv_wx, R.id.iv_wb})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.et_login_user:
break;
case R.id.et_login_password:
break;
case R.id.tv_register:
startActivity(new Intent(this, RegistActivity.class));
break;
case R.id.iv_qq:
UMShareAPI.get(LoginActivity.this).getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map map) {
String uid = map.get("uid");
String name = map.get("name");
String gender = map.get("gender");
String iconurl = map.get("iconurl");
Log.e("Ta", uid + "---" + name + "---");
startActivity(new Intent(LoginActivity.this, ShouActivity.class));
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
}
});
break;
case R.id.iv_wx:
break;
case R.id.iv_wb:
break;
}
}
private void initView() {
tv_login = (TextView) findViewById(R.id.tv_login);
tv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String user = etLoginUser.getText().toString();
final String password = etLoginPassword.getText().toString();
EMClient.getInstance().login(user,password,new EMCallBack() {//回调
@Override
public void onSuccess() {
EMClient.getInstance().groupManager().loadAllGroups();
EMClient.getInstance().chatManager().loadAllConversations();
Log.d("main", "登录聊天服务器成功!");
startActivity(new Intent(LoginActivity.this, ShouActivity.class));
finish();
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String message) {
Log.d("main", "登录聊天服务器失败!");
}
});
}
});
}
@Override
protected void onResume() {
super.onResume();
etLoginUser.setText(RegistActivity.number);
etLoginPassword.setText(SetMiMaActivity.mima);
}
}
6.注册连接状态监听activity 也就是首页activity
package com.example.sj.laddernetwork;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.util.NetUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ShouActivity extends AppCompatActivity {
@BindView(R.id.shou_fragment_conton)
RelativeLayout shouFragmentConton;
@BindView(R.id.shou_one)
android.widget.TextView shouOne;
@BindView(R.id.shou_two)
android.widget.TextView shouTwo;
@BindView(R.id.shou_three)
android.widget.TextView shouThree;
private FragmentManager fragmentManager;
private FragmentTransaction transaction;
private String user1;
private String psw;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Toast.makeText(ShouActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shou);
ButterKnife.bind(this);
Intent intent = getIntent();
user1 = intent.getStringExtra("user");
psw = intent.getStringExtra("psw");
//注册一个监听连接状态的listener
EMClient.getInstance().addConnectionListener(new MyConnectionListener());
}
//实现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) {
// 显示帐号在其他设备登录
Edit();
} else {
if (NetUtils.hasNetwork(ShouActivity.this)){
//连接不到聊天服务器
} else{
//当前网络不可用,请检查网络设置
}
}
}
});
}
}
private void Edit(){
Toast.makeText(ShouActivity.this, "账号已登录", Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(ShouActivity.this)
.setTitle("账号已登录")
.setMessage("是否退出登录")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EMClient.getInstance().logout(false, new EMCallBack() {
@Override
public void onSuccess() {
Log.i("lzan13", "logout success");
// 调用退出成功,结束app
startActivity(new Intent(ShouActivity.this,LoginActivity.class));
finish();
}
@Override
public void onError(int i, String s) {
Log.i("lzan13", "logout error " + i + " - " + s);
}
@Override
public void onProgress(int i, String s) {
}
});
}
})
.setNegativeButton("重新登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EMClient.getInstance().login(user1,psw,new EMCallBack(){
@Override
public void onSuccess() {
Message message = new Message();
handler.sendMessage(message);
}
@Override
public void onError(int i, String s) {
}
@Override
public void onProgress(int i, String s) {
}
});
}
}).show();
}
}