Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7437073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:20:30+00:00 2026-05-29T10:20:30+00:00

I have an Android application which uses C2DM services (aka push). I have a

  • 0

I have an Android application which uses C2DM services (aka push).

I have a separate class which implements the registration process and which receives the data (and extends BroadcastReceiver).

I want to communicate this data to the activity which currently is in the foreground. The activity currently in the foreground may differ depending on user action.

What’s the best way to communicate in between the receiver and the current activity?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:20:30+00:00Added an answer on May 29, 2026 at 10:20 am

    I solved this problem by sending out a new broadcast from the C2DMReceiver class, which looked something like this.

    The C2DMReceiver class:

    public class C2DMReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {    
                handleRegistration(context, intent);
            } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
                handleMessage(context, intent);
            }
        }
    
        private void handleRegistration(Context context, Intent intent) {
            // handle registration
        }
    
        private void handleMessage(Context context, Intent intent) {
            Intent i = new Intent("push");
            i.putExtras(intent);
            // context.sendOrderedBroadcast(i, null);
            context.sendBroadcast(i);
        }
    
    }
    

    Another class I called PushReceiver. This is the class that will extend BroadcastReceiver and receive the broadcast sent by C2DMReceiver.

    public class PushReceiver extends BroadcastReceiver {
    
        public PushReceiver() {
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // do stuff
            abortBroadcast();
        }
    
        public static class PushFilter extends IntentFilter {
    
            private static final int DEFAULT_PUSH_PRIORITY = 1;
    
            public PushFilter() {
                this(DEFAULT_PUSH_PRIORITY);
            }
    
            public PushFilter(int priority) {
                super("push");
                setPriority(priority);
            }
    
        }
    
    }
    

    And the activity class, in this case called MyActivity. This should work well if you are using a base activity class that all other activities extend. That way every activity registers the receiver. By doing the register/unregister in onResume/onPause, you should be able to guarantee that only the current activity receives the broadcast. If not, you can send an ordered broadcast from C2DMReceiver and use priority in the PushFilter.

    public class MyActivity extends Activity {
    
        private PushReceiver pushReceiver;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // your onCreate method
            pushReceiver = new PushReceiver();
        }
    
        public void onResume() {
        super.onResume();
            // your onResume method
            registerReceiver(pushReceiver, new PushReceiver.PushFilter());
        }
    
        public void onPause() {
            super.onPause();
            // your onPause method
            unregisterReceiver(pushReceiver);
        }
    
    }    
    

    In my case, I wrote the PushReceiver constructor to take a View and then “did stuff” with the view in the onReceive method. Without knowing more about what your trying to do, I can’t elaborate on this, but hopefully this can provide a decent template to work from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In eclipse workset I have an android library and an application which uses this
I have written an Android application that uses SQLite database which is saved in
I have developed an android application, which uses image files and strings from the
I have written a simple Android application that uses a sqlite database which is
I have an application which uses the tts engine in Android, now as the
In my android application which uses GPS, I have UI elemnts that should blink
I have an Android application, which uses javax.crypto to encrypt some text data in
I have an Android application which uses the NDK to execute a large amount
I'm working on an application which uses SQLite Database on Android.I have a custom
I have a simple Android application that uses an instance of a class, let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.