0

I created a service that will check some online data in specific time of the day and everyday (for example at 6 AM), and rather than using a background running service and broadcast receiver I decided to use AlarmMananger and the code below:

Intent myIntent =  new Intent(MainActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager =  (AlarmManager)getSystemService(ALARM_SERVICE);

  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(System.currentTimeMillis());
 calendar.set(Calendar.HOUR_OF_DAY, 6);
   calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
   alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

My questions are:

1- How to unregister from the AlarmManager when user decided to de-activate my service?

2- What is the use of this line

calendar.setTimeInMillis(System.currentTimeMillis());

3- What could happen if I called the same function above with the same params and data, will AlarmMananger overwrite my request or it will register a new one, and when the time comes (6 AM) it will call my service twice?

4- Is it possible to check AlarmMananger if it has successfully registered my Intent?

Edit 1:

The code below is not triggering my broadcast receiver, why?

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 6); // For 6 AM
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this, mybroadcastreceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

AndroidManifest.xml

    <receiver
        android:name=".mybroadcastreceiver"
        android:enabled="true"
        android:exported="true"></receiver>
Jack
  • 693
  • 1
  • 7
  • 25

2 Answers2

0

You can use if else function for register and unregister from AlarmManager. for example checkBox isSelectted() or not.

calendar.setTimeInMillis(System.currentTimeMillis());

this line will return you system time in millisecond format. This format is like this "2547889955511"

yes if you can perfectly set up AlarmManager your pending intent can call your service twice.

yes you have to set up perfectly then it will possible to check AlarmMananger is it successfully registered your Intent or not.

  • Thanks for all these answers, but here's another problem, I added an edit above where I want everyday at 6 PM AlertMananager should call my receiver, but that doesn't happen even for a 1 time, what is wrong in the code? knowing that I added SET_ALARM permission and the same result my receiver is not called. – Jack Feb 28 '16 at 07:13
  • Did you save your input data in any data storage ? If not then save. After save compare with save time and system time for call AlarmManager – Rashiduzzaman Khan Feb 28 '16 at 09:03
0

Coming to all your questions :-

  1. How to unregister from the AlarmManager when user decided to de-activate my service?

    Answer :- To cancel an AlarmService you created, you need to cancel it like this.

    this.getAlarmManager().cancel(pendingIntent);

where this is the service (or Activity/Context) from where you called the AlarmService.

  1. What is the use of this line calendar.setTimeInMillis(System.currentTimeMillis());

Well if you are aware of the method Date.setTime(), which generates the Date object with milliseconds provided in the argument. Calender.setTimeInMillis() does the same for the Calender object. System.currentTimeMillis() is used to provide the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. ( I guess you are aware of whole Timestamp concept)

3. What could happen if I called the same function above with the same params and data, will AlarmMananger overwrite my request or it will register a new one, and when the time comes (6 AM) it will call my service twice?

As the documentation says, If there is already an alarm scheduled for the same IntentSender, that previous alarm will first be canceled.

4. Is it possible to check AlarmMananger if it has successfully registered my Intent?

I think it is possible, but there is no such method to check it directly, you need to do the hack. I think you will find this answer suitable.

I hope this helps you :)

Community
  • 1
  • 1
Dave Ranjan
  • 2,966
  • 24
  • 55