How to send data to a running activity from Broadcast Receiver,

I am able to receive C2DM message fine but I want to send the data to a running activity, i.e when the activity is running, if the receiver receives C2DM message it is to send the data to the running activity. The code of receiver is (no bugs in the code):

public class C2dmreceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.w("C2DM", "Message Receiver called");
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) 
        {
            final String payload = intent.getStringExtra("key1");   
            Log.d("C2DM", "message = " + payload );
       }
     }}

I have tried like this inside the activity in an attempt to register the receiver in the activity so that the receiver can send data and the running activity can receive the data :-

  • How to deal with a large JSON object on Android
  • How to start/ launch application at boot time Android
  • actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) deprecated
  • Set target mail address in Firebase Invites
  • DataPicker looks like old design on new API's as well
  • How can I use Tesseract in Android?
  • C2dmreceiver c2dmr = new C2dmreceiver();
    Registration.this.registerReceiver(c2dmr, new IntentFilter());
    

    I don’t know what to put inside the IntentFilter(), also what else I have to put in the code of the activity and the code of the receiver so that while the activity is running and some C2DM message comes the receiver can send the data to the running activity.

    So, please tell me the code that is to put in the activity and in the receiver and may also be in the manifest so that the data from the receiver could be send to running activity.

    Any advice is highly appreciated.

    Related posts:

    how to download apk files from android market to pc?
    A more indepth explaniton of procstats for Android
    Send push notification from server to android device in Java
    Jsoup http error fetching url
    How can I align an image inside an ImageView to the bottom of the ImageView?
    Newly installed Eclipse and Android SDK. Can't get emulator to work. Hangs on clock screen
  • VLC RTSP live stream to Android
  • Change the icon of the push notification status bar on Android and IOS
  • Android Studio: Application Installation Failed
  • FloatingActionButton doesn't hide
  • NFC tag detection is not calling onNewIntent and it's Launching From Main Activity
  • Best way to secure Private REST API without user authentication for mobile app
  • One Solution collect form web for “How to send data to a running activity from Broadcast Receiver,”

    First of all it’s not the best idea to subscribe c2dm receiver in activity. Do it in manifest. For passing data to activity you can create static string field in Activity and set you String there.

    You can do something like this:

    in Activity

    public static YourActivity mThis = null;
    @Override
    protected void onResume() {
        super.onResume();
        mThis = this;
    }
    @Override
    protected void onPause() {
        super.onPause();
        mThis = null;
    }
    

    In your BroadcastReceiver:

    @Override
    public void onReceive(Context context, Intent intent) {
    ...
    if (YourActivity.mThis != null) {
        ((TextView)YourActivity.mThis.findViewById(R.id.text)).setText("received c2dm");
    }
    else {
    ...
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.