Why doesn’t the alarm open Activity?

Asked

Viewed 77 times

1

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

I call the function to insert a new Alarm by clicking on a list :

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

the function returns a boleano value if it is true bookmarks.

In my Utility class I have the code that was supposed to play 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);



    }

While testing the application I always leave it open because I know that the case of Restart so I know the Alerts are lost , still does not work my Alert.

1 answer

2


The code is defining a Intent for a Activity:

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

however it is creating a Pendingintent for a Broadcastreceiver:

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

If the intended is Alarm launch the MainActivity change the previous line to:

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

Note: Alarms are not lost if close the application only get lost if turn off the device.

  • I already modified the code and still not receiving the expected alarm any suggestion?

  • What do you mean, "I still don’t get the expected alarm"? What do you expect to happen with the "trigger" of the alarm?

  • 1

    It was a misunderstanding on my part. Thanks for the help is working the alarm now I have another problem with the firing of a notification but I’ll open another question for that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.