0

I'm trying to make an alarm app and so far it works when the app is running on foreground and background. But if I close the app (swipe on running apps) the alarm doesn't go off.

This is my code

Manifest.xml

<receiver
    android:name=".AlarmReceiver">
    <intent-filter>
        <action android:name="es.monlau.smartschool.AlarmReceiver"/>
    </intent-filter>
</receiver>

MyActivity.java:

Intent intentAlarm = new Intent(this, AlarmReceiver.class);
intentAlarm.setAction("es.monlau.smartschool.AlarmReceiver");
AlarmManager alarmManager = (AlarmManager)this.getApplicationContext()
    .getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentAlarm, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

AlarmReceiver.java:

@Override
public void onReceive(Context context, Intent intent){
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = pm.isScreenOn();
    if (!isScreenOn) {
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock");
        wl.acquire(10000);
        PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock");

        wl_cpu.acquire(10000);
    }

    sendNotification(context, title, descr, id);
}
private void sendNotification(Context context,String messageTitle, String messageBody, int id) {
    Intent intent = new Intent(context, Alarma.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, id,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    boolean vibrar = settings.getBoolean("pref_notifyVibrar", true);
    boolean sonido = settings.getBoolean("pref_notifySonido", true);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(context, "Alarma")
                    .setSmallIcon(R.drawable.logo_app_monlau)
                    .setContentTitle(messageTitle)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setFullScreenIntent(pendingIntent,true)
                    .setLights(Color.BLUE,1,1);

    if (sonido){
        Uri alarmUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        mRingtone = RingtoneManager.getRingtone(context, alarmUri);
        mRingtone.play();
    }

    if (vibrar){
        long[] pattern = {0, 1000, 0, 1000, 0};
        notificationBuilder.setVibrate(pattern);
    }

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager != null) {
        notificationManager.notify(id, notificationBuilder.build());
    }
}

I know this is a very common problem and I've looked all the related posts.

I've triend using a Service, initiated by a another broadcast receiver with the action BOOT_COMPLETED but everything just stopped working. I don't really know what to change.

Yluna
  • 103
  • 2
  • 15
  • on which OS version you test this code above? – Shalan93 Jul 16 '18 at 09:26
  • Android 5.1.1 - API 22 – Yluna Jul 16 '18 at 09:51
  • you can make broadcast receiver to start a service that will trigger the notification you want when app is closed check this for clearness https://stackoverflow.com/questions/39979703/alarmmanager-is-not-triggered-when-app-is-closed – Shalan93 Jul 16 '18 at 09:59
  • I've added the service and I call it from the broadcast receiver, but it doesn't seem to work. I had a null pointer error with intent.getAction() because I didn't set the action on the activity when starting the intent, after I fixed that, nothing. It doesn't look like it's calling onReceive method anymore. – Yluna Jul 16 '18 at 11:17
  • Okey, I got it to work. But i'm in the same situation as before, it only works when the app is on foreground or background. If I close it, the alarm doesn't go off. – Yluna Jul 16 '18 at 11:43
  • it may be a vendor restriction like in xiaomi phones, it has some optimizations for background jobs. so for disabling this feature you will found it under device setting – Shalan93 Jul 16 '18 at 15:09
  • I'm indeed testing the app in a Xiaomi phone. I disabled all the MIUI optimization options but still doesn't work when I close the app. I will try to test on on other device. – Yluna Jul 17 '18 at 08:00
  • 1
    I got it to work allowing the app to start when the phone boots. Thank you so much! I was convinced there was something wrong with my code. – Yluna Jul 17 '18 at 08:22

0 Answers0