简体   繁体   中英

Android : setAlarmManager(…) open an activity instead of delaying the opening

In android, I have to set an alarm at a special hour that will ring and open an activity at the correct time of the day.

My problem is that the function "alarmManager.set(...)" OPEN INSTANTLY the targeted activity and, when the time is reached, open again the same activity.

I don't understand why this happening how can I correct it ?

Current State : I have a timepicker which help me to select the time. I also have a button which call the function alarmManager.set() when I click on it by retrieving the time from the timepicker and calculate the next alarm in ms.

Thx

Code of the button :

public void setTheAlarm(View view) {

    TimePicker tp_clock = (TimePicker) findViewById(R.id.tp_timeToRing);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int alarmHours = tp_clock.getCurrentHour();
    int alarmMinutes = tp_clock.getCurrentMinute();

    Toast.makeText(this, "Alarm set for " + alarmHours + "h" + alarmMinutes, Toast.LENGTH_LONG).show();

    Long time = calculateTime(alarmHours, alarmMinutes);
    alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}

Code of the function calculateTime :

public Long calculateTime(int alarmHours, int alarmMinutes) {

    Calendar c = Calendar.getInstance();
    int currentHours = c.get(Calendar.HOUR);
    if (c.get(Calendar.AM_PM) == 1)
        currentHours +=12;
    int currentMinutes = c.get(Calendar.MINUTE);
    int setAlarmHours = 0;
    int setAlarmMinutes = 0;
    int balanceWithMinute = 0;

    if (alarmMinutes >= currentMinutes) {

        setAlarmMinutes = alarmMinutes - currentMinutes;
    }
    else {

        setAlarmMinutes = (60 - currentMinutes) + alarmMinutes;
        balanceWithMinute = 1;
    }
    if (alarmHours > currentHours)
        setAlarmHours = alarmHours - currentHours;

    if (alarmHours < currentHours)
        setAlarmHours = (24 - currentHours) +alarmHours - balanceWithMinute;

    if (alarmHours == currentHours && balanceWithMinute == 1)
        setAlarmHours = 23;

    Long time = new GregorianCalendar().getTimeInMillis()+setAlarmHours*setAlarmMinutes*60*1000;
    return time;
}

Code of the alarmManager :

package fr.pixelcraft.victor.wackertest.SoundPackage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import fr.pixelcraft.victor.wackertest.LoginActivity;

public class AlarmReciever extends BroadcastReceiver {

    private String userToken;
    private String userId;

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent i=new Intent(context, PlanASoundActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        userToken = intent.getStringExtra(LoginActivity.USER_TOKEN);
        userId = intent.getStringExtra(LoginActivity.USER_ID);

        i.putExtra(LoginActivity.USER_TOKEN, userToken);
        i.putExtra(LoginActivity.USER_ID, userId);
        context.startActivity(i);

        Toast.makeText(context, "Alarm triggered", Toast.LENGTH_LONG).show();
    }
}

By https://stackoverflow.com/users/2649012/frank-n-stein :

Probably you set a time which is already past. This makes the AlarmManager fire immediately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM