Skip to content Skip to sidebar Skip to footer

How To Send/receive Broadcast Between Service And Activity

I need to send message from SmsReceiver Class to My Main_Activity and can't do it. I tried out very much and searched But... . Here is my code SmsReceiver.java public class SmsRece

Solution 1:

Your code to send and register broadcast receiver not correct. Send broadcast:

Intentintent=newIntent("your_action_name");
intent.putExtra(....);
sendBroadcast(intent);

receive:

BroadcastReceiver mysms=newBroadcastReceiver() {
      @OverridepublicvoidonReceive(Context arg0, Intent arg1) {
          String sms=arg1.getExtras().getString("m");
          intext.setText(sms);

      }
};
registerReceiver(mysms, newIntentFilter("your_action_name"));

and remember to unregister when destroy your activity.

Solution 2:

Instead of context.sendBroadcast(intent1); you should try context.startActivity(intent1);

try this

Intent i = newIntent(context,Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(i);

Post a Comment for "How To Send/receive Broadcast Between Service And Activity"