1

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

我该做什么?请帮忙。

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

活动.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
}

清单.xml

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

1 回答 1

0

编辑:

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

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

这在你的代码中:

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

替换your.package.name为您的包名!希望有帮助:)

于 2013-10-05T02:10:37.613 回答