I am trying to make a reminder event that would display a notification at a given time in my application for sake of this example I set the Calendar
instance to one minute to the current time. This is my appointment.java
code, this is where the instance of Calendar
are initialized to current time + one minute for the sake of this example.
Calendar ctest = Calendar.getInstance();
ctest.add(Calendar.MINUTE, 1);
Intent myIntent = new Intent(Appointments.this, AlarmRec.class);
pendingIntent = PendingIntent.getBroadcast(Appointments.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, ctest.getTimeInMillis(), pendingIntent);
Then I have the following code in my AlarmRec.class
that acts as the BroadcastReceiver.
public class AlarmRec extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
Then at last in my MyAlarmService.class
I have the following
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
and my AndroidManifest
contains
<service android:name=".MyAlarmService"
android:enabled="true" />
<receiver android:name=".AlarmRec"/>
the problem I am having is nothing is being displayed no notification or anything so I'm not sure if i am doing something wrong
Also if i have made mistakes in my post please bear with me if i made mistakes with my formatting in the question.
EDIT
wow solved thanks for the help guys, got rid of broadcast receiver and just used the service although it was still not working at the end i realized i had a small typo in my android manifest
<service android:name=".MyAlarmService"
android:enabled="true" />
If you see I forgot to specify my package name for the service should have been myCF.MyAlarmService
Thanks for the help everyone I really appreciate it
try this, replace your broadcast receiver class to service `
(AlarmRec.class===> MyAlarmService.class))`
Calendar ctest = Calendar.getInstance();
ctest.add(Calendar.MINUTE, 1);
Intent myIntent = new Intent(Appointments.this, MyAlarmService.class);
pendingIntent = PendingIntent.getBroadcast(Appointments.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
EDIT
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 5000, pendingIntent);