Domanda

Ho impostato una schermata delle preferenze in cui ho un elenco di preferenze che consente all'utente di selezionare l'intervallo di tempo per avvisarlo.

Possono scegliere se avvisarli ogni 2,4,8,10 o 12 ore.

Ecco la mia preferenza per l'elenco:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
            android:title="Notification Timer"
            android:summary="Select when to Notify"
            android:dialogTitle="Show Notification after every:"
            android:positiveButtonText="OK"
            android:negativeButtonText="Cancel"
            android:entries="@array/entries"
            android:entryValues="@array/entries"
            android:key="listPrefs" />
</PreferenceScreen>

Ecco il mio file Settings.class:

public class Settings extends PreferenceActivity implements OnPreferenceChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        ListPreference listPreference = (ListPreference) findPreference("listPrefs");
        listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {

                return false;
            }
        });
        @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // TODO Auto-generated method stub
        return false;
    }
}

voglio aggiornare o ripristinare gli allarmi ogni volta che l'utente modifica l'intervallo di tempo di notifica.ad esempio: se l'utente seleziona 4 ore, dovrà essere avvisato dopo 4 ore o se l'utente sceglie 10 ore, dovrà essere avvisato dopo 10 ore!

È stato utile?

Soluzione

Segui questi passi:

  1. Copia i seguenti metodi sul tuo file Impostazione.java file:

    private final int NOTIFICATION_TIMER = 11;
    
    public void setAlarm(Context mContext,int requestCode,long time) {
    
        Intent myIntent = new Intent(mContext, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requestCode, myIntent,0);
    
        cancelAlarmIfExists(mContext,requestCode,myIntent);
    
        AlarmManager alarmManager = (AlarmManager)mContext.getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis()+time), time, pendingIntent);
    }
    
    public void cancelAlarmIfExists(Context mContext,int requestCode,Intent intent){
        try {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requestCode, intent,0);
            AlarmManager am=(AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
            am.cancel(pendingIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
  2. Chiamali dal onPreferenceChange() metodo.Così:

    listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
    
            long time = 34352355253; // calculate millisecons from change value by user.
            setAlarm(Settings.this,NOTIFICATION_TIMER,time);
            return false;
        }
    });
    

Altri suggerimenti

flag_cancel_current ha lo stesso effetto della risposta sopra

PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top