So I have a BroadcastReceiver which looks like this:
public class UpdateReceiver extends BroadcastReceiver {
public UpdateReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
System.out.println("Broadcast received");
PendingIntent operation = PendingIntent.getService(context, 0,new Intent("REFRESH_THAT"), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), cal.getTimeInMillis(), operation);
}
}
This is how I call the BroadcastReceiver
Intent in = new Intent ("REFRESH_BROADCAST");
sendBroadcast(in);
And this is my intent-filter in Android Manifest file
<service android:name = ".services.RefreshService">
<intent-filter>
<action android:name="REFRESH_THAT"/>
</intent-filter>
</service>
<receiver android:name=".services.UpdateReceiver">
<intent-filter>
<action android:name="REFRESH_BROADCAST"/>
</intent-filter>
</receiver>
BroadcastReceiver received a brodcast without any problem, but AlarmManager seems to do nothing. If I call operation.send() it works without a problem, so I presume there is something wrong with AlarmManager.
Alright so finally I found a solution a it was my fault.
I have set int type to AlarmManager.ELAPSED_REALTIME and long triggerAtMillis to System.currentTimeMillis() property of alarmManager.setInexactRepeating(.....) which is wrong, it only can be paired with AlarmManager.RTC / RTC_WAKEUP.
So the functional code is this:
PendingIntent operation = PendingIntent.getService(context, 0,new Intent("REFRESH_THAT"), 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() , 30000, operation);
I should really read API documentation more carefully. If you hover AlarmManager.ELAPSED_REALTIME , it will told you what kind of time trigger you have to use. Hope this my stupid mistake will help someone in future.
As per the setInexactRepeating doc, The third parameter is:
intervalMillis: interval in milliseconds between subsequent repeats of the alarm
Then you should put 10000 ms (which is 10 sec) instead of cal.getTimeInMillis()
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, cal.currentTimeMillis(), 10000, operation);
Which means that, youer alarm will go off after 10 sec of the intent firing, then will repeat each 10 sec. And of course, since you are using this method, This is inexact timing.
Related
I have in my MainActivity a listView containing "Entry" objects, In my entry object I have a function getDaysLeft() that returns int,
I want my application to issue a notification whenever the getDaysLeft() of any item in the list becomes less than a set threshold.
How do I have the application check for that in the background and issue a notification whenever this happens ?
You'll need to use AlarmManager and BroadcastReceiver.
Checking for timing condition in background is bad idea, it's way better to tell Android "wake up my app tomorrow at 10 AM".
To set up one-time alarm that wakes your app up:
AlarmManager AM = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("com.yourapp.package.SOME_ACTION");
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTimeInMillis() 24 * 60 * 60 * 1000); //24h from now
AM.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pending);
Your .package.MyAlarmReceiver class:
public class MyAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Show a notification here
}
}
Also, you'll need to register your MyAlarmReceiver in AndroidManifest.xml:
<receiver android:name=".package.MyAlarmReceiver">
<intent-filter>
<action android:name="com.yourapp.package.SOME_ACTION" />
</intent-filter>
</receiver>
In case you've got lot of Entries schedule alarm for nearest notification or two, then in onReceive method schedule alarm for next notification and so on, to avoid cluttering AlarmManager.
No matter how I try, I cannot get the alarmManager and receiver to work - there seem to be SO many different ways to do it from what i've searched, but nothing seems to work.
I have in Manifest:
<receiver android:name="AlarmReceiver" />
I am using inner class for broadcastReceiver:
private class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("ALARM","TRIGGERED");
Notification(filteredList.get(0).getTitle());
}
}
I call function SetAlarm to start the manager:
private void SetAlarm(long time) {
AlarmManager alarm_mgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
//PendingIntent pi = PendingIntent.getService(MainActivity.this, 0, intent, 0);
PendingIntent pi = PendingIntent.getBroadcast(this, 111, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
alarm_mgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
Log.d("ALARMSET", "Alarm " + calendar.getTime().toString());
}
As you can see, in the receiver I have a notification call - I won't post because I know this works, but there is also a log that is not working.
For testing purposes I've set time to current time using the Calendar function.
You can see the pendingIntent, i have tried both getService and getBroadcast, I can't figure out which I need, but neither works anyway.
I'm sure I'm missing something, i don't know what, and I can't seem to find any answers at Google.
Is the AlarmReceiver ok as an inner class ? I have put it there because I need access to my "filteredList" List object.
thx
I am using inner class for broadcastReceiver:
This will not work. Android has to be able to create an instance of your BroadcastReceiver, and it cannot do so. At best, your BroadcastReceiver could be a static nested class, but then you would have to fix your manifest entry to refer to the outer class:
<receiver android:name="OuterClass$AlarmReceiver" />
No, actually the BroadcastReceiver needs to be public.Only then you can register it in manifest and access them. Place the AlarmReceiver code in a separate .java file. You will see the log inside the BroadcastReceiver.
try this
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
For more details.. Look this
I stucked 1 day for this problem, and finally solved this.
AlarmManager is used to generate a broadcast every 30 seconds, and a broadcast receiver is used to receive the message and execute ...
So the main problem is the following 2 steps
1. In the AndroidManifist.xml a action filter needed.
<receiver android:name="com.istep.gps.straight.MyTimerReceiver">
<intent-filter>
<action android:name="com.istep.gps.straight.MyTimerReceiver.Action"/>
</intent-filter>
</receiver>
2. The most important is the Intent need to be empty, no AAA.class
private void initTimer(Context context) {
Intent intent = new Intent().setAction("com.istep.gps.straight.MyTimerReceiver.Action")
.putExtra("id",UniqueID.getID(context));
PendingIntent sender = PendingIntent.getBroadcast(context, 18, intent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1000 * 30, sender);
}
When you use calendar, you should setTimeInMillis
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
I was trying to display the notifications using background service for which I wrote 3 classes. The service should run in background even if the appllication closed and and it should start running automatically so I used ,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.s2si.ucom.ui.UcomBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
So in onReceive() of BroadcastReceiver class I am starting the service using,
Intent service = new Intent(context, AlarmService.class);
context.startService(service);
In service class I am using AlarmManager to set particular interval so that I can display notification for every period of time. and finally I am invoking the BroadcastReceiver class to display the notification using AlarmManager. The problem is I am not getting the output :-(...instead of that I am getting error like, Couldn't load memtrack module (No such file or directory)... I am literally new to android coding...Can any one help me how to display notifications in such manner..and let me know what I did wrong above...Thanks much :-).
code in onCreate() method of service class,
Intent intent = new Intent(context, NotificationServiceReceiver.class);
PendingIntent sender = PendingIntent
.getBroadcast(context, 0, intent, 0);
// We want the alarm to go off 5 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 5 * 1000;// start 5 seconds after first register.
// Schedule the alarm!
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 5000, sender);// 5seconds interval
in short this is how you create a broadcast
private static final String ACTION_ALARM = "your.company.here.ACTION_ALARM";
public static void createAlarm(){
Intent alarmIntent = new Intent();
alarmIntent.setAction(ACTION_ALARM);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timestamp for alarm, pi);
}
public void onReceive(...){
//whatever is supposed to happen on receive
}
and you need to declare that broadcastreceiver and the actionname its supposed to receive in manifest:
<receiver
android:name="your.company.here.AlarmReciever">
<intent-filter>
<action android:name="your.company.here.ACTION_ALARM" />
</intent-filter>
</receiver>
I am building a cab booking app, I need current location of the cab every 20 seconds.
I have defined a AlarmManager and need it to repeat itself every 20 seconds. But its not repeating itself regularly. Instead it repeated itself after 233 seconds, and just once. What am I doing wrong here ?
My HomeScreen has a inner class OnAlarmReceiver, in the onCreate of my HomeScreen I am calling AlarmManager
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, OnAlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
cal.getTimeInMillis(), God.UPDATE_PENDING_INTERVAL, pi);
Inner class in HomeScreen
public class OnAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// PullPendingRequests.acquireStaticLock(context);
Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG)
.show();
Log.d("Taxeeta:PullPendingRequets", "CallService Location");
context.startService(new Intent(context, PullPendingRequests.class));
}
}
My AndroidManifest file has
<service
android:name="com.taxeeta.support.PullPendingRequests"
android:enabled="true"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Light.NoTitleBar" />
<receiver android:name=".com.taxeeta.HomeScreen.OnAlarmReceiver" />
</application>
Output of adb shell dumpsys alarm
com.taxeeta
51471ms running, 5248 wakeups
5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver
Output of adb shell dumpsys alarm | grep taxeeta
ELAPSED_WAKEUP #7: Alarm{409303b0 type 2 com.taxeeta}
operation=PendingIntent{408ba2d8: PendingIntentRecord{40887be8 com.taxeeta broadcastIntent}}
com.taxeeta
5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver
To fix it, I removed the inner class OnAlarmReceiver and fixed the androidmanifest.xml file.
<receiver
android:name="com.taxeeta.support.OnAlarmReceiver"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
If the answer above doesn't work for you then there is another way to not receive any callbacks when AlarmManager fires an expired alarm. You simply need to check this one out: by sending the wrong Intent on instantiation of PendingIntent. For example you wanted to receive a call onReceive on one of your receivers but you instantiated a PendingIntent via getActivity or getService, but what you actually meant is getReceiver.
When creating instance of PendingIntent, there are many ways to create it (getService, getActivity,getReceiver, getForegroundService:
if you want Activity the receiver of the intent then you:
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);
if you want BroadcastReceiver the receiver of the intent:
PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);
if you want a foreground Service the receiver of the intent:
PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);
if you want a Service the receiver of the intent:
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);
Also, make sure you intents are pointing to the correct class. (e.g. creating intents for Activity, Service etc.). You will not receive any call if you pass wrongfully like this:
Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver
// PendingIntent was created in such a way
// you wanted this to be received by an activity.
// you will not receive any call if you set it up like this.
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);
I also posted similar answer here.
HTH
This piece of code worked for me and make sure you have added reciever in android manifest file.
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 20 seconds after boot completed
cal.add(Calendar.SECOND, 20);
//
// Fetch every 20 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 1000*20, pending);
The above solutions didn't work for me.
Additionally, registering dynamically via code did the trick:
Intent intent = new Intent();
intent.setAction("android.intent.action.NOTIFY");
//Register the receiver
context.registerReceiver(new OnAlarmReceiver(),new IntentFilter());
For anyone still stuck - make sure your broadcast receiver is not crashing in the background. Make sure to check your LogCat!
I know this a basic problem but it is still driving me crazy. I am setting a repeating alarm but the receiver is never called.
Intent intent = new Intent(NewSchedule.this, RepeatingAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(NewSchedule.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), 5 * 1000, sender);
Log.i("calendar",calendar.getTimeInMillis() + "");
Toast.makeText(NewSchedule.this, "repeating_scheduled", Toast.LENGTH_SHORT).show();
public class RepeatingAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "repeating_received", Toast.LENGTH_LONG).show();
}
}
<receiver android:name=".RepeatingAlarm" android:process=":remote" />
I am testing on my phone. The calendar log shows the exact time. I never get the Toast in the receiver class.
Reference : Android Alarm Manager with broadcast receiver
Intent sender = new Intent("WhatEverYouWant");
PendingIntent senderPIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, senderPIntent);
// In Manifest.xml file
<receiver android:name="com.package.YourOnReceiver">
<intent-filter>
<action android:name="WhatEverYouWant" />
</intent-filter>
</receiver>
Actually it turned out my code was good. Somehow the alarm was up and running and thus for some unknown reason (at least to me) the recevier could not be called. I figured it out when I created a new project and tested that that receiver was working fine. I also had to stop that alarm. Then I went back to my original project and started the same alarm without changing any lines and it was working fine. Has anyone experienced this?