user3855998
user3855998

Reputation: 19

Service gets restarted when app is reopened

My app's Service starts as soon as the app is launched for the first time. Once it gets started, I don't want app to restart it when it is reopened. How can I achieve that?

Upvotes: 0

Views: 51

Answers (2)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

Read SharedPreferences once your Activity starts and check if you still need the Service

SharedPreferences prefs = getSharedPreferences("prefsName", MODE_PRIVATE);
boolean doINeedService = prefs.getBoolean("doINeedService", true);
if(doINeedService){
   startService(...);
}

Once the service is done, put a boolean into SharedPreferences like this

SharedPreferences prefs = getSharedPreferences("prefsName", MODE_PRIVATE);
Editor edit = prefs.edit();
edit.putBoolean("doINeedService", false);
edit.commit();

Upvotes: 4

Daniel Tung
Daniel Tung

Reputation: 427

Test to see if the service is already running. I expect somewhere in the Android SDK you can interrogate the running services and see if it's already running.

Someone else will likely have a better idea though, sorry.

Upvotes: 1

Related Questions