Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getContext on a background Service

I'm trying to create a Service that runs even when my app is closed. However, I need to use my app Context inside this Service. When the app is running, the service works as well, but when I close the app (onDestroy() was called), the getContext() always returns null.

Service

public class SubscribeService extends Service {

    private Context context;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this; //Returns null when service is running on background
        context = MyApp.getContext(); //Also null
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //do stuff using context
    }

MyApp

public class MyApp extends Application {

    private static Context context;

    public static Context getContext() {
        return context.getApplicationContext();
    }

    @Override
    public void onCreate() {
        context = getApplicationContext();
        super.onCreate();
    }
}

Service start from Activity onCreate()

startService(new Intent(this, SubscribeService.class));

How should I use the Context in this scenario?

Edit

Managed to get it to work properly after Onik's help. I just had to call the MyApp.getContext(); before super.onCreate(); Like so:

@Override
public void onCreate() {
    context = MyApp.getContext();
    super.onCreate();
}
like image 846
urukh Avatar asked Sep 18 '18 20:09

urukh


People also ask

What is the use of getContext in Android?

getContext():It returns the Context which is linked to the Activity from which it is called. This is useful when we want to call the Context from only the current running activity.

What is the difference between getActivity and getContext?

getActivity() is at least a method on Fragment, to get the activity it is attached to. Whenever a context is needed in an instance method of an activity, you can use this . A context is needed whenever contextual info is needed, or when stuff needs to be displayed.

What is the difference between getContext and getApplicationContext?

getContext() : Returns the context the view is currently running in. Usually the currently active Activity. Activity. getApplicationContext() : Returns the context for the entire application (the process all the Activities are running inside of).


3 Answers

Service extends Context. You can use this, where this is the reference to the Service instance.

Putting more details on my comment below regarding the following code of SubscribeService class:

@Override
public void onCreate() {
    super.onCreate();
    context = this;
    context = MyApp.getContext();
}

In your Service's onCreate() context = this cannot be null by a fundamental programming paradigm.

like image 114
Onik Avatar answered Oct 22 '22 04:10

Onik


Try this:

Added super.onCreate(); before MyApp.context = getApplicationContext();

public class MyApp extends Application {

    private static Context context;

    public void onCreate() {
        super.onCreate();
        MyApp.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApp.context;
    }
}

Edit: Calling MyApp.getAppContext() will return the application Context.

like image 34
ʍѳђઽ૯ท Avatar answered Oct 22 '22 04:10

ʍѳђઽ૯ท


already once left an answer, which was to use getApplicationContext() in the Service.

also, using an IntentService with Context.startService(Intent) might make sense here.

... and do not insert statements before calling to super.onCreate().

like image 35
Martin Zeitler Avatar answered Oct 22 '22 04:10

Martin Zeitler