我遇到聊天应用程序的问题,我正在尝试使用处理程序运行聊天接收器功能,这样一旦收到消息,它们就会被处理并显示在屏幕上。但是当我尝试返回并恢复聊天时失败,因为Handler继续运行,因此与之关联的消息对象,并且无法重新初始化它。以下是代码:
public class hotListener extends ListActivity {
private HotspotService service;
private XMPPConnection connection;
private IBinder binder;
private Handler mHandler = new Handler();
private ArrayList<String> messages = new ArrayList<String>();
ArrayList<ChatMessage> messagex= new ArrayList<ChatMessage>();
ChattingAdapter adaptex= new ChattingAdapter(hotListener.this, messagex);;
HotspotService.MyBinder binderx;
Intent mIntent ;
private ListView listview;
EditText sender_message ;
String msg;
Thread t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
setListAdapter(adaptex);
System.out.println("inside on create");
Button send_button = (Button) findViewById(R.id.chat_send_message);
sender_message = (EditText) findViewById(R.id.chat_input);
send_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
msg = sender_message.getText().toString();
sender_message.setText("");
if(!(msg.length()==0)){
messagex.add(new ChatMessage(msg, true));
//addNewMessage(new ChatMessage(msg, true));
adaptex.notifyDataSetChanged();
getListView().setSelection(messagex.size()-1);
}
}
});
if(!isMyServiceRunning()){
System.out.println("seems like service not running");
startService(new Intent(this,HotspotService.class));
System.out.print(" now started ");
}
}
@Override
protected void onStart(){
super.onStart();
System.out.println("in onstart");
}
private void receivespots(XMPPConnection connection2, final ChattingAdapter adaptex2) {
connection2.getChatManager().addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat arg0, boolean arg1) {
arg0.addMessageListener(new MessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
//final String from = message.getFrom();
final String body = message.getBody();
mHandler.post(new Runnable() {
@Override
public void run() {
messagex.add(new ChatMessage(body, false));
for(int i=0;i<messagex.size();i++){
ChatMessage xc = messagex.get(i);
System.out.println(xc.message);
}
adaptex.notifyDataSetChanged();
getListView().setSelection(messagex.size()-1);
Toast.makeText(hotListener.this,body,Toast.LENGTH_SHORT).show();
}
});
}
});
}
});
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for(RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){
if(HotspotService.class.getName().equals(service.service.getClassName())){
return true;
}
}
return false;
}
@Override
protected void onResume() {
bindService(new Intent(this, HotspotService.class), mConnection, Context.BIND_AUTO_CREATE);
adaptex.notifyDataSetChanged();
System.out.println("inside on resume");
super.onResume();
}
@Override
protected void onDestroy(){
super.onDestroy();
System.out.println("in on destroy");
unbindService(mConnection);
mHandler.removeCallbacksAndMessages(null);
}
@Override
protected void onPause() {
System.out.println("inside on pause");
super.onPause();
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
connection = null;
service = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
service = ((HotspotService.MyBinder)binder).getService();
connection = service.getConnection();
receivespots(connection,adaptex);
}
};
}
运行此类方法是正确的方法吗?绝对不是,我也可以尝试在sqlite中保存消息并在显示器上重新加载,但这也会失败,因为与mhandler关联的messagex不会重新初始化并且无法显示活动恢复后在屏幕上收到的任何消息。它第一次正常工作。但是,在处理程序中使用了时刻messagex,它继续将消息附加到旧的messagex,并且在活动恢复后无法显示