Reputation: 19
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
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
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