Last active
December 18, 2015 03:09
-
-
Save mancdevcarl/5716335 to your computer and use it in GitHub Desktop.
Check if service is running using SharedPreferences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//**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