1

I am having problem with chatting app , I am trying to run chat receiver functionality using handler such that as soon as messages are received they are taken care of and displayed on screen . But it fails when I try to go back and resume the chatting, since Handler keeps on running so is the message object associated with it , and it fails to reinitialize it. Following is the code :

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);
            }
        };
   } 

Is it right way to run such methods ? Definitely not , I can also try to save messages in sqlite and reload on display but that will also fail , since messagex associated with mhandler does not reinitializes and fails to display any message received on screen after resume of activity . It does work properly for first time . But moment messagex is used in handler it keeps on appending messages to old messagex and fails to display after resume on activity

Harshit
  • 1,207
  • 1
  • 20
  • 40

0 Answers0