-3

I really need help guys , i'm trying to make a toast appear at a specific time with the alarm manager from android . I made some research and i made this code , but i don't know what isn't ok ... I tried everything

public void start() {
        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Date dat = new Date();
        Calendar cal_alarm = Calendar.getInstance();
        Calendar cal_now = Calendar.getInstance();
        cal_now.setTime(dat);
        cal_alarm.setTime(dat);
        cal_alarm.set(Calendar.HOUR_OF_DAY,12);
        cal_alarm.set(Calendar.MINUTE,13);
        cal_alarm.set(Calendar.SECOND,0);
        if(cal_alarm.before(cal_now)){
            cal_alarm.add(Calendar.DATE,1);
        }
        manager.set(AlarmManager.RTC_WAKEUP,cal_alarm.getTimeInMillis(),pendingIntent);
    }

If i use cal_alarm - cal_now in manager.set it fires off after 5 seconds .

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Radu Ionuţ
  • 41
  • 1
  • 2
  • 7

1 Answers1

3

You need to add a Receiver to get the result

In your AndroidManifest.xml add this

<receiver android:name=".AlarmReceiver" />

Create this BroadcastReceiver AlarmReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

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

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

and finally, fix your method start

public void start() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Date dat = new Date();
    Calendar cal_alarm = Calendar.getInstance();
    Calendar cal_now = Calendar.getInstance();
    cal_now.setTime(dat);
    cal_alarm.setTime(dat);
    cal_alarm.set(Calendar.HOUR_OF_DAY,14);
    cal_alarm.set(Calendar.MINUTE,18);
    cal_alarm.set(Calendar.SECOND,0);
    if(cal_alarm.before(cal_now)){
        cal_alarm.add(Calendar.DATE,1);
    }

    Intent myIntent = new Intent(context, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);

    manager.set(AlarmManager.RTC_WAKEUP,cal_alarm.getTimeInMillis(), pendingIntent);
}
Gorio
  • 1,606
  • 2
  • 19
  • 32
  • i had the declaration in manifest and i had the alarmreceiver.java too. But still nothing. – Radu Ionuţ Apr 15 '16 at 17:32
  • In manifest, you don't need to add extension .java check it. Use CTRL + SPACE to help you to select your BroadcastReceiver inside tag – Gorio Apr 15 '16 at 17:44
  • I downloaded a full project of alarm manager and it still doesn't work – Radu Ionuţ Apr 15 '16 at 18:00
  • It's a problem from my android studio emulator ? – Radu Ionuţ Apr 15 '16 at 18:00
  • @Gorio what is `pendingIntent` – TheCoderGuy Feb 15 '19 at 17:02
  • 1
    @Spritzig https://developer.android.com/reference/android/app/PendingIntent A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it... – Gorio Feb 16 '19 at 20:22