Testing BroadcastReceiver with AlarmManager - java

I'm trying to test launching alarm with AlarmManager and accepting it with BroadcastReceiver. I've found this sources, and do everything like there. But it works so-so and fires from time to time (like 1 successful test from 20 attempts):
#Test public void testOnReceive_defaultAlarm_accepted() throws Exception {
final SharedPreferences sharedPrefs = mQuestions.getAppComponent().getSharedPrefs();
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis() + TIME_DELAY_SNOOZE); // time for alarm
sharedPrefs.edit()
.putString(mQuestions.getString(R.string.pref_notification_time_key),
TimePreference.timeToString(calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE)))
.apply(); // set test prefs to alarm
QuestionNotificationUtils.launchDefaultAlarm(mQuestions, sharedPrefs); // launch alarm
Thread.sleep(TIME_DELAY_SNOOZE + TIME_DELAY); // wait for alarm to be fired
assertNotNull(mQuestionNotificationReceiver.mRealmWrapper); // check if injected, and HERE IT FAILS
verify(mQuestionNotificationReceiver.mRealmWrapper, times(1)).getQuestions(); // check if fired with Mockito
}
Launch default alarm is wrapper around this func:
private static void launchAlarm(Context ctx, SharedPreferences prefs, int id) {
final AlarmManager alarmMgr = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
final PendingIntent alarmIntent =
PendingIntent.getBroadcast(ctx, id, QuestionNotificationReceiver.getIntent(ctx, id),
PendingIntent.FLAG_UPDATE_CURRENT);
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
final String time = prefs.getString(ctx.getString(R.string.pref_notification_time_key),
ctx.getString(R.string.questions_preference_fragment_default_notification_time));
calendar.set(Calendar.HOUR_OF_DAY, TimePreference.parseHour(time));
calendar.set(Calendar.MINUTE, TimePreference.parseMinute(time));
alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
}
Parts of the broadcast receiver:
#Inject RealmWrapper mRealmWrapper;
#Override public void onReceive(Context context, Intent intent) {
((Questions) context.getApplicationContext()).getQuestionNotificationReceiverComponent()
.inject(this);
onPostInjectReceive(context, intent);
}
Also here is #Before method:
#Before public void setUp() throws Exception {
mQuestionNotificationReceiver = new QuestionNotificationReceiver();
mQuestions.registerReceiver(mQuestionNotificationReceiver,
new IntentFilter(QuestionNotificationReceiver.getAction(mQuestions)));
}
Sometimes it works, it shows, that code is almost right, but it's very very flaky. If I start to use custom RetryRule to retry test if it's failed, test becomes very long, since Thread.sleep() takes about 10 seconds. How to manage this situation?

Use either Calendar.HOUR or Calendar.HOUR_OF_DAY, not both

Related

Android setAlarmClock triggering immediately

I created a simple application to try out setAlarmClock(). There is only a single button in the MainActivity, and it calls setAlarm when clicked.
The code is below.
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "alarmclock.MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setAlarm(View view) {
Context context = getApplicationContext();
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
long nextTrigger = SystemClock.elapsedRealtime()+10*1000;
Log.i(TAG, SystemClock.elapsedRealtime() + ": scheduling next alarm at " + nextTrigger);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager.AlarmClockInfo ac = new AlarmManager.AlarmClockInfo(nextTrigger, null);
alarmManager.setAlarmClock(ac, pendingIntent);
}
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "alarmclock.AlarmReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, SystemClock.elapsedRealtime() + " in AlarmReceiver onReceive()");
}
}
I am basically triggering the alarm for 10seconds later, but for some reason it's triggering immediately.
2020-09-21 22:09:56.664 17910-17910/com.example.alarmclock I/alarmclock.MainActivity: 3362358: scheduling next alarm at 3372358
2020-09-21 22:09:56.687 17910-17910/com.example.alarmclock I/alarmclock.AlarmReceiver: 3362382 in AlarmReceiver onReceive()
If I use setExactAndAllowWhileIdle the same code seems to work fine. But I am specifically wanting to try out setAlarmClock.
The AlarmClockInfo requires a wall time:
time at which the underlying alarm is triggered in wall time milliseconds since the epoch
but you are using elapsedRealtime() time so use System.currentTimeMillis to fix the issue.
It worked after I changed from SystemClock.elapsedRealtime() to System.currentTimeMillis().
long nextTrigger = System.currentTimeMillis()+10*1000
After reading the documentation in greater detail, I realise that the first parameter of the AlarmClockInfo constructor takes
long: time at which the underlying alarm is triggered in wall time
milliseconds since the epoch
which is what System.currentTimeMillis() gives.
On the other hand, SystemClock.elapsedRealtime() returns the
time since the system was booted...
which will always be a much smaller number than wall time since epoch. As a result, the alarm gets scheduled immediately because
If the stated trigger time is in the past, the alarm will be triggered immediately.

Alarm successfully sets for future time, but not for Calendar date

I have this app where i want to set a notification on a specific date the user defines. I saw lots of code and relative answers here on SO but for some reason nothing works so far.
In this test project i have 2 methods. One that sets an alarm to ring 5 seconds on future and one that assigns the desired date.
Problem:
If i set the alarm to a second delay it works fine. The notification is shown within 5 seconds after the alarm assignment. But if i use the method that passes a Calendar date, it does nothing. Not triggers no notification
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setAlarmOnClick(View view){
// this method call works fine
setAlarm(getNotification("Date test"), 3000);
// this one doesn't
// setAlarm(getNotification("Date test"), getDate());
}
// Here nothing happens when the date comes
private void setAlarm(Notification notification, Calendar cal) {
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
Toast.makeText(this, "Alarm worked.", Toast.LENGTH_LONG).show();
}
// This alarm get successfully trigger here showing the notification
private void setAlarm(Notification notification, int delay) {
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pendingIntent );
Toast.makeText(this, "Alarm worked.", Toast.LENGTH_LONG).show();
}
private Calendar getDate(){
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,8);
cal.set(Calendar.YEAR,2017);
cal.set(Calendar.DAY_OF_MONTH,29);
cal.set(Calendar.HOUR_OF_DAY,11);
cal.set(Calendar.MINUTE,47);
return cal;
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Scheduled Notification");
builder.setContentText(content);
builder.setSmallIcon(R.mipmap.ic_launcher);
return builder.build();
}
}
NotificationPublisher.java
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notofManager.notify(id, notification);
}
}
I would really appreciate anykind of help!
P.S.
How can i be sure that an alarm was successfully set on a date. Even if a code works and a date is set a week form now. How can i test that it will trigger then? Obviously we can't wait a week to see that. Changing the device date is working? Thank you
Calendar month goes from 0-11 instead of 1-12 as u assumed.
So something like:
cal.set(Calendar.Month, 7 );
would set your calendar month to August.

How to give background notifications in android on exacly each hour everyday(eg.7:00am,8:00am,9:00am)

I am doing a time table project for my college, in which I want to generate notification on each hour using AlarmManager, I almost got the solution, but the alarm gets triggered during random time, like between hours. What I want is to trigger alarm manger messages on exactly each hour, like 7.00am,8.00am, and so on on everyday. My code is given below.
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper2 db = new DatabaseHelper2(this);
private PendingIntent pendingIntent;
private AlarmManager alarmManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Cursor resService = db.getAllData();
int pref = 0;
if(resService.getCount()!=0){
Date date2 = new Date();
int h=date2.getHours();
int m = date2.getMinutes();
int s = date2.getSeconds();
Intent alarmIntent = new Intent(this,AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0,alarmIntent,0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//int interval = 60 * 60 * 10000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),AlarmManager.INTERVAL_HOUR,pendingIntent);
Toast.makeText(this,"Alarm set at "+h+" : "+m+" : "+s,Toast.LENGTH_LONG).show();
}
}
AlarmReciever.java
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Date d = new Date();
Toast.makeText(context,"Broadcast message triggered at "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds() ,Toast.LENGTH_SHORT).show();
Intent service1 = new Intent(context, MyService.class);
context.startService(service1);
}
}
MyService.java
public class MyService extends Service {
DatabaseHelper2 db = new DatabaseHelper2(this);
String sub;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Date date = new Date();
int now=date.getHours();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String dayOfWeek = sdf.format(date);
Cursor resNow = db.getNowSub(now,dayOfWeek);
if(resNow.getCount()!=0){
if(resNow.moveToNext()){
sub = resNow.getString(3);
}
NotificationManager notif = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification.Builder
(getApplicationContext()).setContentTitle("NowSub").setContentText("Now:"+sub+ " at "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()).setContentTitle("currenet subject").setSmallIcon(R.mipmap.ic_launcher).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
return START_STICKY;
}
I spent more than a week for this particular problem, hope someone will help.
Thanks in advance.
As in the description of AlarmManager:
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS
will shift alarms in order to minimize wakeups and battery use. There
are new APIs to support applications which need strict delivery
guarantees; see setWindow(int, long, long, PendingIntent) and
setExact(int, long, PendingIntent). Applications whose
targetSdkVersion is earlier than API 19 will continue to see the
previous behavior in which all alarms are delivered exactly when
requested.
I suggest to use setExact instead of setRepeating to be waked up at an exact time.
Also, check out JobScheduler as stated here: Android AlarmManager setExact() is not exact
Instead of this:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),AlarmManager.INTERVAL_HOUR,pendingIntent);
You need to pass an actual time when you want this to wake up, not System.currentTimeMillis()
So something like
Calendar calendar = Calendar.getInstance();
Date newDate = calendar.setTime(date2);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,calendar.getTimeInMillis(),AlarmManager.INTERVAL_HOUR,pendingIntent);
Try this instead. I change to AlarmManager.ELAPSED_REALTIME

AlarmManager executed even if the time is wrong

I am using the following code to execute the Alarm at 10:30 AM and then keep executing every 8seconds, but the problem is, this code execute the Alarm manager even when the time is not 10:30.
I have tried everything but the code keep executing the Alarm regardless of what time is set,
public class MainActivity extends Activity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
startAt10();
}
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
/* Repeating on every 8 seconds interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 8, pendingIntent);
}
}
and this is my AlarmReceiver class,
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "Welcome Back ! ", Toast.LENGTH_SHORT).show();
}
I have also added this in my AndroidManifest.xml,
<receiver android:name="com.example.test2.AlarmReceiver" android:enabled="true" >
</receiver>
How to make it to only execute at 10:30AM ?
Your code is correct. The alarm will start at 10:30 am and repeats at every 8 seconds. But once check the time in the device or emulator in which your app is running. If the time is already past then the alarm starts executing immediately.
If the time is not completed then once uninstall the application and run again it may work.

AlarmManager responding at wrong time

Sorry if answered before, but i looked everywhere but didn't get the proper solution
I am using AlarmManager to automatically fire a notification at 9am everyday, but when i try to run it on emulator it executes immediately, and every half hour (31-32min to be precise) after that instead of only once at 9am everyday.
Any ideas why? help appreciated.
code is as below:
public class Home extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bsheet);
notificationAlert(savedInstanceState);
}
private void notificationAlert(Bundle savedInstanceState) {
AlarmManager manager;
PendingIntent pendingIntent;
Intent intent=new Intent(Home.this, Notify.class);
manager=(AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent=PendingIntent.getService(Home.this,
0, intent, 0);
GregorianCalendar gcal = new GregorianCalendar();
gcal.set(Calendar.HOUR_OF_DAY, 9);
gcal.set(Calendar.MINUTE, 0);
gcal.set(Calendar.SECOND, 0);
gcal.set(Calendar.MILLISECOND, 0);
long initTime = gcal.getTimeInMillis();
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
24*60*60*1000, pendingIntent);
}
}
cheers,
EDIT: my intention is that, once the app is installed, it fires this alarm at 9am. i have put the alarm in the onCreate, so im not sure if the alarm is only being created everytime i start the app and something weird is happening when i hide the app... again insight would be appreciated!
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 24*60*60*1000, pendingIntent);
Alarm manager will fire immidiatly if initTime < System.currentTimeMillis()
from docs:
If the time occurs in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.
According to the code you've provided, gcal.getTimeInMillis() will return millisecods corresponding to Today 9.00. So if you'll try to run your code after this time, initTime will be less then current system time which triggers immidiate run of AlarmManager.
To fix this, for example, you can add one day to your calendar before passing its gcal.getTimeInMillis() if it is already in past so it will point to Tomorrow 9.00 to let it run tomorrows morning
Update1
Tried code like this and it worked as expected for me - fired service every 10 seconds:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initManager();
}
private void initManager() {
AlarmManager manager;
PendingIntent pendingIntent;
Intent intent = new Intent(this, NotifyService.class);
manager=(AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
long initTime = System.currentTimeMillis() + 5000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime,
10*1000, pendingIntent);
}
However you can use another option: there is AlarmManager.set() method
You can fire it like this
long runTime = /* your calendar time + 24 hours in millis*/
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
so it will fire your service at runTime. And then in the service invoke the same method with recalculated runTime for another day so it could look something like that:
public MainActivity extends Activity{
protected onCreate(Bundle bundle){
....
initManager();
}
private initManager(){
...
long runTime = /* time of your first alarm/notification/whatever you develope */
Intent intent = new Intent(this, NotifyService.class);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
}
}
And the service:
public NotifyService extends Service{
...
public int onStartCommand(...){
...
/* do stuff */
Intent intent = new Intent(getApplicationContext(), NotifyService.class);
pendingIntent=PendingIntent.getService(this, 0, intent, 0);
long runTime = /* recalculate it according to the next time of firing this service*/
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);
}
}
So your service will register an intent in AlarmManager itself everytime the service fires.
try this :
Calendar currentTime = Calendar.getInstance();
Calendar alarmTime = Calendar.getInstance();
currentTime.set(Calendar.HOUR_OF_DAY, 9;
currentTime.set(Calendar.MINUTE, 0);
currentTime.set(Calendar.SECOND, 0);
currentTime.set(Calendar.MILLISECOND, 0);
if (alarmTime.compareTo(currentTime) <= 0) {
// check for time
alarmTime.add(Calendar.DATE, 1);
}
Intent intent = new Intent(getBaseContext(), Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);

Categories

Resources