0

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...

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
linarcx
  • 116
  • 3
  • 17

1 Answers1

0

change your application class like:

public class MyApplication extends Application {
public void onCreate() {
    super.onCreate();
    Log.i("Logger", "Service Starting");
    startService(new Intent(this, ClipboardService.class));
   }
}

you don't need to call getApplicationContext() ...Application class has a method startService()

Stop your service in fragment like this:

getActivity().stopService(new Intent(...));

Also declare the service in manifest

 <service android:enabled="true" android:name=".ClipboardService" />

in your fragment check the specific condition weather if the else block is called or not:

else{
     PreferenceUtils.setPreferenceValue(getContext(), "clipBoardService", "False");
     getActivity().getApplicationContext().stopService(new Intent(getActivity().getApplicationContext(), ClipboardService.class));
  }

you need to start your service in fragment like this:

getActivity().startService(new Intent(getActivity(),ClipboardService.class));

and stop service:

getActivity().stopService(new Intent(getActivity(),ClipboardService.class));
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • thanks for reply.but no change occured! i have one question.in this line of code: `getActivity().stopService(new Intent(...)); ` i call stopServic method like this: `getActivity().stopService(new Intent(getActivity().getBaseContext(), ClipboardService.class));` is it true? – linarcx Dec 11 '16 at 08:53
  • nothing change.i debug the programm and find a Surprising thing. when i go to myFragment, the service start automatically :( i trace the app from when the application start until the start of fragement lifeCycle(onCreate Method).the service is not running.when i go to fragment, service start automatically. i am confuese...very very confused! – linarcx Dec 11 '16 at 10:44