Skip to content Skip to sidebar Skip to footer

Sending Intent From Broadcastreceiver Class To Currently Running Activity

I have a class which extends BroadcastReceiver. On receiving a SMS, I would like to pass information to my main activity class to display the text in a box (Append, if already text

Solution 1:

You have three ways: 1) You can define your broadcast inside your MainActivity like this: in onCreate()

registerReceiver(smsReceiver, new IntentFilter(SMS_RECIEVED));  

and define smsReciver in MainActivity

privateBroadcastReceiversmsReceiver=newBroadcastReceiver() {
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        //you can update textBox here
        handler.postDelayed(sendUpdatesToUI, 10);  
    }
};  

define a runnable to update UI

private Runnable sendUpdatesToUI = new Runnable() {
    publicvoidrun() {
        update();
    }
};

and update method

private void update(String text) {
    textView.setText(textView.getText().toString() + text);
} 

2) Register a receiver between your Activity and BroadCastReceiver

3) Start your Activity with new Intent to update current open Activity

Intent intent = newIntent(context, MainActivity.class);
intent.putExtra("Key", "text");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);  

UPDATE : explain method 2 MainActivity.class

in onResume()

registerReceiver(broadcastReceiver, new IntentFilter(SmsReceiver.BROADCAST_ACTION));  

in onDestroy()

unregisterReceiver(broadcastReceiver);

local broadCast (broadcastReceiver, in MainActivity.class)

privateBroadcastReceiver broadcastReceiver = newBroadcastReceiver() {
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        updateUI(intent);
    }
};
privatevoidupdateUI(Intent intent) {
    String text = intent.getStringExtra("key");
    textView.setText(textView.getText().toString() + text);
}

SmsReceiver.class global attribute

publicstaticfinalStringBROADCAST_ACTION="your.package.name.displayevent";
privatefinalHandlerhandler=newHandler();
Intent intent;
Context context;

in onReceive()

handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 10);

this.context = context;//you can retrieve context from onReceive argumentthis.intent = new Intent(BROADCAST_ACTION);

define two method

private Runnable sendUpdatesToUI = new Runnable() {
    publicvoidrun() {
        display();
    }
};

privatevoiddisplay() {
    intent.putExtra("key", text);
    context.sendBroadcast(intent);
}

Solution 2:

Modify your code as below.

publicclassSmsReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent)
    { 
        Intenti=newIntent(context, MainActivity.class);
            i.putExtra("updatedString","Hello");            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(i);
    } 
}

publicclassMainActivityextendsActivity{

  private TextView results;
  @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundleextras= getIntent().getExtras();
        if(extras!=null){
            results = (TextView) findViewById(R.id.results);
            results.setVisibility(View.VISIBLE);
            results.append(extras.getString("updatedString"));
        } 

@OverrideprotectedvoidonNewIntent(Intent intent) {
        super.onNewIntent(intent);
      //handle your intent here.Note this will be called even when activity first created.so becareful to handle intents correctly.
    }

}

Post a Comment for "Sending Intent From Broadcastreceiver Class To Currently Running Activity"