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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:39:42+00:00 2026-05-27T14:39:42+00:00

I have a setup where I am running a background service in Android with

  • 0

I have a setup where I am running a background service in Android with an attached BroadcastReceiver listening for particular types of Intents. Those intents are then used by the broadcast receiver to call specific methods in the service. Below is code describing the structure of the service:

public class MyService extends Service {

     private class ServiceBroadcastReceiver extends BroadcastReceiver {


            private MyService getMyService() {
                return MyService.this;
            }

            public IntentFilter getASReceiverFilter() {
                IntentFilter filter = new IntentFilter()
                filter.addAction(Constants.ACTION_DO_SOMETHING);
                return filter;
            }

            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                MyService svc = getMyService();
                String intentAction = intent.getAction();
                Log.i(Constants.LOG_TAG, "Now trying to dispatch following action: " + intentAction);
                //Dispatch to the appropriate method in MyService. 
                if (intentAction.equals(AppServiceConstants.ACTION_DO_SOMETHING)) {
                    svc.doSomething();
                } 
            }
        }


    private ServiceBroadcastReceiver mRequestReceiver = new ServiceBroadcastReceiver();


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public void onCreate() {

        Log.i(Constants.LOG_TAG, "Service Created!");
        //Register for broadcast messages indicating the add music button was pressed
        mRequestReceiver = new ServiceBroadcastReceiver();
       registerReceiver(mRequestReceiver, mRequestReceiver.getASReceiverFilter()); 
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(Constants.LOG_TAG, "Service Starting ... ");

        // If we get killed, after returning from here, restart
            return START_STICKY;
    }



    /***********************************************************************************
     * Service Dispatch Methods
     */
     protected void doSomething() {
         Log.i(Constants.LOG_TAG, "Doing something"); 
     }



}

My issue is with getting this service and its broadcast receiver to capture broadcasted intents from throughout my app. If I do something like the following ….

getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));

.. it only works if I invoke that from within an Activity. However, I want to be able to “do something” even when the application first starts up, so I have put that statement in my own custom Application subclass in the onCreate() method, right after I create the MyService and start it up. For some reason, the broadcast is not working from within the Application class code when the app start up, but it will work if I put it in some code within an activity (eg in the onCreate() method of the activity). I can confirm that the service’s onCreate() and onStartCommand() methods are being invoked prior to broadcasting the intent in my Application object, but it simply doesn’t seem to get the intent and do the associated work.

Any help would be appreciated.

Update:

Here is how I send the broadcast from my application (note that I start the service first):

 public class MyApplication extends Application {
        @Override
        public void onCreate() {
            Intent intent = new Intent(getApplicationContext(), MyService.class);
        getApplicationContext().startService(intent);
    getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));

        }

 }

Note that I’m starting the service before I actually do the broadcast. The service DOES NOT receive the broadcast, but if I do a similar call from an activity that I start up later, it does receive.

  • 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-27T14:39:43+00:00Added an answer on May 27, 2026 at 2:39 pm

    Your Application is created before any other component in the process. So in onCreate() there is no Serivce running. And if you send broadcast at the moment when Application’s onCreate() is called, Service has no broadcast receiver registered because it wasn’t even created.

    So, in other words, you service is not receiving broadcast notification because broadcast receiver is not yet registered. And its not yet registered because Service is not yet started. And it can’t be started before Application’s onCreate() returns by design.

    You can directly start service using Context.startService() from Application’s onCreate(). You’ll be able to path all the required data through Intent.


    Updated answer to an updated question:

    When you call:

    getApplicationContext().startService(intent);
    

    You ask Android to start service at some later time. The service is not started right away. It’s only scheduled to start later. In fact it will definitely not start before you return from Application’s onCreate() function. That’s because all Service’s lifecycle callback functions (like onCreate() and onStart()) are called from main UI thread. And Applicaiton’s onCreate is currently blocking main UI thread.

    So when you send broadcast using these lines of code:

    getApplicationContext().startService(intent);
    getApplicationContext().sendBroadcast(new Intent(Constants.ACTION_DO_SOMETHING));
    

    You actually ask to start service at some later time, then immediately after that you send broadcast. And because service didn’t even had chance to start and to register for this broadcast
    notifications, it will not receive anything when it actually gets started.

    You could change your service’s onStart() code to handle the intent and then start service using:

    Intent intent = new Intent(getApplicationContext(), MyService.class);
    intent.setAction(Constants.ACTION_DO_SOMETHING);
    getApplicationContext().startService(intent);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have development server setup running Adobe Coldfusion8 (.war install) on top of Caucho
I have a Delphi Web Server setup and running, publishing Web Services, and I
I'm running wamp on Vista (Apache v2.2.11) and have projects setup such that http://localhost/projectx
I have a following setup: App - Magnolia site running under Tomcat 6.0.16 ISAPI
I have 2 servers setup at the moment. One is a web server running
Our setup as follows: We have a local development server running Ubuntu, with a
I have a very similar setup to the person here: PHP Background Processes i.e
I currently have a service setup that emails a bunch of files. What I
Background Info: I have a program running through a list of url; each URL
I have a surfaceView setup and running, but when I resume it I get

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.