Question

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

Was it helpful?

Solution

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);

OTHER TIPS

Please follow the following code:

long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis * DateUtils.MINUTE_IN_MILLIS;
Maybe you meant for the alarm to go off in one minute:

long nextUpdateTimeMillis = currentTimeMillis + DateUtils.MINUTE_IN_MILLIS;
Anyway first use:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                          System.currentTimeMillis() + 10000, 
                          5000,
                          pendingIntent);
To confirm your setup is correct, if so you need to recalculate your nextUpdateTimeMillis

Courtesy of Sam https://stackoverflow.com/a/13593926/1465910

You need to register the broadcast in your service, similar to this one: register receiver in a service

Call the service from the Activity because no need of receiver:

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);
      alarmManager.set(AlarmManager.RTC, ctest.getTimeInMillis(), pendingIntent);
startService(myIntent );

After that change your MyAlarmService.class by the following code:

    @Override   
    public void onCreate() 
    {   
      super.onCreate(); 

    }

    @Override   
    public int onStartCommand(Intent intent, int flags, int startId) 
    {   


        NotificationManager mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
           Intent intent1 = new Intent(this.getApplicationContext(),MainActivity1.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);
        return 0;

    }

It will work. try this and let me know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top