我正在开发翻译应用。我想在主要活动中启动一项特殊服务。我的服务类是:
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 -->
我有一个片段,其中包含一个复选框,可以在我的服务有效/无效之间切换。当我&#34; 取消选中&#34; checkBox,我想我的服务将是&#34;停止&#34;但它不会停止。我的片段代码是:
@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));
}
}
});
}
我的问题是: &#34;为什么在我调用 StopService()方法时服务不会停止?&#34; 我应该采取额外措施来停止服务吗?
答案 0 :(得分:0)
关注,这是一个有效的代码。
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;
}
}