Reputation: 141
I have two buttons -start and stop service buttons- in the fragment. In the initial state start button is enabled and stop button is disabled. When i click "start button",buttons states are changed. -It means start button is disabled and stop button is enabled. In the service i check gps state. If the gps is disabled, i call stopself method to destroy service. After calling stopself method, I wanna change buttons states in the fragment. But i didn't find any solution. How can i do this?
Upvotes: 1
Views: 135
Reputation: 6140
You can use below option.
In your fragment.
//In your on Create method
IntentFilter updateState = new IntentFilter();
updateState.addAction("UPDATE_BUTTON_STATE");
registerReceiver(updateStatueReceiver, updateState);
private BroadcastReceiver updateStatueReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("UPDATE_BUTTON_STATE")) {
// Here you can update your button state
}
}
};
//unregister Receiver in on Destroy method
unregisterReceiver(updateStatueReceiver);
In your Service Class
You can send broadcast using this code
Intent broadCastIntent = new Intent();
broadCastIntent.setAction("UPDATE_BUTTON_STATE");
sendBroadcast(broadCastIntent);
Upvotes: 1