i'm developing a translator app and for some reasons i want to initiate some service in Application Class. i create a "MyApplication" class that extends Application:
public class MyApplication extends Application{
@Override
public void onCreate() {
if(PreferenceUtils.getPreferenceValue(getApplicationContext(),"clipBoardService").equals(""))
PreferenceUtils.setPreferenceValue(getApplicationContext(),"clipBoardService","True");
//Start ClipBoard Services
if(PreferenceUtils.getPreferenceValue(getApplicationContext(),"clipBoardService").equals("True"))
getApplicationContext().startService(new Intent(getApplicationContext(), ClipboardService.class));
}
you see that i start ClipboardService with this line of code:
getApplicationContext().startService(new Intent(getApplicationContext(), ClipboardService.class));
and for some other reasons i want to stop this service in one of my Fragment, called it "SettingFragment":
public class SettingFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_setting, container, false);
SwitchCompat scClipBoard = (SwitchCompat) inflate.findViewById(R.id.sb_ClipBoard_Service);
SwitchCompat scPopUp = (SwitchCompat) inflate.findViewById(R.id.sb_PopUp_Service);
// Turn On CheckBox's
if(PreferenceUtils.getPreferenceValue(getContext(),"clipBoardService").equals("True"))
scClipBoard.setChecked(true);
if(PreferenceUtils.getPreferenceValue(getContext(),"popUpService").equals("True"))
scPopUp.setChecked(true);
// handle OnCheckListener of CheckBox's
scClipBoard.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
PreferenceUtils.setPreferenceValue(getContext(), "clipBoardService", "True");
getActivity().getApplicationContext().startService(new Intent(getActivity().getApplicationContext(), ClipboardService.class));
}
else{
PreferenceUtils.setPreferenceValue(getContext(), "clipBoardService", "False");
getActivity().getApplicationContext().stopService(new Intent(getActivity().getApplicationContext(), ClipboardService.class));
}
}
});
you see that i stop service with this line of code:
getActivity().getApplicationContext().stopService(new Intent(getActivity().getApplicationContext(), ClipboardService.class));
but when i run my programm the service don't stop. i'm confuse what happen and why my service dont be STOP.am i make a mistake in some points? best regards...