将onPostExecute()结果发送到服务

时间:2014-12-30 05:40:06

标签: java android android-asynctask

我有两个类,MainActivity和SMSMonitorService。

在MainActivity类中,我实现了AsyncTask。

在SMSMonitorService类中,我有一个带有两个参数的senReplySMS方法:String number和String responseText。

我想将onPostExecute()结果作为sendReplySMS参数(responseText)发送。

MainActiviy.class

public class MainActivity extends ActionBarActivity {
   /*.
     . Code
     .
   */

   public class DownloadWebpageTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {

        // params comes from the execute() call: params[0] is the url.
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }

    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {

    //Extract String between two delimiters.

        String finalResult = StringUtils.substringBetween(result, "extract\":\"", "\"}}}}");            
        String finalResponse = StringEscapeUtils.unescapeJava(finalResult);

        textView.setText(finalResponse);            

        Intent intent = new Intent(MainActivity.this, SMSMonitorService.class);
        intent.putExtra("onPostResult", finalResponse);
        startService(intent);            
    }
}

SMSMonitorService.class

public void sendReplySMS(String number, String responseText) { 

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(number, null, responseText, null, null);
}

public int onStartCommand (Intent intent, int flags, int startId){

    String onPostReceive = intent.getStringExtra("onPostResult");

    sendReplySMS(number, onPostReceive);

    return START_STICKY;
}

该应用无法启动。

如何将onPostExecute()结果字符串作为sendReplySMS方法参数发送?

感谢您提前。

编辑:

更正:当我按下切换按钮运行服务时,我会强行关闭。

logcat的:

12-30 00:24:33.647  10929-10929/com.textnet.apitest I/MainActivity﹕ SMSMonitor Started
12-30 00:24:33.927  10929-10929/com.textnet.apitest I/SMSMonitorService﹕ SMS Monitor service started
12-30 00:24:33.937  10929-10929/com.textnet.apitest I/SMSMonitorService﹕ Broadcast Receiver  registered 
12-30 00:24:33.977  10929-10929/com.textnet.apitest W/dalvikvm﹕ threadid=1: thread exiting with      uncaught exception (group=0x40b099f0)    
12-30 00:24:34.147  10929-10929/com.textnet.apitest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start service com.textnet.apitest.SMSMonitorService@4125e140 with Intent { cmp=com.textnet.apitest/.SMSMonitorService }: java.lang.IllegalArgumentException: Invalid destinationAddress
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2376)
        at android.app.ActivityThread.access$1900(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: Invalid destinationAddress
        at android.telephony.SmsManager.sendTextMessage(SmsManager.java:88)
        at com.textnet.apitest.SMSMonitorService.sendReplySMS(SMSMonitorService.java:31)
        at com.textnet.apitest.SMSMonitorService.onStartCommand(SMSMonitorService.java:38)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
        at android.app.ActivityThread.access$1900(ActivityThread.java:123)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
        at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

您应该覆盖onHandleIntent

,而不是定义onStartCommand

请注意,SMSMonitorService必须从IntentService派生才能执行此操作。

答案 1 :(得分:0)

我所知道的是你应该在调用SMSManager之后调用stofSelf():你可能会得到ANR,因为Service完成了任务并且它在UI主线程中运行。如果UI主线程被阻塞超过5秒,系统将触发ANR。请检查并恢复您的观察。

public void sendReplySMS(String number,String responseText){

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null, responseText, null, null);
stopSelf()

}

答案 2 :(得分:0)

检查onStartCommand()中的数字变量;一旦检查它是否已初始化,如果已初始化,请检查它是否是有效的电话号码