我有一个微调器类,其中有三个微调器项,在每个项目的情况下我设置一个重复alarm的时间间隔。问题是每次我打开微调器类,应用内服务自动启动但我想要服务启动只有当我打开微调器类时,我才更改微调器项目。 这是spinner项目的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();
}
}
答案 0 :(得分:-1)
创建一个bool标志,因为你的微调器监听器会在创建时自动被第一次调用,因此你的警报代码将被执行,所以你需要检查一下只在选择时执行警报代码。
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
int previousitm =sharedPreferences.getInt("previousItemselected",0);
boolean flag =false;
switch (pos){
case 0:
break;
case 1:
if(previousitm!=1)
flag=true;
editor.putInt("intervalInMinutes", 5*60*1000);
editor.putInt("previousItemselected",1);
break;
case 2 :
if(previousitm!=2)
flag=true;
editor.putInt("intervalInMinutes", 10*60*1000);
editor.putInt("previousItemselected",2);
break;
case 3 :
if(previousitm!=3)
flag=true;
editor.putInt("intervalInMinutes", 15*60*1000);
editor.putInt("previousItemselected",3);
break;
}
editor.apply();
if(flag){
GpsTrackerAlarmReceiver alarm = new GpsTrackerAlarmReceiver();
alarm.setAlarm(getActivity());
}
}