0
我有一個微調類,其中有三個微調項目,在每個項目的情況下,我設置了重複鬧鐘的時間間隔。問題是我每次打開微調類,應用程序服務自動啓動,但我希望服務只在我每次打開微調器類時更改微調器項目時才啓動。 這裏是微調項目的Java代碼。當我打開微調時,報警服務自動啓動活動
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_setting, container, false);
textView = (TextView) rootView.findViewById(R.id.textView);
spinner = (Spinner) rootView.findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.time_interval, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id){
// String item = parent.getItemAtPosition(pos).toString();
// Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
Object test = parent.getSelectedItem();
switch (pos){
case 0:
break;
case 1:
editor.putInt("intervalInMinutes", 5*60*1000);
break;
case 2 :
editor.putInt("intervalInMinutes", 10*60*1000);
break;
case 3 :
editor.putInt("intervalInMinutes", 15*60*1000);
break;
}
editor.apply();
GpsTrackerAlarmReceiver alarm = new GpsTrackerAlarmReceiver();
alarm.setAlarm(getActivity());
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
// Inflate the layout for this fragment
return rootView;
}
@Override
public void onPause() {
super.onPause();
SharedPreferences prefs = getActivity().getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
prefs.edit().putInt("spinner_indx", spinner.getSelectedItemPosition()).apply();
}
@Override
public void onResume() {
super.onResume();
SharedPreferences prefs = getActivity().getSharedPreferences("prefs_name", Context.MODE_PRIVATE);
int spinnerIndx = prefs.getInt("spinner_indx", 0);
spinner.setSelection(spinnerIndx);
}
@Override
public void onAttach(Activity activity) {
//noinspection deprecation
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
嘗試更新的代碼 –
感謝後共享prefrences其工作 –
我的快樂和幸福的編碼 –