9

Possible Duplicate:
Can I startService from Application#onCreate()?

Can we start a service from the application class?

I want to start a service when my application starts. So instead of starting the service from the starting activity, can I use my application class for this? Thanks.

What I tried is Inside application class :

public void onCreate() {
    super.onCreate();


    Log.i("Logger", "Service Starting");
    Intent errorLoggerService = new Intent(getApplicationContext(),
            ErrorLoggerService.class);
    getApplicationContext().startService(errorLoggerService);
}
Community
  • 1
  • 1
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • It is not mandatory to start it from oncreate. What i need is to start the service from Application class to make sure the service starts when my application starts. – Eldhose M Babu Sep 12 '12 at 06:24

2 Answers2

29

Yes, you could do this before API Label 26 but not anymore.

If your API version is less than 26

public class MyApplication extends Application {
    public void onCreate() {
        super.onCreate();
        startService(new Intent(this, MyService.class));
    }
}

EDIT Don't call getApplicationContext(), just call startService(). Also, make sure you've declared the service in your manifest.

For API label 26 and above

EDIT 2 From official Google Docs: https://developer.android.com/guide/components/services.html#StartingAService

Note: If your app targets API level 26 or higher, the system imposes restrictions on using or creating background services unless the app itself is in the foreground. If an app needs to create a foreground service, the app should call startForegroundService(). That method creates a background service, but the method signals to the system that the service will promote itself to the foreground. Once the service has been created, the service must call its startForeground() method within five seconds.

So, these days, you can't start a background service from Application. Only GUI components are able to start background service.

Or, you can start a foreground service from background components like Application.

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
-3

Yes, You can use Intent to start your service,

Intent serviceIntent = new Intent();
serviceIntent.setAction("packagename.serviceName");
context.startService(serviceIntent);    
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • OP not asking how to start a `service`? query is that `Can we start a service from the application class?`. – Mohammed Azharuddin Shaikh Sep 12 '12 at 06:04
  • @hotveryspicy, my code answer the question `Can we start a service from the application class?` – Lucifer Sep 12 '12 at 06:11
  • No, You mentioned for `how we can start a service` but the question still unanswered, Answer may be in either yes/no. – Mohammed Azharuddin Shaikh Sep 12 '12 at 06:13
  • @hotveryspicy, ok i forgot to add "Yes" in the starting of my answer, thanks for mentioning it, i already added it to my answer :) – Lucifer Sep 12 '12 at 06:16
  • @Lucifer, I know how to start a service. But what i need is to start the service from Application class so that I can ensure the service will be started when my application starts. When I try to start from onCreate of Application class, the service was not working. So in my application class from where should I start the service or is it possible? – Eldhose M Babu Sep 12 '12 at 06:22
  • you mean a normal .java class ? though i haven't use but I am sure it works. – Lucifer Sep 12 '12 at 06:24
  • 4
    @Lucifer I thought(high reputed) you must be knowing `Application class` in Android but Irony, you don't (`you mean a normal .java class ?`). Its not an answer if you dnt know. – Mohammed Azharuddin Shaikh Sep 12 '12 at 06:27
  • 1
    @Lucifer he is asking you if you can start a Service in here: http://developer.android.com/reference/android/app/Application.html. You don't know what you are talking about. :P – mishmash Sep 12 '12 at 06:28
  • @hotveryspicy, Basically I am new to Android, and in Java, Application class is .java that's why i thought like that. Ok i saw the above link, I have never worked with Application Class still – Lucifer Sep 12 '12 at 06:29
  • 3
    If you are new then you must not be confident in saying `YES`. It is .Java but its extends `Application`. – Mohammed Azharuddin Shaikh Sep 12 '12 at 06:30
  • No a .java file is a file that contains Java code (which is in a class). The entire project would be the application. The class mentioned here is the Application class. – mishmash Sep 12 '12 at 06:30
  • 2
    No idea why this answer is being downvoted. @hotveryspicy is being shallow and pendantic. The question was: can I start a Service from my Application class. And Lucifer answered yes and showed how. Pretty straightforward. – LuxuryMode Sep 12 '12 at 06:36
  • 1
    @LuxuryMode Lucifer said in above comment `Basically I am new to Android, and in Java, Application class is .java that's why i thought like that. Ok i saw the above link, I have never worked with Application Class still`, that makes me uncomfortable. – Mohammed Azharuddin Shaikh Sep 12 '12 at 06:39
  • @Lucifer for reason of down-vote see my comment [here](http://stackoverflow.com/questions/12381977/starting-a-service-from-application-class/12382423#comment16634847_12382423) – Lalit Poptani Sep 12 '12 at 07:06