AlarmManager does not work when the device is rebooted

0

I am making an application that, given a specific time and date, shows a notification on that date and time. Everything works normal but the problem arises when I restart the device, when this happens the notification does not jump as expected, reading the AlarmManager documentation I find that this only works while the phone is not turned off or restarted and after consulting how this is solved I find that it is necessary to create a class that receives the services once the device is restarted (RestartAlarmsReceiver) I implement this in my application but it only works when I reboot the device and login to the app before the time and date for the alarm, if I restart the device and do not enter the app the alarm notification does not jump.

This is what I have:

MainActivity

    Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
    final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    long firstMillis = System.currentTimeMillis(); //first run of alarm is immediate // aranca la palicacion
    int intervalMillis = 1 * 3 * 1000; //3 segundos


    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMillis, pIntent);

MyAlarmReciver

    Intent notificationIntent = new Intent(contexto, MainActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(contexto, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    long[] pattern = new long[]{2000, 1000, 2000};

    NotificationCompat.Builder builder = new NotificationCompat.Builder(contexto);
    builder.setContentIntent(contentIntent)

            .setTicker("")
            .setContentTitle("alarma ")
            .setContentTitle("")
            .setContentText(t)
            .setContentInfo("Info")
            .setLargeIcon(BitmapFactory.decodeResource(contexto.getResources(), R.drawable.ic_launcher_background))
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setAutoCancel(true) //Cuando se pulsa la notificación ésta desaparece
            .setSound(defaultSound)
            .setVibrate(pattern);

    Notification notificacion = new NotificationCompat.BigTextStyle(builder)
            .bigText(t)
            .setBigContentTitle("ejemplo")
            .setSummaryText("Resumen de tareas")
            .build();

    notificationManager = (NotificationManager) contexto.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notificacion);
    
asked by Juan Ar 17.06.2018 в 19:22
source

1 answer

0

You will have to make another Broadcast Receiver for when you reboot the device and declare it in the manifest with permission for the Boot (start):

public class RestartAlarmsReceiver extends BroadcastReceiver {

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

    ... tu lógica para obtener la fecha y hora
}

In the manifest add this:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application ....
<receiver android:name=".RestartAlarmsReceiver"
          android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

</application>
    
answered by 17.06.2018 / 22:27
source