0

我目前有一个管理广播接收器的服务。此接收器可以关闭和打开。

目前,当收到 SMS 时触发 OnReceive。然后它将 SMS 上传到服务器。代码工作正常,但我听说 3.0 以上的网络连接需要在异步任务上运行。即使不是这种情况,我也想将上传移至另一个线程,因此上传任务不能阻塞 UI 线程。(我已经厌倦了将服务移动到 IntentService,这会导致广播接收器泄漏)

          SMSBR = new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                //START OF ONRECIVER************************
                Bundle extras = intent.getExtras();

                String strMessage = "";

                if ( extras != null )
                {
                    Object[] smsextras = (Object[]) extras.get( "pdus" );

                    for ( int i = 0; i < smsextras.length; i++ )
                    {
                        SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);

                        String strMsgBody = smsmsg.getMessageBody().toString();
                        String strMsgSrc = smsmsg.getOriginatingAddress();

                        strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;                    

                        Log.d("Message", "is "+strMessage);
                        //***************************************************************
                        InputStream is = null;
                        try{
                            HttpClient httpclient = new DefaultHttpClient();
                            HttpPost httppost = new HttpPost("http://mayar.abertay.ac.uk/~1001077/insert.php");

                            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                                nameValuePairs.add(new BasicNameValuePair("DeviceNumber", telephonyManager.getLine1Number().toString()));
                                nameValuePairs.add(new BasicNameValuePair("SenderNumber", strMsgSrc));
                                nameValuePairs.add(new BasicNameValuePair("Message", strMsgBody));
                                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                                HttpResponse response = httpclient.execute(httppost);
                                HttpEntity entity = response.getEntity();
                                is = entity.getContent();
                                Log.d("is = ", is.toString());
                                Log.d("In SMS", "Sender success");
                                  if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
                                        Log.d("Message upload Running", "on UI");
                                    } else {
                                        Log.d("Message upload NOT running", "on UIx");
                                    }

                        }catch (Exception e){
                            Log.e("Log_tag", "Error in http sms " + e.toString());
                            Log.d("In SMS", "Sender NOT success");
                        }
                        //****************************************************************

                    }

                }
                //END OF ON RECEIVE****************************
            }


          };
4

1 回答 1

1

让我在不谈论 AsyncTasks 的情况下回答您的问题:看看

BroadcastReceiver.registerReceiver(BroadcastReceiver接收器、IntentFilter过滤器、String broadcastPermission、Handler调度器)

我发现,当我检查有关 BroadcastReceivers 的一些文档时,它看起来正是您所需要的(查看此链接)。还有一个提示:将它与HandlerThread.

于 2013-05-01T15:53:18.763 回答