服务不会停止在应用程序类

时间:2016-12-10 07:58:25

标签: android android-fragments android-service android-application-class

我正在开发一个翻译应用程序,出于某些原因,我想在Application Class中启动一些服务。 我创建了一个" MyApplication"扩展应用程序的类:

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)); 
}

你看到我用这行代码启动了ClipboardService:

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

由于其他一些原因,我想在我的一个Fragment中停止这项服务,称之为" 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));
                }
            }
        });

你看到我用这行代码停止服务:

getActivity().getApplicationContext().stopService(new Intent(getActivity().getApplicationContext(), ClipboardService.class));

但是当我运行我的程序时,服务不会停止。 我混淆了发生了什么以及为什么我的服务不是STOP.am我在某些方面犯了错误? 最好的问候......

1 个答案:

答案 0 :(得分:0)

更改您的应用程序类,如:

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

你不需要调用getApplicationContext()...应用程序类有一个方法startService()

以这样的片段停止你的服务:

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

同样在清单

中声明服务
 <service android:enabled="true" android:name=".ClipboardService" />
如果调用了else块,则在您的片段中

检查特定条件天气:

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

你需要以这样的片段开始你的服务:

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

并停止服务:

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