Issue with Alarm wakeup after phone restart and switch off and on!

  • Replies:3
keval
  • Forum posts: 1

Dec 24, 2015, 8:59:32 AM via Website

Hello Guys,

We are developing one application which deal with AlarmManager class. We schedule alarm and app wake up at specific time and do some stuff.

It works fine when there is no restart or no switch on/off for mobile. but all alarm get cleared when we restart or turn off and on the phone this is because the AlarmManager class it self says.

Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

To solve this issue we have implemented custom logic which is below one:

When restart or On and off occurs, at the boot complete event we fetching alarm from database and register/set them back so they can run as scheduled.

we already give below two permissions:

QUICKBOOT_POWERON
BOOT_COMPLETED

Now the problem is the custom wakeup works well with many phones Samsung, Lenova, Intex and some other phones but some how the process that set alarm back to AlarmManager after boot completion execute some time and some time it do not.

It having issue on ASUS and Samsung Galaxy S4, LG G3 and Doogee phone!

Here is the code we using :

java code set alarm

private void setAlarm(Calendar targetCal,int id)
{

      //uf.Show_Alert_Dialog(Screen_Add_Flight.this,"\n\n***\n"+ "Alarm is set@ " + targetCal.getTime() + "\n"+ "***\n",R.drawable.ic_launcher);

      Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
      //final int _id = (int) System.currentTimeMillis();
      final int _id = id;
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), _id, intent, PendingIntent.FLAG_ONE_SHOT);
      AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
      if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT)         
          alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
      else
          alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
      //alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

}

Receiver code

package com.passgrabber.checkinapp;

import java.util.Calendar;
import java.util.TimeZone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

@SuppressWarnings("unused")
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent action)
{
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();

    Intent i = new Intent();
    i.setClassName("com.example.checkinapp", "com.example.checkinapp.Screen_Webview");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}

}

Android mainfest file

<receiver android:name=".AlarmReceiver" android:process=":remote" android:exported="true" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

So any clue to fix this issue or is there something wrong with code ? Some thought or help will be really helpful!

Thanks in advance!!

— modified on Dec 24, 2015, 9:04:43 AM

Reply
Deactivated Account
  • Forum posts: 263

Dec 24, 2015, 3:13:33 PM via Website

I am still learning it so I think I might not be able to help you, but there are many other experts here, so be patient.

Looking for security app? Try Leo Privacy Guard 3.0.

Join Our Christmas Contest by visiting our Facebook page LEO Privacy Guard

Reply
Android Dev
  • Forum posts: 2

May 24, 2018, 7:59:58 AM via Website

Hi , Did u get any solution for this?. I am also facing same problem

Reply
Sailesh Sirari
  • Forum posts: 13

May 25, 2018, 8:25:05 AM via Website

Not sure just a thought , you said alarm is not registered in some devices after reboot , did you meant you didn't received boot complete event on some devices? And that too sometimes (may be some race condition in system).
You can debug further , if this issue is happening in new Os versions , there were some changes to conserver battery life in new versions like doze mode, background service limits etc . If you don't need running at exact time consider other designs like Job schedulers.

Reply