- Forum posts: 1
Dec 24, 2015, 8:59:32 AM via Website
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
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.