For some Android applications, I would like to integrate the following feature:
The user can define a time when he wants to be reminded of something. When the time has come then, the application should create a notification in the notification bar even when the user doesn't use the app at this moment.
For this purpose, the classes AlarmManager, NotificationManager und Notification.Builder are the ones to look at, right?
So how do I create a timed notification in advance? My code (so far) is this:
Add this under to the AndroidManifest to register the broadcast receiver:
<receiver android:name="AlarmNotificationReceiver"></receiver>
Create a new class file which handles the alarm that it receives:
public class AlarmNotificationReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String additionalData = extras.getString("displayText");
// show the notification now
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification mNotification = new Notification(R.drawable.ic_launcher, context.getString(R.string.app_name), System.currentTimeMillis());
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); // open MainActivity if the user selects this notification
mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), additionalData, pi);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND;
mNotificationManager.notify(1, mNotification);
}
}
}
Use this code (for example in MainActivity) to set the alarm to 3 seconds from now:
Intent i = new Intent(this, AlarmNotificationReceiver.class);
i.putExtra("displayText", "sample text");
PendingIntent pi = PendingIntent.getBroadcast(this.getApplicationContext(), 234324246, i, 0);
AlarmManager mAlarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mAlarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3*1000, pi);
What do I need to change to make this work? Thank you!
The two problems are:
The notification's text does not change when I change it in code. It only changes when I change the requestCode in PendingIntent.getBroadcast(...). What is this request code all about? Can it be a random value or 0?
After rebooting my phone, the "planned" notification, or the alarm, is gone. But now I've seen that this is normal behaviour, right? How can I circumvent this?
Not sure about part 1, but for part 2 the general approach is to intercept the BOOT_COMPLETED intent and use that to re-register all alarms. This does unfortunately mean that for each alarm you have registered with the alarm manager you have to store it in your app's db as well.
So, you'll need a broadcast receiver to intercept the BOOT_COMPLETED intent:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// get your stored alarms from your database
// reregister them with the alarm manager
}
}
To get the BOOT_COMPLETED intent, you must put the following permission in your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And the BootReceiver also needs to be registered in your manifest with the following intent filter:
<receiver android:enabled="true" android:name=".receiver.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
It's important to note that if your app is installed to the sdcard, it can never receive the BOOT_COMPLETED intent. Also, it's worth noting that this implementation is a bit naive in that it executes code immediately on booting which can slow the user's phone down at startup. So, I recommend delaying your execution for a few minutes after intercepting the boot intent.
I personally would do it without a Broadcast Receiver. I'd get the AlarmManager to fire the intent to start a seperate Activity, rather than receiver. Then this new Activity could make the notification for you. I'm not sure if this is a better way, but it seems less complicated to me.
Edit: A Service would probably be better still than an Activity
In your MainActivity:
Intent i = new Intent(getBaseContext(), NotificationService.class);
PendingIntent pi = PendingIntent.getService(getBaseContext(), 0, i, 0);
AlarmManager mAlarm = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
mAlarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10*60*1000, pi);
Your Service:
public class NotificationService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
//Create the notification here
return START_NOT_STICKY;
}
Your Manifest:
<service android:name="com.android.yourpath.NotificationService"></service>
Related
Creating notification:
PendingIntent pIntent = PendingIntent.getActivity(context, (int) taskId, intent, 0);
intent.setAction(Utils.MARK_AS_DONE);
PendingIntent pIntentMarkAsDone = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setTicker(ticker)
.setContentTitle(title)
.setContentText(description)
.setSmallIcon(getAlarmIcon(type))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
.setContentIntent(pIntent)
.addAction(0, context.getString(R.string.mark_as_done), pIntentMarkAsDone);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) taskId, notification);
I added the adding using a pending intent with getBroadcast.
Receiver:
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Log to check
}
}
This class should "receive" the action. I also add on Manifest
Manifest:
<receiver android:name=".NotificationReceiver">
<intent-filter>
<action android:name="<package_name>.MARK_AS_DONE"/>
</intent-filter>
</receiver>
Well, onReceive is not receiving. What am I doing wrong?
TL;DR: Create a fresh Intent, rather than reusing the one in intent, and get rid of the <intent-filter> from the <receiver>.
Your first line is:
PendingIntent pIntent = PendingIntent.getActivity(context, (int) taskId, intent, 0);
This implies that intent identifies some activity. If you created this via new Intent(context, YourActivityClass.class), then it has a ComponentName set inside of it, identifying your activity.
Then, you call setAction() on intent and use it with getBroadcast(). However, other than setting (or replacing) the action, everything else in intent is the same as it was. In particular, the ComponentName identifying the activity is still there. So, when the broadcast is sent, Android cannot deliver it, as the component is invalid (an activity cannot directly receive a broadcast), and the action string is ignored (as once a ComponentName is set on an Intent, things like actions and categories no longer count for routing).
So, I recommend that you create two Intent objects, one for the activity, one for the receiver.
Note that you do not need an action string for the receiver. You can use the explicit Intent constructor (new Intent(context, NotificationReceiver.class)). In fact, having the action string on the receiver is bad for security, as now any app can send you that broadcast. So, I recommend removing the <intent-filter> and using an explicit Intent to create your broadcast PendingIntent.
I'm looking for a solution on how to make sort of an alarm that after exiting an app it will start a new activity (or ten minutes if a user chooses to). It would be non-repeating, just one time.
I looked at TimerTask and Handlers, but they seem to be working only when an app is in the foreground. AlarmManager looks like it could do the job, but I don't know how to approach that. Any suggestions?
edit1:
So here is what I have in MainActivity:
Intent intent = new Intent("wake_up");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 5000, pendingIntent);
This is BroadcasReceiver:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("(packagename)", "(whole class name)");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
And how I registered it in Manifest (like other activities):
<receiver
android:name="FakeCallBroadcastReceiver" >
</receiver>
I have put Toast in BroadcastReceiver and it works, but it appears immediately - changing time from 5000 to let's say 10000 doesn't change anything.
Add this in onCreate() method in Application instance or main Activity:
Intent intent = new Intent("wake_up");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000, pendingIntent);
and start Activity in BroadcastReceiver:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startActivity(...);
}
}
Register the BroadcastReceiver with intent filter:
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="wake_up" />
</intent-filter>
</receiver>
Alarm manager is definitely the way to go. You'll need to have a broadcast receiver to receive the alarm, and then set a pending intent in that receiver.
As you mentionned, TimerTask and Handlers won't help you much here.
The easiest way to go with a broadcast receiver is to register it in the android manifest as a broadcast receiver. You can also register them manually, but it's a bit harder conceptually.
Have fun!
I have an arraylist of objects, a member of which is a date/time.
The arraylist and object is used to populate the UI listview.
I would like to send a push notification when the time from the object/listview is approaching system/mobile time (whether its 5, 10 mins or modifiable, doesn't matter).
So i'm familiarizing myself with the Notification Builder, but i'm unsure where to put the logic and how the app will monitor the time and notify when its close.
Any ideas, suggestions on the right path much appreciated
You can trigger an alarm at a scheduled time and then use an IntentService to capture it once alarm goes off.
In the main activity (lets assume the name is MainActivity, you can create an alarm like this.
AlarmManager mgr = (AlarmManager) MainActivity.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity, NotifService.class);
PendingIntent pi = PendingIntent.getService(MainActivity, 0, intent, 0);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi);
where the PERIOD defines after how much milliseconds you want the alarm to go off.
To configure the AlarmManager differently, you can read more about it.
NotifService is what captures the alarm when it goes off. Create a class called NotifService it extends IntentService
public class NotifService extends IntentService {
public NotifService() {
super("My service");
}
#Override
protected void onHandleIntent(Intent intent) {
//write your code for sending Notification
Intent intent = new Intent();
intent.setAction("com.xxx.yyy.youractivitytobecalled");
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder (getApplicationContext());
builder.setContentTitle("Your Application Title");
builder.setContentText("Notification Content");
builder.setContentIntent(pendingIntent);
builder.setTicker("Your content");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setOngoing(false);
builder.setPriority(0);
Notification notification = builder.build();
NotificationManager notificationManger = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
notificationManger.notify(1, notification);
}
}
Register NotifService in your AndroidManifest.xml
<application
fill content here />
<service android:name=".NotifService" >
</service>
</application>
Just for future reference, I created a class that extends BroardcastReceiver and used that to handle the alarms from AlarmManager - its all there in the docs but its kinda confusing how everything sits together at first.
In my manifest file I have declared the receiver. (as follows)
<receiver android:name=".OnAlarmReceive" />
however, once I shut down my application, I am not able to get the alarms and the notifications. Apparently, a call to the OnReceive in my Broadcast receiver is never made.
public class OnAlarmReceive extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent arg1)
{
//various stuff
}
}
Inside the MainActivity, my alarm manager class is as the follows.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent("MY_ALARM_NOTIFICATION");
intent.setClass(this, OnAlarmReceive.class);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar timeCal = Calendar.getInstance();
timeCal.set(Calendar.HOUR_OF_DAY, hour);
timeCal.set(Calendar.MINUTE, minutes);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), pendingIntent);
and my manifest as is follows :
<receiver android:name=".OnAlarmReceive">
<intent-filter android:priority="1">
<action android:name="MY_ALARM_NOTIFICATION"/>
</intent-filter>
</receiver>
What should I do in order to receive the notifications/alarms even if I have shut off my app. Background service ?
you should add intent-filter in manifest,as
receiver android:name=".SmsBroadCastReceiver">
<intent-filter android:priority="20">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
As Gong Cong says, you need to declare which events your receiver should listen.
For example :
<receiver android:name=".OnAlarmReceive">
<intent-filter>
<action android:name="MY_ALARM_NOTIFICATION"/>
</intent-filter> </receiver>
and then when your set your alarm, use an intent with your action :
Intent intent = new Intent("MY_ALARM_NOTIFICATION");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pi = PendingIntent.getBroadcast( this, 0, intent, 0 );
Your code is working fine!
All you have to do is to change this line:
alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(),
pendingIntent);
With this line:
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 5000, pendingIntent);
And the code in the "onReceive" will run after 5000ms (5sec) even when app is not running
In My understanding, In some cased depending on the way of implementations, OS has authority to adjust the alarm set time. So try to use AlarmManager.set(...), AlarmManager.setexact(...) etc accordingly. In Some cases, depending on the manufacturer(Custom Android OS), there is a possibility that the OS is blocking fire alarm.
Adding android:exported="true" for receiver in manifest file helped me to receive alarms (and thus, wake the application) even when application was shut-down (intentionally by me, removing app from task list).
1.Declare the receiver in the Manifest-file:
<receiver android:name="your.package.name.TestAlarmReceiver"></receiver>
Always remember that the whole Android-System is case sensitive. So check your spelling is correct in the AndroidMainfest.xml.
2.If you create a PendingIntent for your Receiver, please add an requestCode - even it is a random number! Without your onReceive code never get called!
The function which start AlarmManager should look like below:
public static void scheduleTestAlarmReceiver(Context context) {
Intent receiverIntent = new Intent(context, TestAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+startDelay, someDelay, sender);
}
BroadcastReceiver class:
package your.package.name;
public class TestAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
// your code here!
}
}
The original article: Why my BroadcastReceiver does not get called?
I'm developing a live wallpaper for Android. To refresh the wallpaper at set times I use AlarmManager. Most of the times this works great, but occasionally my alarm isn't received. On top of that I can't replicate this behaviour, it just randomly happens. I've run into this using at least 3 ROMs.
Now for the code.
I use this PendingIntent:
mRefreshIntent = new Intent()
.setComponent(new ComponentName(mContext, RefreshBroadcastReceiver.class))
.setAction("my.package.name.REFRESH_WALLPAPER");
mPendingRefreshIntent = PendingIntent.getBroadcast(
mContext,
0,
mRefreshIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
This is my code to set the alarm:
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, mPendingRefreshIntent);
where time is the UTC time in milliseconds. I often verified if the alarm is set as intended using adb shell dumpsys alarm, which it is.
The receiving side:
public class RefreshBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("DayNight", "onReceive ; " + System.currentTimeMillis());
DayNightService.refresher.refresh();
Log.d("DayNight", "onReceive done; " + System.currentTimeMillis());
}
}
Associated manifest lines:
<application>
...
<receiver
android:name="RefreshBroadcastReceiver">
<intent-filter>
<action android:name="my.package.name.REFRESH_WALLPAPER" />
</intent-filter>
</receiver>
...
</application>
Alarms which are not fired always exist in the queue (dumpsys alarms) beforehand, and are not in the alarm log afterwards. Seems like they get 'lost' at T minus zero.
I will be very happy if one of you can solve this problem for me.
I use following code:
Intent intent = new Intent(ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
Log.d(LOG_TAG, "pending intent: " + pendingIntent);
// if no intent there, schedule it ASAP
if (pendingIntent == null) {
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// schedule new alarm in 15 minutes
alarmService.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),300000, pendingIntent);
Log.d(LOG_TAG, "scheduled intent: " + pendingIntent);
}
Note, that I request inexact repeating alarm and RTC ( not RTC_WAKEUP ) - if phone is sleeping deep inside jeans pocket, user is not interested on changes of your live wallpaper - no need to waste battery juice and wake phone up
You may also need to register boot complete broadcast receiver to start update scheduling
on reboot.