Skip to content

Instantly share code, notes, and snippets.

@mancdevcarl
Last active December 18, 2015 03:09
Show Gist options
  • Save mancdevcarl/5716335 to your computer and use it in GitHub Desktop.
Save mancdevcarl/5716335 to your computer and use it in GitHub Desktop.
Check if service is running using SharedPreferences
//**in the service (ServiceX)
public static boolean isRunning(Context ctx) {
sharedPref = ctx.getSharedPreferences("my_pref", MODE_PRIVATE);
return sharedPref.getBoolean("running", false);
}
private void setRunning(boolean running) {
sharedPref = getSharedPreferences("my_pref", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putBoolean("running", running);
prefEditor.commit();
}
//in the onCreate()
setRunning(this);
//in the onDestory()
setRunning(false);
//**from activity
if (ServiceX.isRunning(this)) {
Log.d("", "Running");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment