私はトランスレータアプリケーションを開発していますが、何らかの理由でアプリケーションクラスでサービスを開始したいと考えています。 iは拡張し、「MyApplicationを」クラスを作成したアプリケーション:サービスはアプリケーションクラスで始まるフラグメントでは停止しません
getApplicationContext().startService(new Intent(getApplicationContext(), ClipboardService.class));
や他のいくつかの理由のために私は、このサービスを停止する:あなたは、私はこのコード行でClipboardServiceを開始していることがわかり
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));
}
あなたは、私がこのコード行でサービスを停止していることがわかり
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));
}
}
});
:
私の断片の一つは、「SettingFragment」と呼びましたgetActivity().getApplicationContext().stopService(new Intent(getActivity().getApplicationContext(), ClipboardService.class));
私のプログラムを実行すると、サービスは停止しません。 何が起こるのか、なぜ私のサービスが停止しないのか混乱しています。私はいくつかの点で間違いを犯しますか? よろしく...
返信ありがとうございます。変更はありませんでした。私は1つの質問があります。このコード行では、次のようになります。 'getActivity()。stopService(new Intent(...)); ' ' getActivity()。stopService(新しいインテント(getActivity()。getBaseContext()、ClipboardService.class)); ' これは本当ですか? –
が動作すれば... plsは答えを受け入れます – rafsanahmad007
何もchange.iプログラムをデバッグし、驚くべきことを見つけます。 私はmyFragmentに行くと、自動的にサービスが開始されます:( アプリケーションが開始されてからアプリケーションが開始するまでの間、アプリケーションが開始されるまでのライフサイクル(onCreateメソッド)が実行されていません。 私はconfueseです...非常に非常に混乱している! –