1
votes

I developed chat app using xmpp by smack client. I used a background thread for incoming msg notification and working fine. But now when I am in chat view then I don't want notification of incoming msg. so I removed chatmangerlistener. but it is not working .

I used my second method that when I'll come in chat view then my background thread will be close. but i saw that background thread is not closing or stoping. isCancelling method is giving me false.

this is code :-

 public class incomingmsg extends AsyncTask<String, Void, String>
    {
      String msg;
        protected  String doInBackground(String... urls) {

            connection =  XMPPLogic.getInstance().getConnection();

            // register listeners
              ChatManager chatmanager = connection.getChatManager();
              chatmangerlistnr = new ChatManagerListener()
              {

                @Override
                public void chatCreated(final Chat chat, final boolean createdLocally) {
                    chat.addMessageListener(new MessageListener()
                      {
                        @Override
                        public void processMessage(Chat chat, Message message) {
                            msg = message.getBody();
                            System.out.println("Received message: " 
                                        + (message != null ? message.getBody() : "NULL"));
                            GeneratNotification(msg);
                        }
                      });
                }
              };

              connection.getChatManager().addChatListener(chatmangerlistnr);

              // idle for 20 seconds
            /* final long start = System.nanoTime();
              while ((System.nanoTime() - start) / 1000000 < 20000) // do for 20 seconds
              {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              }
              */
              System.out.println("is cancellable "+this.isCancelled());
            return msg;
        } 

          protected void onPostExecute(String r) {
             // GeneratNotification(r);

           }
    }

I m confusion if isCancellable() method is false then how can i stop it? or how can I remove my chatlistener?

please expert help me.

2

2 Answers

0
votes

Cancelling a task A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

Check the documentation for more.

0
votes

to start you async:

incomingmsg aTask = new incomingmsg();     
aTask.execute(...);

to stop you async

aTask.cancel(true);

by convention uses the name of its class starting with capital letter

public class incomingmsg extends AsyncTask<String, Void, String>...

change to:

public class Incomingmsg extends AsyncTask<String, Void, String>...

but it just a good practice

Full example:

AsyncTask<Void, Void, Void> myTask = new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {                   
                        //do this test ever step of you async task.                        
                        if(!isCancelled()){     
                         //do something here
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        myTask = null;
                    }

                };

  //here u start u task:
  myTask.execute(null, null, null);
  if(myTask != null){
   //here u stop u task:
   myTask.cancel(true);
  }