I wrote sms application for which I wrote BroadcastReceiver. I want to get data from BroadcastReceiver into my activity, so how do I get it.
My Broadcast Receiver code is below:
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFro开发者_JAVA百科mPdu((byte[])pdus[i]);
str += msgs[i].getOriginatingAddress();
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
I want to get back "str" value into another activity.
In broadcast:
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("sms-text", str);
context.startActivity(intent);
in your activity:
Intent intent = getIntent();
String sms = intent.getExtras().getString("sms-text");
精彩评论