I'm writing two applications that cooperate together one collecting data (using a service) and the other is a custom keyboard getting the data from the service and writing them to whatever text field is selected. All this is working fine my only problem is the service binding; right now the service will be started from any app when they call bind. That is not the behavior i want to have; The data collection app should be the only one starting / stopping the service via an activity, the keyboard will only bind if the service is running. what i need to do that is check if the service is running before calling the bind. is there anyway for me to check the status from the keyboard app ? is there some kind of lock to signal the presence of the service ? all i find right now is methods that require the class name and context from where the service was started and that doesn't work for me.
basically ended with three possibilities hope this helps someone else in the future.
Method 1: (what i went with event though method 2 is better)
public static boolean isServiceRunning(Context queryingContext) {
try {
ActivityManager manager = (ActivityManager) queryingContext.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.myservice".equals(service.service.getClassName()))
return true;
}
} catch (Exception ex) {
Toast.makeText(queryingContext, "Error checking service status", Toast.LENGTH_SHORT).show();
}
return false;
}
Method 2:
like pskink suggested above use bindService
with flag 0. only issue with this is the method will always return true whether it got connected or not, you'll have to find another way to know what happened (you won't receive the service handle if it failed)
Method 3:
suggested by yosriz use SharedPreference
to stop a status flag that the other application will check and know the status of the service.