服务不要停止碎片



我正在开发 Translator App

public class ClipboardService extends Service {
    private ClipboardManager CM;
    private DataBaseHelper dbHelper;
    private String englishClip = "";
    private String persianClip = "";
    private String currentDate;
    private List<String> item;
    private Handler handler;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        handler = new Handler();
        dbHelper = new DataBaseHelper(getApplicationContext());
        try {
            dbHelper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //super.onStartCommand(intent, flags, startId);
        handler.post(updateStatus);
        CM = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        CM.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                englishClip = CM.getText().toString();
                persianClip = dbHelper.showData(englishClip);
                currentDate = DateConverter.toShamsiDate(DateConverter.getCurrentDateTime(), DateTimePickerType.DateHorMin);
                Toast.makeText(getApplicationContext(), persianClip, Toast.LENGTH_LONG).show();
                Integer ID_Dic = dbHelper.getRowID(englishClip, "ID");
                if (ID_Dic != 0)
                    dbHelper.insertUserWords(ID_Dic, currentDate, 0, "", 1);
                item = new ArrayList<>();
                item.add(englishClip);
                item.add(persianClip);
                item.add(currentDate);
                item.add("1");
                WordListFragment.recyclerAdapter.addItem(item);
            }
        });
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        stopSelf();
        super.onDestroy();
        handler.removeCallbacks(updateStatus);
        handler = null;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        handler.removeCallbacks(updateStatus);
        handler = null;
        return super.onUnbind(intent);
    }
    private Runnable updateStatus = new Runnable() {
        @Override
        public void run() {
            // do something here
            handler.postDelayed(updateStatus, 1000);
        }
    };
}

在清单文件中,我还声明了服务:

<!-- ClipBoard Serivce -->
        <service
            android:name=".service.ClipboardService"
            android:enabled="true">
        </service>
<!-- /ClipBoard Serivce -->

我有一个片段,它具有一个复选框,可以在活动/不活动之间切换。停止。在这里是我的片段代码:

    @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);
        // 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().startService(new Intent(getActivity(), ClipboardService.class));
                }
                else{
                    PreferenceUtils.setPreferenceValue(getContext(), "clipBoardService", "False");
                    getActivity().stopService(new Intent(getActivity(), ClipboardService.class));
                }
            }
        });
    }

我的问题是:"为什么当我调用 stopService()方法时,服务不会停止?"我可以采取额外的措施来停止服务吗?

跟随,这是一个工作代码。

 public class SampleFragment extends Fragment{
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            final View view = inflater.inflate(R.layout.sample_fragment,container,false);
            final CheckBox checkBox = (CheckBox) view.findViewById(R.id.chkbox);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (checkBox.isChecked()) {
                        getActivity().startService(new Intent(getActivity(), MyService.class));
                    } else {
                        getActivity().stopService(new Intent(getActivity(), MyService.class));
                    }
                }
            });
            return view;
        }
    }

最新更新