为什么对方删除我后,不会回调我的删除方法
对方同意申请好友可以收到监听,删除监听却收不到
package com.example.liaotian;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.easemob.EMCallBack;
import com.easemob.EMConnectionListener;
import com.easemob.EMError;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactListener;
import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.NetUtils;
public class MainActivity extends Activity {
private Listusernames;
private ListView listview;
private FriendsAdapter fAdapter;
Handler handle =new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
fAdapter.notifyDataSetChanged();
break;
case 3://同意添加好友
String username3 =(String)msg.obj;
Toast.makeText(getApplication(), username3+"已经同意添加你为好友", 1).show();
break;
case 4://被删除好友
Toast.makeText(getApplication(), "被删除啦", 1).show();
fAdapter.notifyDataSetChanged();
break;
case 5:
String username5 =(String)msg.obj;
Toast.makeText(getApplication(), username5+"申请添加好友", 1).show();
break;
case 6:
String username6 =(String)msg.obj;
Toast.makeText(getApplication(), username6+"拒绝添加你为好友", 1).show();
break ;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EMChatManager.getInstance().login("123","123",new EMCallBack() {//回调
@Override
public void onSuccess() {
runOnUiThread(new Runnable() {
public void run() {
EMGroupManager.getInstance().loadAllGroups();
EMChatManager.getInstance().loadAllConversations();
Log.d("main", "登陆聊天服务器成功!");
init();
}
});
}
@Override
public void onProgress(int progress, String status) {
}
@Override
public void onError(int code, String message) {
Log.d("main", "登陆聊天服务器失败!");
}
});
listview = (ListView) findViewById(R.id.listview);
fAdapter = new FriendsAdapter();
listview.setAdapter(fAdapter);
registerForContextMenu(listview);
findViewById(R.id.find_friend).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplication(), FindFriends.class));
}
});
}
private void init() {
// TODO Auto-generated method stub
EMChatManager.getInstance().addConnectionListener(new MyConnectionListener());
//获取好友列表
new Thread(){
public void run() {
try {
usernames = EMContactManager.getInstance().getContactUserNames();
handle.sendEmptyMessage(1);
} catch (EaseMobException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
setLister();
}
public void setLister(){
EMContactManager.getInstance().setContactListener(new EMContactListener() {
@Override
public void onContactAgreed(String username) {
Log.i("test", " 好友请求被同意...");
//好友请求被同意
}
@Override
public void onContactRefused(String username) {
//好友请求被拒绝
Log.i("test", " 好友请求被拒绝...");
}
@Override
public void onContactInvited(String username, String reason) {
Log.i("test", " 申请添加好友...");
//收到好友邀请
}
@Override
public void onContactDeleted(ListusernameList) {
Log.i("test", " 被删除..."+usernameList);
//被删除时回调此方法
}
@Override
public void onContactAdded(ListusernameList) {
//增加了联系人时回调此方法
Log.i("test", " 增加了联系人..."+usernameList);
}
});
EMChat.getInstance().setAppInited();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// if(((AdapterContextMenuInfo)menuInfo).position > 0){ m,
getMenuInflater().inflate(R.menu.delete_message, menu);
// }
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == R.id.delete_message) {
int i =((AdapterContextMenuInfo) item.getMenuInfo()).position;
Log.i("test", "删除信息:"+i);
return true;
}else if(item.getItemId() == R.id.delete_friends){
final int i =((AdapterContextMenuInfo) item.getMenuInfo()).position;
new Thread(){
public void run() {
try {
EMContactManager.getInstance().deleteContact(usernames.get(i));
usernames.remove(i);
handle.sendEmptyMessage(1);
} catch (EaseMobException e) {
// TODO Auto-generated catch block
Log.i("test", "发生异常");
e.printStackTrace();
}
};
}.start();
return true;
}
return super.onContextItemSelected(item);
}
class FriendsAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return (usernames!=null && usernames.size()>0)?usernames.size():0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view =View.inflate(getApplication(), R.layout.my_friends_item, null);
TextView userName = (TextView) view.findViewById(R.id.user_name);
userName.setText(usernames.get(position));
return view;
}
}
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.CONNECTION_CONFLICT) {
// 显示帐号在其他设备登陆
} else {
if (NetUtils.hasNetwork(MainActivity.this)){
//连接不到聊天服务器
}else{
//当前网络不可用,请检查网络设置
}
}
}
});
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
EMChatManager.getInstance().logout();//此方法为同步方法
}
}
没有找到相关结果
已邀请:
1 个回复
环信技术支持中心