|
地板
楼主 |
发表于 2015-9-30 16:49:42
|
只看该作者
package com.example.videomeeting;
import com.bairuitech.anychat.AnyChatBaseEvent;
import com.bairuitech.anychat.AnyChatCoreSDK;
import com.bairuitech.anychat.AnyChatObjectEvent;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity implements AnyChatBaseEvent,
AnyChatObjectEvent {
private String ip_address, user_name;
private int ip_port, room_number;
private Button but_login, but_more;
private TextView loginmsg;
private EditText ipaddress, username, ipport, roomnumber;
private ProgressBar progress;
AnyChatCoreSDK anychat = null;
Context context;
private boolean login_true = true;
int[] asd = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
context = MainActivity.this;
anychat = new AnyChatCoreSDK();
// 初始化SDK第一个参数传入的是当前系统android版本号!
anychat.InitSDK(android.os.Build.VERSION.SDK_INT, 0);
anychat.SetBaseEvent(this);
anychat.SetObjectEvent(this);
getview();
getuserinfo();
listener();
}
private void getuserinfo() {
try {
ip_address = ipaddress.getText().toString().trim();
user_name = username.getText().toString().trim();
room_number = Integer.parseInt(roomnumber.getText().toString()
.trim());
ip_port = Integer.parseInt(ipport.getText().toString().trim());
} catch (Exception e) {
e.printStackTrace();
}
}
private void listener() {
but_more.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(1);
}
});
but_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (login_true) {
handler.sendEmptyMessage(2);
progress.setVisibility(0);
} else {
Log.e("", "退出登录!");
anychat.LeaveRoom(-1);// 离开房间“-1”表示当前房间
anychat.Logout();// 退出服务器,下次进入房间需要重新连接服务器
handler.sendEmptyMessage(5);
}
}
});
}
private void getview() {
ipaddress = (EditText) findViewById(R.id.ipaddress);
ipport = (EditText) findViewById(R.id.ipport);
username = (EditText) findViewById(R.id.username);
roomnumber = (EditText) findViewById(R.id.roomnumber);
but_login = (Button) findViewById(R.id.loding);
but_more = (Button) findViewById(R.id.app_title_but_right);
but_more.setText("更多");
but_more.setVisibility(0);
loginmsg = (TextView) findViewById(R.id.mainUIbottomConnMsg);
progress = (ProgressBar) findViewById(R.id.progressBar1);
progress.setVisibility(8);
loginmsg.setTextColor(context.getResources().getColor(
R.color.color_black));
}
public Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1: {// 点击顶部按钮的事件处理
try {
if (but_more.getText().toString() == "更多") {
but_more.setText("关闭");
} else {
but_more.setText("更多");
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
case 2: {// 没有登录的情况下点击登录按钮的处理
try {
but_login.setText("登录中...");
loginmsg.setText("正在尝试连接服务器!");
loginmsg.setTextColor(context.getResources().getColor(
R.color.color_yellow));
getuserinfo();
anychat.Connect(ip_address, ip_port);
// anychat.EnterRoom(room_number, "");
} catch (Exception e) {
e.printStackTrace();
}
break;
}
case 3: {// 连接服务器成功的处理
try {
loginmsg.setText("连接服务器成功!");
loginmsg.setTextColor(context.getResources().getColor(
R.color.color_blue));
but_login.setText("退出登录");
progress.setVisibility(8);
login_true = false;
} catch (Exception e) {
e.printStackTrace();
}
break;
}
case 4: {// 连接服务器失败的处理
try {
loginmsg.setText("连接服务器失败!");
loginmsg.setTextColor(context.getResources().getColor(
R.color.color_red));
but_login.setText("登录");
progress.setVisibility(8);
login_true = true;
} catch (Exception e) {
e.printStackTrace();
}
break;
}
case 5: {// 退出登录后的处理
try {
loginmsg.setText("等待连接服务器!");
but_login.setText("登录");
loginmsg.setTextColor(context.getResources().getColor(
R.color.color_black));
progress.setVisibility(8);
login_true = true;
} catch (Exception e) {
e.printStackTrace();
}
break;
}
case 6: {// 网络出错后的处理
try {
loginmsg.setText("网络出错!");
but_login.setText("登录");
loginmsg.setTextColor(context.getResources().getColor(
R.color.color_red));
progress.setVisibility(8);
login_true = true;
} catch (Exception e) {
e.printStackTrace();
}
break;
}
default:
break;
}
}
};
@Override
public void OnAnyChatObjectEvent(int dwObjectType, int dwObjectId,
int dwEventType, int dwParam1, int dwParam2, int dwParam3,
int dwParam4, String strParam) {
// TODO Auto-generated method stub
}
// 登录服务器时的回调
@Override
public void OnAnyChatConnectMessage(boolean bSuccess) {
Log.e("OnAnyChatConnectMessage", "登录了服务器!" + bSuccess);
if (bSuccess) {
handler.sendEmptyMessage(3);// 连接服务器成功
} else {
handler.sendEmptyMessage(4);// 连接服务器失败
}
anychat.Login(user_name, "");
}
// 登录成功后的回调:自己的ID和错误代码,如果错误代码为0,则代表正常加入了房间
@Override
public void OnAnyChatLoginMessage(int dwUserId, int dwErrorCode) {
Log.e("OnAnyChatLoginMessage", "登录代码:" + dwUserId + dwErrorCode);
asd = anychat.GetUserFriends();
Log.e("", asd.length+"");
for (int i = 0; i < asd.length; i++) {
Log.e("", "" + asd[i]);
}
}
// 进入房间后的回调接口
@Override
public void OnAnyChatEnterRoomMessage(int dwRoomId, int dwErrorCode) {
Log.e("OnAnyChatEnterRoomMessage", "进入了房间号:" + room_number);
}
// 房间人数和房间号码的回调
@Override
public void OnAnyChatOnlineUserMessage(int dwUserNum, int dwRoomId) {
Log.e("OnAnyChatOnlineUserMessage", "当前有:" + dwUserNum + "人在线!");
Log.e("OnAnyChatOnlineUserMessage", "当前在:" + dwRoomId + "号房间!");
}
// 用户进入/退出房间消息,dwUserId表示用户ID号,bEnter表示该用户是进入(TRUE)或离开(FALSE)房间
@Override
public void OnAnyChatUserAtRoomMessage(int dwUserId, boolean bEnter) {
Log.e("OnAnyChatUserAtRoomMessage", "有用户进入/退出房间:" + dwUserId + bEnter);
}
// 网络中断的回调,该回调只有在成功登录之后才会生效!得到的是错误代码
@Override
public void OnAnyChatLinkCloseMessage(int dwErrorCode) {
// TODO Auto-generated method stub
Log.e("OnAnyChatLinkCloseMessage", "网络出错:" + dwErrorCode);
handler.sendEmptyMessage(6);
}
}
|
|