Issue
I am passing a value from activity to broadcast receiver:
My activity code:
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();
}
My broadcast receiver code:
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
}
}
This is my manifest:
<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>
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.
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
).
Solution
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 } );
To get them:
String[] str = intent.getStringArrayExtra(android.content.Intent.EXTRA_TEXT);
String message = str[0];
String phone_no = str[1];
Or you can use a Bundle
as well.
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();
}
}
}
}
Answered By - Darkman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.