Why does not the alarm open to Activity?

1

I have in my code an alarm that is supposed to ring when the 22:48 hours arrive but nothing happens now I wonder if the hours that the calendar uses are the system hours? if so what am i doing wrong so my alarm does not show up?

I call the function to insert a new alarm when clicking on a list:

boolean res = MyUtility.addFavorite(HorariosActivity.this, horarioitem,
                                    HorariosActivity.this.getApplicationContext());

The function returns a boolean value if it is true added to favorites.

In my Utility class I have the code that supposedly would ring the alarm:

 public static boolean addFavorite(Activity activity, String favItem , Context context) {


        String favList = getStringFromPreferences(activity, null, EVENTOS);


           AlarmManager alarmMgr;
           PendingIntent alarmIntent;

            alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, MainActivity.class);
            alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);


            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 22);
            calendar.set(Calendar.MINUTE, 55);


            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 10 , alarmIntent);



    }

I while testing the application I leave it always open because I know that restart case by what I know the alerts are lost, it still does not work my alert.

    
asked by anonymous 15.06.2016 / 17:58

1 answer

2

The code is defining an Intent for an Activity :

Intent intent = new Intent(context, MainActivity.class);

However, you are creating a PendingIntent for a BroadcastReceiver :

alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

If you want Alarm to launch the MainActivity change the previous line to:

alarmIntent = PendingIntent.getActivity(context, 0, intent, 0);

Note: Alarms are not lost if you close the application and will only be lost if you disconnect the device.

    
15.06.2016 / 23:08