为什么从活动传递到广播接收器时字符串值为空? [英] Why is the string value null when passed from activity to broadcast receiver?

查看:28
本文介绍了为什么从活动传递到广播接收器时字符串值为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个值从活动传递到广播接收器:

I am passing a value from activity to broadcast receiver:

我的活动代码:

String stringBuilder = "Hello";
String phone_no = "1234567890";

try{
     SmsManager sms = SmsManager.getDefault();

     sms.sendTextMessage(phone_no, null, stringBuilder, null, null);

     Intent broadcastIntent = new Intent(contacts.this, Receiver.class);

     broadcastIntent.putExtra("message",stringBuilder);

     sendBroadcast(broadcastIntent);

 }catch (Exception e) {
     e.printStackTrace();
 }

我的广播接收器代码:

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

         //String msg= intent.getStringExtra("message") //returns null

         Bundle bundle = intent.getExtras();

         if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {

             String msg= intent.getStringExtra("message") //returns null

             if (bundle != null){

                  Object[] pdus = (Object[]) bundle.get("pdus");

                  if (pdus.length == 0) {
                    return;
                  }

                  // large message might be broken into many
                  SmsMessage[] messages = new SmsMessage[pdus.length];
                  StringBuilder sb = new StringBuilder();
                  for (int i = 0; i < pdus.length; i++) {
                      messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                      sb.append(messages[i].getMessageBody());
                  }
                  String sender = messages[0].getOriginatingAddress();
                  String message = sb.toString();
                  
                if (message.equals(msg)) {
                    //do something
                }

              }
   
             Toast.makeText(context,message,Toast.LENGTH_SHORT).show(); //empty toast
    }
}

这是我的清单:

         <receiver
            android:name=".Receiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="2147483647" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

正如您所看到的,key 在这两种情况下都是相同的,当从活动传递时,该值在我的广播接收器中仍然返回 null.我已经没有关于如何将值从活动传递到广播接收器的想法.请帮帮我.

As you can see the key is same in both the cases, still the value is returned null in my broadcast receiver, when passed from the activity. I've run out of ideas on how to pass the value from activity to broadcast receiver. Please help me.

我只想检查在我的广播接收器中收到的消息 (String message = sb.toString();) 是否等于从活动发送的消息 (stringBuilder).

I just want to check if the message received in my broadcast receiver (String message = sb.toString();) is equal to the message sent from the activity (stringBuilder).

推荐答案

好吧,再看一遍你的代码,我注意到一条可能会导致问题的可疑线条.

Well, looking at your code one more time, I noticed a fishy line that might causes the problem.

broadcastIntent.putExtra("message",stringBuilder);

你的意思是,像这样使用它:

Did you mean, to use it like this:

broadcastIntent.putExtra(android.content.Intent.EXTRA_TEXT,stringBuilder);

那么你可以通过以下方式获得:

Then you can get it by:

intent.getStringExtra(android.content.Intent.EXTRA_TEXT);

或者如果想发送多个东西:

Or if want to send multiple stuff:

broadcastIntent.putExtra(android.content.Intent.EXTRA_TEXT, new String[] { stringBuilder, phone_no } );

获取它们:

String[] str = intent.getStringArrayExtra(android.content.Intent.EXTRA_TEXT);
String message = str[0];
String phone_no = str[1];

或者您也可以使用 Bundle.

Bundle bundle = new Bundle();
bundle.putCharSequence("message", stringBuilder);
bundle.putCharSequence("phone_no", phone_no); 
broadcastIntent.putExtras(bundle);

稍后您可以使用以下方法检索它们:

Later you can retrive them using following method:

public class Receiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
        {
            //String message= intent.getStringExtra("message") //returns null
            Bundle bundle = intent.getExtras();
            if(bundle != null) {
                String message = bundle.getString("message");
                Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
            } else {
                 Toast.makeText(context,"Bundle is null",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

这篇关于为什么从活动传递到广播接收器时字符串值为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆