Skip to content Skip to sidebar Skip to footer

How To Access A Variable Present In A Service

I want to access a variable present in a service from another service/an activity.... Can anyone give an idea?

Solution 1:

To communicate between two service or activity, you need to use AIDL It is not really difficult to do, and there is a lot of tutorial like this.

Solution 2:

You can make a publicgetter for that variable in your Service class, bind to that service, and access the getter to give you that variable.

Solution 3:

If what you mean is that you want to access the variable after you close and open the app, then you're probably looking for SharedPreferences. Note that this requires a context (an activity or service).

To store:

intdata=5;
SharedPreferencesstorage= getSharedPreferences("storage", Context.MODE_PRIVATE);
SharedPreferences.Editoreditor= storage.edit();
editor.putInt("myInt", data);
editor.commit();

To get:

SharedPreferencesstorage= getSharedPreferences("storage", Context.MODE_PRIVATE);
intdata= storage.getInt("myInt", 0);

Post a Comment for "How To Access A Variable Present In A Service"