使用sendTextMessage()将数据传递到广播接收器

时间:2013-10-04 22:33:00

标签: java android android-intent broadcastreceiver sms

使用sendTextMessage()时如何将数据传递到广播接收器 我使用了intent.putExtra("key","value");,当onReceive函数调用intent.getStringExtra("key")的值为 null 时。

我该怎么办?请帮忙。

注意:使用sendBroadcast(new Intent(smsSent).putExtra("key","value"));时,值已通过

Activity.java

void send_sms(){
String smsSent = "SMS_SENT";
String smsDelivered = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(smsSent).putExtra("key1", "value1"), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(smsDelivered).putExtra("key2", "value2"), 0);

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("5556", null, "hi", sentPI, deliveredPI);
}

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

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

 Log.i("my receiver"," "+intent.getAction());// this statement works fine
 Log.i("my receiver"," "+intent.getStringExtra("key1"));// null is returned
}

的manifest.xml

<receiver android:name=".MyReceiver">
<intent-filter>
    <action android:name="SMS_SENT" />
    <action android:name="SMS_DELIVERED" /> 
    </intent-filter>
</receiver>

1 个答案:

答案 0 :(得分:0)

修改

我认为问题可能是,你没有提到你的包裹。 在你的清单中试试这个:

<intent-filter>
    <action android:name="your.package.name.SMS_DELIVERED" />
</intent-filter>

并在您的代码中:

String smsDelivered = "your.package.name.SMS_DELIVERED";

用您的包裹名称替换your.package.name! 希望有所帮助:)