如何在BroadcastReceiver OnReceive事件上执行HTTP POST请求

时间:2014-01-08 15:56:25

标签: android broadcastreceiver

我想在BroadcastReceiver的OnReceive事件上向URL发送HTTP POST请求,但我得到异常NetworkOnMainThreadException,如何在OnReceive事件上运行线程。

public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();
    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();

                String senderNum = phonenumber;
                String message = currentmessage.getDisplayMessageBody();

                String msg = message.substring(5); 

                Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + msg);

                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + msg, duration);
                toast.show();   
                sendhttprequest("http://example.com/product", "rl", "12345678");                    
            }
        }
    }
    catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);

    }       

}

sendhttprequest是用于调用http post请求的函数。

请帮忙。

2 个答案:

答案 0 :(得分:4)

请记住,一旦onReceive()返回,BroadcastReceiver就可能被系统杀死。这意味着卸载到asynctask / thread的任何工作都不能保证完成。 考虑调用BroadcastReceiver:goAsync()以使进程保持活动状态最多10秒。 在此处阅读更多内容:http://developer.android.com/reference/android/content/BroadcastReceiver.html#goAsync()

答案 1 :(得分:1)

此示例可能对您有所帮助:

public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();
    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();

                String senderNum = phonenumber;
                String message = currentmessage.getDisplayMessageBody();

                String msg = message.substring(5); 

                Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + msg);

                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + msg, duration);
                toast.show();   
                new NetworkAccess().execute(); // Call AsyncTask                
            }
        }
    }
    catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);
    }       

    public class NetworkAccess extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // call some loader 
        }
        @Override
        protected Void doInBackground(Void... params) {
            // Do background task
            sendhttprequest("http://example.com/product", "rl", "12345678");
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            super.onPostExecute(result);
            // dismiss loader 
            // update ui 
        }
    }
}