Reputation: 5084
I am basically trying to show a daily notification at a scheduled time (for example: 7:30 AM everyday). However, the code I implemented doesn't show a notification at all.
The Activity where I set the time:
//This method is called by a button onClick method
private void SaveData() {
//I get the hour, minute and the AM/PM from 3 edittexts
String hours = hoursBox.getText().toString();
String minutes = minutesBox.getText().toString();
String ampm = ampmBox.getSelectedItem().toString();
if (hours.length() != 0 && minutes.length() != 0 && ampm.length() != 0) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hours));
calendar.set(Calendar.MINUTE, Integer.parseInt(minutes));
calendar.set(Calendar.SECOND, 0);
//calendar.set(Calendar.AM_PM, Calendar.AM);
Intent intent=new Intent(this, ReminderService.class);
AlarmManager manager=(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent=PendingIntent.getService(this, 0,intent, 0);
manager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),24*60*60*1000,pendingIntent);
}
}
ReminderService.java
public class ReminderService extends Service {
@Override
public void onCreate()
{
Intent resultIntent=new Intent(this, Dashboard.class);
PendingIntent pIntent=PendingIntent.getActivity(this,0,resultIntent,0);
Notification noti_builder= new Notification.Builder(this)
.setContentTitle("Hello from the other side!")
.setContentIntent(pIntent)
.setSmallIcon(R.drawable.helloicon)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti_builder.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1,noti_builder);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
What am I doing wrong here? Should I add anything to the manifest as well? These are the only two implementations I have right now. Thanks in advance!
Upvotes: 0
Views: 361
Reputation: 39201
Any Service
used in your app must be listed in the manifest. Additionally, since your Service
is to be used by only your app, setting the exported
attribute to false
is recommended.
For example, inside the <application>
tags in the manifest:
<service android:name=".ReminderService"
android:exported="false" />
Also, the Calendar.HOUR_OF_DAY
component on a Calendar
sets the hour on a 24-hour clock. To use a 12-hour clock, use Calendar.HOUR
, and set the Calendar.AM_PM
component as well.
Finally, you'll want to somehow acquire a WakeLock
to ensure that your Notification
is issued even if the phone isn't active. In lieu of handing the WakeLock
yourself, a couple of other options are available. The WakefulBroadcastReceiver
class in the v4 support library can be used to start your Service
, from which you can signal the Receiver to release the lock when done. Alternatively, you could simply replace your Service
with CommonsWare
's WakefulIntentService
class, if you don't want to add a Receiver component.
If you choose to use the WakefulBroadcastReceiver
, you might still consider changing your Service
to an IntentService
, if it's not going to be doing any long-running operations, as an IntentService
takes care of stopping itself when its work is done.
Upvotes: 1