如何将数据从BroadcastReceiver传输到服务

时间:2012-04-23 14:11:13

标签: android broadcastreceiver android-service

我正在创建一个应用程序,它用于通过短信进行轮询。我想要的是,如果有人说“是”,我会得到+1的肯定数,对于“不”的情况也是如此,我希望这是在后台完成的,所以我正在使用服务。并在我的MainActivity上显示最终结果。我试过了,并做了以下代码:: BroadcastReciver

/*Bundle bundle = intent.getExtras();
        SmsMessage[]  msg = null;
        String body = "";
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            msg = new SmsMessage[pdus.length];
            for (int i = 0; i < msg.length; i++) {
                body = msg[i].getMessageBody().toString();              
            }
        }*/

        String body = "yes";
        if(body.equals("YES") || body.equals("yes")|| body.equals("y") || body.equals("Y")){
            Intent yes = new Intent(context,PollCounter.class);
            yes.putExtra("yes", true);
            yes.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(yes);
            context.stopService(yes);
        }
        else if(body.equals("NO") || body.equals("no")|| body.equals("n") || body.equals("N")){
            Intent no = new Intent(context,PollCounter.class);
            no.putExtra("no", true);
            no.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(no);
            context.stopService(no);
        }
        else {
            Intent invalidMsg = new Intent(context,PollCounter.class);
            invalidMsg.putExtra("invalidMsg", true);
            context.startService(invalidMsg);
        }
    }

}

BroadcastReceiver也有问题,我得到一个“E / AndroidRuntime(708):java.lang.RuntimeException:无法启动接收器com.atiffarrukh.smspolling.SMSBroadcastReciever:java.lang.NullPointerException”错误,当我取消注释以下

/*Bundle bundle = intent.getExtras();
            SmsMessage[]  msg = null;
            String body = "";
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msg = new SmsMessage[pdus.length];
                for (int i = 0; i < msg.length; i++) {
                    body = msg[i].getMessageBody().toString();              
                }
            }*/

不明白的原因,我之前使用过这段代码而且它正在运行。

Service ::

boolean yes = intent.getBooleanExtra("yes", false);
        boolean no = intent.getBooleanExtra("no", false);
        boolean invalidMsg = intent.getBooleanExtra("invalidMsg", false);

        if (yes) {
            yesCount++;
            Intent intentYes = new Intent(this,MainActivity.class);
            intentYes.putExtra("yesCount", yesCount);
            startActivity(intentYes);
            Toast.makeText(getBaseContext(), "yes is recvd", Toast.LENGTH_LONG).show();
        } else if (no) {
            noCount++;
            Intent intentNo = new Intent(this,MainActivity.class);
            intentNo.putExtra("yesCount", noCount);
            startActivity(intentNo);
            Toast.makeText(getBaseContext(), "no is recvd", Toast.LENGTH_LONG).show();
        } else {
            invalidMsgCount++;
            Intent intentInavalidMsg = new Intent(this,MainActivity.class);
            intentInavalidMsg.putExtra("yesCount", invalidMsgCount);
            Toast.makeText(getBaseContext(), "Invalid is recvd", Toast.LENGTH_LONG).show();
        }

    }

我尝试使用Bundle b = intent.getExtras();,但我不确定如何使用它,以及我应该用{。}}初始化{。}}。

谢谢...

P.S:我是android新手。对不起,如果我的问题有点奇怪或我的编码似乎很奇怪。

1 个答案:

答案 0 :(得分:0)

好像你忘记了

msg[i] = SmsMessage.createFromPdu((byte[])pduArray [i]);

在你的周期中。

应该是

for (int i = 0; i < msg.length; i++) {
         msg[i] = SmsMessage.createFromPdu((byte[])pduArray [i]);
         body = msg[i].getMessageBody().toString();              
}

否则getMessageBody()确实会返回null