启动服务并返回值

时间:2014-05-10 15:16:02

标签: android

我从OnReceive开始提供一些服务,当服务结束时我想从该服务获得一些价值回到OnReceive()..

那是我的代码:

public class SmsListener extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {

            Bundle bundle = intent.getExtras();

            Object messages[] = (Object[]) bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
            for (int n = 0; n < messages.length; n++) {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            }

            //show first message
            Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
            toast.show();
            String s=smsMessage[0].getMessageBody();
            //START A SERVICE
            Intent serviceIntent = new Intent(context,Lineeq.class);
            serviceIntent.putExtra("message",s); 
            context.startService(serviceIntent);

//这里我需要从服务中获取一个值

            abortBroadcast();


        }


                                  any ideas?
                                  Thanks.

1 个答案:

答案 0 :(得分:1)

BroadcastReceiver返回后,onReceive()将消失一两毫秒。您无法从服务返回结果,更不用说以某种方式阻止等待它的onReceive()方法。

另外,请记住abortBroadcast()无效starting with Android 4.4,因为您无法中止短信广播。