即时聊天APP - MainActivity
Posted Joker
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了即时聊天APP - MainActivity相关的知识,希望对你有一定的参考价值。
主活动包含三个Fragment,分别是会话、联系人和设置,初始布局隐藏所有碎片,然后把应该显示的显示出来:
//隐藏所有Fragment
private void hideAll(){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.hide(first)
.hide(second)
.hide(third)
.commit();
}
//初始化布局
private void init() {
textView = findViewById(R.id.common_actionbar);
textView.setOnClickListener(this);
textView.setText("会话");
}
//初始化Fragment
private void initFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
first = new Conversationlist();
second = new Friends();
third = new My();
ft.add(R.id.fragment,first);
ft.add(R.id.fragment,second);
ft.add(R.id.fragment,third);
hideAll();
//判断当前显示界面
if(Tips.current == 0 || Tips.current == 1){
ft.show(first);
textView.setText("会话");
}else if(Tips.current == 2){
ft.show(second);
textView.setText("联系人");
}else {
ft.show(third);
textView.setText("设置");
}
ft.commit();
}
接下来连接服务器,这里我放置了一个线程,每隔1秒检查检查一次是否连接成功:
//连接服务器
private void connect() {
//检查网络连接
if(!NetWork.isNetConnection(this)){
Toast.makeText(this,"无网络连接!",Toast.LENGTH_SHORT).show();
isConnect = false;
}else{
//每秒检查是否连接
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(!isConnected())
isConnect = false;
else
isConnect = true;
}
}, 300,1000);
try {
if (!isConnect) {
if (Tips.MainActivity_fail == 0) {
Toast.makeText(MainActivity.this, "服务器连接失败!正在重试...", Toast.LENGTH_SHORT).show();
Tips.MainActivity_fail = 1;
Tips.MainActivity_success = 0;
}
BmobIM.connect(MyUser.getUni(), new ConnectListener() {
@Override
public void done(String s, BmobException e) {
if (e == null) {
isConnect = true;
retry = 0;
if (Tips.MainActivity_success == 0) {
Toast.makeText(MainActivity.this, "服务器连接成功!", Toast.LENGTH_SHORT).show();
Tips.MainActivity_success = 1;
Tips.MainActivity_fail = 0;
}
}else {
if (retry == 0)
if (Tips.MainActivity_fail == 0) {
Toast.makeText(MainActivity.this, "服务器连接失败!正在重试...", Toast.LENGTH_SHORT).show();
Tips.MainActivity_fail = 1;
Tips.MainActivity_success = 0;
} else
retry++;
connect();
}
}
});
}
}catch (Exception e){}
}
}
这里是判断是否连接的代码:
//判断是否连接
private boolean isConnected(){
boolean IsCon = false;
try {
String mg = BmobIM.getInstance().getCurrentStatus().getMsg();
if(mg.equals("connected"))
IsCon = true;
}catch (Exception e){
IsCon = false;
}
return IsCon;
}
这里是发送自定义广播的代码,使用EventBus来触发事件:
//发送广播
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(cn.edu.sau.joker.MessageEvent event) {
Intent intent = new Intent("cn.edu.sau.action.MESSAGE");
intent.putExtra("msg",event.message);
sendBroadcast(intent);
}
下面是退出代码,上面的部分是用户在点击退出的时候会finish掉此Activity,下面时用户点击两次返回键finish当前活动退出:
//销毁活动
@Override
protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
if (intent != null) {
boolean isExit = intent.getBooleanExtra(TAG_EXIT, false);
if (isExit) {
this.finish();
}
}
}
//连续按两次返回键就退出
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if (firstTime + 2000 > System.currentTimeMillis()) {
super.onBackPressed();
} else {
Toast.makeText(this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
// Toa("再按一次退出程序");
}
firstTime = System.currentTimeMillis();
}
另外,在使用EventBus时要进行注册和注销:
//注册订阅者
EventBus.getDefault().register(this);
// 注销订阅者
EventBus.getDefault().unregister(this);
以上是关于即时聊天APP - MainActivity的主要内容,如果未能解决你的问题,请参考以下文章