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 9012361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:58:36+00:00 2026-06-16T02:58:36+00:00

I have a main Activity (OceanintelligenceActivity). In this activity I register the device for

  • 0

I have a main Activity (OceanintelligenceActivity). In this activity I register the device for push notifications and also I registered a receiver that shows a Dialog and starts the proper Activity depending on the info sent from my server. This is the code I’m using to register the device and the receiver :

protected void gcmRegistration(){

    PMApplication thisApp = PMApplication.getInstance();
    AppDelegate delegate = thisApp.getAppDelegate();
    final Context context = this;
    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);     
    // Make sure the manifest was properly set - comment out this line
    // while developing the app, then uncomment it when it's ready.     
    GCMRegistrar.checkManifest(this);

    // Let's declare our receiver
    registerReceiver(mHandleMessageReceiver,new IntentFilter(DISPLAY_MESSAGE_ACTION));

    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {       
        Log.d("", "Lets register for Push");
        GCMRegistrar.register(this, SENDER_ID);       
    }else {

      if(GCMRegistrar.isRegisteredOnServer(this)) {
          // Skips registration.                          
          String apnsToken = delegate.sso.getAPNSToken();           
          if(!apnsToken.equals(regId)){

            Log.d("", "The Device RegId has changed on GCM Servers");
            // We should let our servers know about this
            ServerUtilities.update(regId, context);
          }


      } else {        

          Log.d("","Is not register on PM Server");               
          // Try to register again, but not in the UI thread.
          // It's also necessary to cancel the thread onDestroy(),
          // hence the use of AsyncTask instead of a raw thread.              
          mRegisterTask = new AsyncTask<Void, Void, Void>() {

              @Override
              protected Void doInBackground(Void... params) {

                  boolean registered = ServerUtilities.register(context, regId);
                  // At this point all attempts to register with the app
                  // server failed, so we need to unregister the device
                  // from GCM - the app will try to register again when
                  // it is restarted. Note that GCM will send an
                  // unregistered callback upon completion, but
                  // GCMIntentService.onUnregistered() will ignore it.
                  if (!registered) {
                      GCMRegistrar.unregister(context);
                  }
                  return null;
              }

              @Override
              protected void onPostExecute(Void result) {
                  mRegisterTask = null;
              }

          };
          mRegisterTask.execute(null, null, null);

      }
    }                         
}

This is how I set the receiver:

private final BroadcastReceiver mHandleMessageReceiver =
        new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        Log.d("","BroadcastReceiver onReceive");  
        notificationIntent = GCMIntentService.getNotificationIntent(context);

        new AlertDialog.Builder(context)
        .setMessage(newMessage+". Would you like to see it right now?")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {                                                       
                // Show update                                                              
                startActivity(notificationIntent);                                                              
            }
        })
        .setNegativeButton("No", null).show();                                    
    }
};

GCMIntentService.getNotificationIntent(context). This line returns the the Intent with the Activity I want to start.

Whenever there is a notification onReceive gets called but the Dialog only shows if I am on the main activity. So if the app is on a different activity, onReceive still gets called but the dialog doesn’t show and therefore I can’t start the proper activity.

How can I display a dialog on Currently visible activity on BroadcastReceiver?

  • 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-06-16T02:58:37+00:00Added an answer on June 16, 2026 at 2:58 am

    Playing around with this one and searching on google I came across a solution. It is not the best one but it works. I still can’t believe there is not an easy way to get the current context in Android. So this is what I did to manage to show the Dialog regardless of what the current activity is : I have a public static property of type Context on my singleton class(AppDelegate) and on each activity I override the onResume method and set the Context to the current activity like this AppDelegate.CURRENT_CONTEXT = this. Then on my dialog : AlertDialog.Builder(AppDelegate.CURRENT_CONTEXT)…..

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

Sidebar

Related Questions

i have problems with my game. I have an Main Activity that's shows a
I have Application that contains main Activity, Service and several Threads run on this
I have a main activity, that holds the main menu. This menu has an
Splash I have main activity UI startup operations that take between 5-10 seconds (that
I have a design of activities like this I have one main activity and
I am making an app in which i have a main activity that the
I have a lot of buttons on main activity. Each button run this same
I have a main activity with this layout file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
I have a main activity that takes elements from a database and displays them
I have this problem in android. i have a main activity who calls a

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.