开发者

How to get data back from BroadcastReceiver to Activity?

开发者 https://www.devze.com 2023-02-25 07:57 出处:网络
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.

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");
0

精彩评论

暂无评论...
验证码 换一张
取 消