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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:50:15+00:00 2026-05-27T18:50:15+00:00

I can call an Activity from a BroadcastReceiver by this way: public class AlarmReceiver

  • 0

I can call an Activity from a BroadcastReceiver by this way:

public class AlarmReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        try {
            Intent i = new Intent(context, MyActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);

            Log.v(TAG, "alarm triggered");
        } catch (Exception e) {
            Log.v(TAG, e.toString());
        }
    }
}

This brings the app to the front and calls onResume() at the Activity. My problem is I can’t determine if the BroadcastReceiver brought me to onResume() or just the user itself by hand. Is there any way I can be sure that the BroadcastReceiver called the Activity?

I also tried to fill the Intent at the BroadcastReceiver with Extras by doing i.putExtra("foo", "bar"). I couldn’t read it out by calling MyActivity.getIntent().getExtras().get("foo") at the Activity.

Hope somebody can help me with this issue, thank you very much!

  • 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-27T18:50:15+00:00Added an answer on May 27, 2026 at 6:50 pm

    You can send extras in your intent and check them onReceive but proper way of doing this would be to set a boolean value for “STARTED_BY_RECEIVER”;

    When starting the activity, throw the third line into your onReceive callback method;

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

    Then in the onCreate of the activity, you can check if this was started by the broadcast receiver like this;

    if(getIntent().getExtras() != null && getIntent().getExtras().getBoolean("STARTED_BY_RECEIVER")){
        // The activity was started by the receiver
    }
    else{
       // The activity was started by user
    

    This would work because the getBoolean method would return false when there’s no extra called “STARTED_BY_RECEIVER”, this way you only have to put the flag in the one place (started by receiver)

    Also don’t forget to the put the “STARTED_BY_RECEIVER” in a static variable and use that in both places!

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

Sidebar

Related Questions

From this code I can call bmwCars.CopyToDataTable() as I expected. var bmwCars = from
I am looking for all the ways one can call another activity from an
In my activity, on user's request for preferences screen, I call: startActivity(new Intent(this, Preferences.class));
I am trying to call an activity from a non activity class but I
How can I call another activity from one (the current) activity? And for the
Using a delegate I can call any function asynchronously. From the documentation I understand
(Yes I know I can call Java code from Scala; but that is pointless;
I call an activity called Activity1 from an Activity called MainActivity using the following:
I have an activity than can be called from my parent activity and from
Does anyone know in android how you can call a non static public function

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.