0

in my app the user_A can send message to the user_B, when the user_A send message to user_B then user_B will return value (status) that the message sent has been received. user_B return status to user_A use asyntask and work very well, but I get problems when user_A send many messages repeatedly and quickly, automatically my asyntask called repeatedly and I get warning "MessageQueue"

12-31 14:09:33.664: W/MessageQueue(3264): Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a      Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): java.lang.RuntimeException: Handler (android.location.LocationManager$ListenerTransport$1) {41403330} sending message to a Handler on a dead thread
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageAtTime(Handler.java:473)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessageDelayed(Handler.java:446)
12-31 14:09:33.664: W/MessageQueue(3264): at android.os.Handler.sendMessage(Handler.java:383)

here my Code

private class messageReceived extends AsyncTask<Void, Void, String> {
    private String val; 
    public messageReceived() {
    }

    @Override
    protected String doInBackground(Void... unsued) {
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        HttpPost post = new HttpPost("http://myUrl.com/updateSts.php");
        List<NameValuePair> nVP = new ArrayList<NameValuePair>(2);  
        nVP.add(new BasicNameValuePair("msg_id", "023921"));  
        post.setEntity(new UrlEncodedFormEntity(nVP));

        response = client.execute(post);
         val= org.apache.http.util.EntityUtils.toString(response.getEntity());
        return val;
    }

    @Override
    protected void onPostExecute(String val) {
        Log.d(TAG,"posExecuteClass=>"+val);
    }
}

how do I solve this problem ? there may be other ways besides this,. thanks

ltvie
  • 931
  • 4
  • 19
  • 48
  • I have pretty similar problem with async task. I found out that there could be only 5 async task running at same time in my app. When i runned another one he was waiting until any of the first 5 ends and then runns normally. Normal threads doesn't have that problem co you could try rewrite it using normal java threads. – Karol Żygłowicz Apr 13 '15 at 07:00
  • yes, i read many tutorial about that and only 5 async task running at same time, maybe any algorithm or another code with same function with asynctasc ? what "using normal java threads " means ? – ltvie Apr 13 '15 at 07:34
  • normal java threads means using Thread class for doing doInBackground work instead of asyncTask: `new Thread(new Runnable() { public void run() { // here your background work } }).start();` But this is more complex read tutorials about threads first if you are not familiar with them http://stackoverflow.com/questions/4722974/threading-example-in-android – Karol Żygłowicz Apr 13 '15 at 07:42
  • if use "thread runnable" from your answer above, there is any limit running at same time like async task? – ltvie Apr 13 '15 at 07:59
  • Use http://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor.html Asynctask is used when you want to post something back on UI after the Runnable is done on background. Since you don't have anything to post back on UI, its better to use Threads in combination with Threadpool where you have better control.' – suresh Apr 13 '15 at 08:18
  • as far as i'm concerned no there is no such limit. (Excluding hardware limits) – Karol Żygłowicz Apr 13 '15 at 08:21
  • ok, thanks karol and @suresh for clear explanation .. i'll try to use threads for my app based on your answer.. – ltvie Apr 13 '15 at 08:50

0 Answers0