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>