Question

I am creating an application in Android in which whenever a user receives new Text message (SMS) it is first sent on server and if server replies true the Broadcast is aborted else we Broadcast it.

Now the problem is we can not communicate with server on UI thread, so to overcome this problem we have to use some kind of thread to perform background operations, therefore I am using AsyncTask to communicate with server. Here is my code:

Android App:

public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public static String message;

public void onReceive(final Context context, Intent intent) {

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();
    String senderNum;
    try {

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage
                        .createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage
                        .getDisplayOriginatingAddress();

                senderNum = phoneNumber;
                message = currentMessage.getDisplayMessageBody();
                CallServer callServer = new CallServer();
                callServer.execute();
                Log.d("textmsg", callServer.getStatus().toString());

                /*
                  while (callServer.getStatus().toString() == "RUNNING") {
                  //Log.d("textmsg", "working in loop"); }
                 */

                if(callServer.get() == "true")
                    abortBroadcast();

                Log.d("textmsg", callServer.getStatus().toString() + "done");

                // Show Alert
                Toast toast = Toast.makeText(context, "senderNum: "
                        + senderNum + ", message: " + message,
                        Toast.LENGTH_LONG);
                toast.show();

            } // end for loop
        } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);

    }
}

private class CallServer extends AsyncTask<String, String, String> {
    @SuppressLint("NewApi")
    protected String doInBackground(String... arg0) {
        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;

        try {
            socket = new Socket("192.168.1.100", 8081);
            Log.d("textmsg", "connected");
            dataOutputStream = new DataOutputStream(
                    socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());
            if (!message.isEmpty()) {
                dataOutputStream.writeBytes(message);

                return (dataInputStream.readBoolean() == true) ? "true"
                        : "false";
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            Log.e("error", e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("error", e.toString());
        }

        return "false";
    }

     @Override protected void onPostExecute(String result){

         Log.d("textmsg", result);

     }

}
}

The problem which I can not understand is that whenever I use callServer.get() or while (callServer.getStatus().toString() == "RUNNING"){} my application crashes I am doing this because I have to wait for server reply i.e: true or false so if server replies true I can abort the broadcast.

LogCat:
D/textmsg(29494): RUNNING
D/textmsg(29494): connected
I/dalvikvm(29399): threadid=3: reacting to signal 3
I/dalvikvm(29494): Wrote stack traces to '/data/anr/traces.txt'

Note: My Server Side code is working perfectly as I receive message when my application sends request on server. I have also tested my server side code by creating simple client server application. My server is running on my local machine so I don't have any network issue.

I have also tried FutureTask however I was facing same problem. Any help would be greatly appreciated.

Was it helpful?

Solution

And that's why you don't want to use .get(). This will actually block your main UI waiting for the result of the AsyncTask execution, so your app will get ANR. Don't use it.

Instead, assign the result once you're done processing your onPostExecute() method to some Activity's variable.

OTHER TIPS

callServer.get() calling get blocks the ui thread waiting for the result. You should never block the ui thread. You need not call get(). Calling get() will make it asynchronous no more.

callServer.execute() invokes doInBackground which is enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top