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

  • Home
  • SEARCH
  • 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 6194819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:21:38+00:00 2026-05-24T03:21:38+00:00

I’ve got an Android application which needs to be woken up sporadically throughout the

  • 0

I’ve got an Android application which needs to be woken up sporadically throughout the day.

To do this, I’m using the AlarmManager to set up a PendingIntent and have this trigger a BroadcastReceiver. This BroadcastReceiver then starts an Activity to bring the UI to the foreground.

All of the above seems to work, in that the Activity launches itself correctly; but I’d like the BroadcastReceiver to notify the Activity that it was started by the alarm (as opposed to being started by the user). To do this I’m trying, from the onReceive() method of the BroadcastReceiver to set a variable in the extras bundle of the intent, thus:

    Intent i = new Intent(context, MyActivity.class);
    i.putExtra(wakeupKey, true);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

In the onResume() method of my Activity, I then look for the existence of this boolean variable:

protected void onResume() {
    super.onResume();

    String wakeupKey = "blah";      
    if (getIntent()!=null && getIntent().getExtras()!=null)
        Log.d("app", "onResume at " + System.currentTimeMillis() + ":" + getIntent().getExtras().getBoolean(wakeupKey));
    else
        Log.d("app", "onResume at " + System.currentTimeMillis() + ": null");
}

The getIntent().getExtras() call in onResume() always returns null – I don’t seem to be able to pass any extras through at all in this bundle.

If I use the same method to bind extras to the PendingIntent which triggers the BroadcastReceiver however, the extras come through just fine.

Can anyone tell me what’s different about passing a bundle from a BroadcastReceiver to an Activity, as opposed to passing the bundle from an Activity to a BroadcastReceiver? I fear I may be doing something very very obvious wrong here…

  • 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-24T03:21:38+00:00Added an answer on May 24, 2026 at 3:21 am

    Just to make it clear (because I used a lot of time figuring out how to make it work)

    In the service class that extends BroadcastReceiver. Put in the following code in onReceive()

    Intent intent2open = new Intent(context, YourActivity.class);
    intent2open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent2open.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    String name = "KEY";
    String value = "String you want to pass";
    intent2open.putExtra(name, value);
    context.startActivity(intent2open);
    

    The FLAG_ACTIVITY_SINGLE_TOP makes sure the apps doesn’t re-open if already open. This means that the “old” intent that opened YourActivity in the first place is re-used and it will NOT contain the extra values. You have to catch them in another method called onNewIntent() in YourActivity.

    public class YourActivity extends Activity {
        private String memberFieldString;
    
        @Override
        public void onCreate(Bundle savedInstanceState) { 
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
    
             // Code doing your thing...
        } // End of onCreate()
    
        @Override
        protected void onNewIntent(Intent intent) {
        Log.d("YourActivity", "onNewIntent is called!");
    
        memberFieldString = intent.getStringExtra("KEY");
    
        super.onNewIntent(intent);
    } // End of onNewIntent(Intent intent)
    
        @Override
        protected void onResume() {
            if (memberFieldString != null) {
                if (opstartsIntent.getStringExtra(KEY) != null) {
                   Log.d("YourActivity", "memberFieldString: "+ memberFieldString);
                } else {
                   Log.d("YourActivity", "The intent that started YourActivity did not have an extra string value");
                }
            }
        } // End of onResume()
    
    }  // End of YourActivity
    

    Please note the two if statements – the onResume() does not know if it’s called after OnCreate()->OnStart() OR onRestart()->onStart()

    Please see: http://www.anddev.org/images/android/activity_lifecycle.png

    It’s just used to test if the app is launched by the user (intent with no extras) OR by the BroadcastReceiver (intent with extras).

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.