注册

申请好友后,为什么会自动申请通过

A申请B后,a马上得到回复申请通过,但是B都没有操作啊
 
package com.example.liaotian;

import java.util.List;

import android.app.Activity;
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 List usernames;
private ListView listview;
private FriendsAdapter fAdapter;
private PopupWindow pop;
private String reason;
private String username;

Handler handle =new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
fAdapter.notifyDataSetChanged();
break;
case 2://发送好友请求
Toast.makeText(getApplication(), "已发送请求", 1).show();
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;
}
};
};
private EditText find_friends_name;
private LinearLayout parent;

@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", "登陆聊天服务器失败!");
}
});

parent = (LinearLayout) findViewById(R.id.parent);

listview = (ListView) findViewById(R.id.listview);
fAdapter = new FriendsAdapter();
listview.setAdapter(fAdapter);


registerForContextMenu(listview);





((android.widget.TextView) findViewById(R.id.find_friends)).setOnClickListener(new View.OnClickListener() {





@Override
public void onClick(View v) {
// TODO Auto-generated method stub
find_friends_name.clearFocus();
View view = View.inflate(getApplication(), R.layout.add_friends, null);

pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pop.setFocusable(true);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);
pop.showAtLocation(parent, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0 , 0);
reason = ((EditText) view.findViewById(R.id.add_content)).getText().toString();
username = find_friends_name.getText().toString();
((TextView)view.findViewById(R.id.user_name)).setText(username);

view.findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pop.dismiss();

new Thread(){
public void run() {

try {
EMContactManager.getInstance().addContact(username, reason);
handle.sendEmptyMessage(2);
} catch (EaseMobException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

};
}.start();

}
});


}
});

find_friends_name = (android.widget.EditText) findViewById(R.id.find_friends_name);

}


private void init() {
// TODO Auto-generated method stub

EMChatManager.getInstance().addConnectionListener(new MyConnectionListener());

//获取好友列表
EMChatManager.getInstance().getChatOptions().setUseRoster(true);
new Thread(){
public void run() {
try {
usernames = EMContactManager.getInstance().getContactUserNames();
handle.sendEmptyMessage(1);
} catch (EaseMobException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

};
}.start();

EMContactManager.getInstance().setContactListener(new EMContactListener() {

@Override
public void onContactAgreed(String username) {
Log.i("test", "好友请求被同意...");
//好友请求被同意
Message msg=Message.obtain();
msg.what=3;
msg.obj=username;
handle.sendMessage(msg);
}

@Override
public void onContactRefused(String username) {
//好友请求被拒绝
Message msg=Message.obtain();
msg.what=6;
msg.obj=username;
handle.sendMessage(msg);
}

@Override
public void onContactInvited(String username, String reason) {
Log.i("test", "申请添加好友...");
//收到好友邀请
Message msg=Message.obtain();
msg.what=5;
msg.obj=username;
handle.sendMessage(msg);
}

@Override
public void onContactDeleted(List usernameList) {
Log.i("test", "被删除..."+usernameList);
//被删除时回调此方法
for(String username:usernameList){
usernames.remove(username);
}

handle.sendEmptyMessage(4);
}


@Override
public void onContactAdded(List usernameList) {
//增加了联系人时回调此方法
for(String username:usernameList){
if(!usernames.contains(username)){
usernames.add(username);
}
}
handle.sendEmptyMessage(1);
}
});

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();//此方法为同步方法
}

}

 
 
package com.example.liaotian;

import java.util.Iterator;
import java.util.List;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.easemob.chat.EMChat;

public class MyApplication extends Application {
public static Context applicationContext;

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
applicationContext=this;
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("com.example.liaotian")) {
//"com.easemob.chatuidemo"为demo的包名,换到自己项目中要改成自己包名

// 则此application::onCreate 是被service 调用的,直接返回
return;
}


Log.i("test", "Application");

EMChat.getInstance().init(applicationContext);

/**
* debugMode == true 时为打开,sdk 会在log里输入调试信息
* @param debugMode
* 在做代码混淆的时候需要设置成false
*/
EMChat.getInstance().setDebugMode(true);//在做打包混淆时,要关闭debug模式,如果未被关闭,则会出现程序无法运行问题
}



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) {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
// Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
// info.processName +" Label: "+c.toString());
// processName = c.toString();
processName = info.processName;
return processName;
}
} catch (Exception e) {
// Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}
}
已邀请:

[已注销]

赞同来自: fat1

上面是uidemo中的代码么 你在初始化的时候有没有定义 :
 protected void initHXOptions(){
        Log.d(TAG, "init HuanXin Options");
       
        // 获取到EMChatOptions对象
        EMChatOptions options = EMChatManager.getInstance().getChatOptions();
        // 默认添加好友时,是不需要验证的,改成需要验证
        options.setAcceptInvitationAlways(hxModel.getAcceptInvitationAlways());
        // 默认环信是不维护好友关系列表的,如果app依赖环信的好友关系,把这个属性设置为true
        options.setUseRoster(hxModel.getUseHXRoster());
        // 设置是否需要已读回执
        options.setRequireAck(hxModel.getRequireReadAck());
        // 设置是否需要已送达回执
        options.setRequireDeliveryAck(hxModel.getRequireDeliveryAck());
        // 设置从db初始化加载时, 每个conversation需要加载msg的个数
        options.setNumberOfMessagesLoaded(1);
       
        notifier = createNotifier();
        notifier.init(appContext);
       
        notifier.setNotificationInfoProvider(getNotificationListener());
    }
 
参考demo的HXSDKHelper类写一下参数配置信息。
options.setAcceptInvitationAlways(hxModel.getAcceptInvitationAlways()); 这个设置为false,即为需要好友验证。

要回复问题请先登录注册