Notification Every Day At The Same Time
Solution 1:
Point A: The service code is missing a key component
In the code above, the service has an onCreate
and onDestroy
, which will be triggered when the service is created and destroyed. However, if a service is triggered and it is already running, then it will not go through onCreate
. It will, however, go through onstartCommand
(onStart
pre android 2.0). The actual structure of your code should be:
onCreate() {
// Stuff you only do when this class is instantiated the first time// and don't need to do if it is called (started in android terminology)// thereafter
}
// The next two are >=2.0 and then <2.0@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {
startHandleIntent(intent);
returnSTART_STICKY; // If you want the service to hang around
}
@OverridepublicvoidonStart(Intent intent, int startId) {
startHandleIntent(intent);
}
voidstartHandleIntent(Intent intent) {
// Do things that shiould happen every time here// eg. in your case, the notification
}
Point B: This isn't really what a service was designed for
You cannot rely on a service hanging around for that long. Inactive services will often be removed to make space for other things. Given that the the service does very little, it would probably be better to use a BroadcastReceiver, which was designed specifically for things that need triggering occasionally but don't really need to be there otherwise. So:
Use a
BroadcastRecevier
to catch the triggers and issue a notification. Something like this:classMyBroadcastReceiverextendsBroadcastReceiver { @OverridepublicvoidonReceive(Context context, Intent intent) { // Issue the notidfication <...> // Reissue a request for a future alarm call here if needed <...> } }
Remember to set it up to receive broadcasts in the manifest:
<application> ... other stuff ... <receiverandroid:name=".MyBroadcastReceiver"android:enabled="true"><intent-filter><actionandroid:name="com.mystuff.coolapp.ACTION_TIME_FOR_NOTIFICATION"/></intent-filter></receiver></application>
To trigger that, you need an intent that will trigger a broadcast:
Intentintent=newIntent("com.mystuff.coolapp.ACTION_TIME_FOR_NOTIFICATION"); context.sendBroadcast(intent);
If you are setting it up to call later via a PendingIntent (change the final flag to zero if you want a reusable
PendingIntent
for a recurring event):Intentintent=newIntent("com.mystuff.coolapp.ACTION_TIME_FOR_NOTIFICATION"); PendingIntentpi= PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT)
If later on you wish to change, or cancel somehting, or if you simply need to know if the Pending Intent exists from the system's point of view:
Intentintent=newIntent("com.mystuff.coolapp.ACTION_TIME_FOR_NOTIFICATION"); PendingIntentpi= PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE); if (pi != null) { // It exists. If you want then to cancel the alarm that triggers it: alarmManager.cancel(pi); } else { // It doesn't exist. If you need to create a reusable PendingIntent:PendingIntentpi= PendingIntent.getBroadcast(context, 0, intent, 0); }
Personally, I would use this approach instead of
initializePendingIntent
, ie:publicstatic PendingIntent getPendingIntent() { Intentintent=newIntent("com.mystuff.coolapp.ACTION_TIME_FOR_NOTIFICATION"); PendingIntentpi= PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE); // If it exists return itif (pi != null) return pi; // It doesn't exist, make it (last parameter to 0 for reusable):return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT); }
Use SharedPreferences (as you already do) to keep track of what is going on (time of alarm)
My preference would be to only create a one shot alarm with a one shot intent for when the next alarm should sound. If it changes, remove this alarm and create a new one. When it triggers, crate a new one. This way you minimise the number of things that have to stay alive for lengths of time.
Solution 2:
Check your logcat for a stack trace. It will be before the activity manager service entries you have provided. This line looks suspect to me, specifically the setAction
as it is not providing a proper resource value for the icon:
mNotifyBuilder.setContentTitle(getString(R.string.app_name)).setContentText(getString(R.string.notification_contenttext)).setContentIntent(pIntent).addAction(0, getString(R.string.notification_action), pIntent).setAutoCancel(true)
Post a Comment for "Notification Every Day At The Same Time"