2016-08-17 62 views
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(); 
    } 
} 

回答

-1

創建一個布爾標誌,因爲你的微調監聽器將得到自動調用首次同時創建,因此您的報警代碼會被執行,所以你需要把支票僅在選擇執行報警代碼。

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()); 
     } 
     } 
+0

嘗試更新的代碼 –

+0

感謝後共享prefrences其工作 –

+0

我的快樂和幸福的編碼 –