0

我試圖讓用戶選擇報警的時間,但警報會觸發每隔15分鐘「這是默認的」即使我選擇另一種偏好我的應用程序沒有響應的喜好來改變

這是代碼相關的問題,在我的MainActivity

public class MainActivity extends AppCompatActivity { 
private SharedPreferences sharedPreferences; 
private static String reminder; 
private static AlarmManager am; 
private ImageButton btn; 
private ImageButton cancel; 
private EditText et; 
private Intent intent; 
private PendingIntent pend; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 
    // initialize variables 
    btn = (ImageButton) findViewById(R.id.imageButton2); 
    cancel = (ImageButton) findViewById(R.id.imageButton); 
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
    // create intents 
    intent = new Intent(getApplicationContext(), Notifications.class); 
    pend = getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    am = (AlarmManager) getSystemService(ALARM_SERVICE); 
    // get the user preference 
    String sel = sp.getString("repeatPref", "1"); 
    final String repeat[] = getResources().getStringArray(R.array.settings_repeat_by_labels); 
    final int ss = Integer.parseInt(sel); 
    // action when user presses the "Tweak" button 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      pend.cancel(); 
      am.cancel(pend); 
      // initialize pend and AlarmManager 
      pend = getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      am = (AlarmManager) getSystemService(ALARM_SERVICE); 
      // if the user didn't type anything 
      Calendar calendar = Calendar.getInstance(); 
       // notify at different times 
       if (repeat[ss - 1].equals("15 MINUTES")) { 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
         am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 60*1000 ,60*1000, pend); 
        } 
       } else if (repeat[ss - 1].equals("1 HOUR")) { 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
         am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 120 * 1000, 120 * 1000, pend); 
        } 
       } else if (repeat[ss - 1].equals("2 HOURS")) { 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
         am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + AlarmManager.INTERVAL_HOUR * 2, AlarmManager.INTERVAL_HOUR * 2, pend); 
        } 

    }); 

另一個按鈕取消報警

cancel.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
       am.cancel(pend); 
       pend.cancel(); 
       Toast.makeText(MainActivity.this,"All cleared", Toast.LENGTH_LONG).show(); 
      } 

     }); 

我創建的掛起和鬧鐘在onCreate方法和第二次Ti我當按下「BTN」因爲我創造另一個按鈕來取消它,我試圖「時間長」來檢查的PendingIntent「掛起」和AlarmManager「AM」已經存在或沒有,然後根據結果取消它,但我失敗了,所以我認爲這種做法可能會奏效

現在每次用戶想要取消報警,並打開該應用程序會創建新的未決意圖和警報管理,所以如果用戶按下取消按鈕而不按「BTN」第一該應用程序不會崩潰,已經工作的警報被取消..我懷疑這是我現在的問題的一部分

我知道這是很多的東西,但我試着解釋儘可能簡單..感謝您花時間閱讀所有這些!

回答

0

我的應用程序沒有響應我的變化,因爲我是reteieving的共享偏好之前,我點擊「BTN」 ..它得到固定後,我搬到共享偏好部分OnClick方法。

相關問題