27875

How to set app to trigger service/alarm at a specific time every day?

Question:

So I want my app to send a notification at <strong>8:00 a.m. everyday</strong> subjected to whether there is an event on that day or not.

I have a SQLlite DB which stores the dates on which notification is to be sent. What I want from my app, is this- Everyday at 8 in the morning, it should check if there is an event for today. And if there is an event for that day, then send a notification.

I have already implemented the part with the DB and the notification. I just need to implement the part where the app checks the DB at 8 a.m. everyday.

**Update: ** I made a service which checks if the current time is 8:00 or not. Following is the code for that service.

public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("inside on start command for service"); myPrefs = this.getSharedPreferences("settings", this.MODE_PRIVATE); checkTime(); return START_STICKY; } // to check if the time for alarm is here private void checkTime() { // TODO Auto-generated method stub try{ System.out.println("inside check time"); Calendar cal; cal = Calendar.getInstance(); if (08 == cal.get(cal.HOUR_OF_DAY) && 00 == cal.get(cal.MINUTE)) { // to call db and check for events nm = new MyNotificationManager(this); if (nm.checkEvent()) { nm.setNotifications(); } } }catch(Exception e){ System.out.println("inside exception for check time "+e.toString()); } }

The problem is that the service is only checking the time once. How to make it check the time every minute? Can anyone please help me?

Answer1:

Use Pending Intent

A pending intent is a token that you give to another application (e.g., notification manager, alarm manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.

To perform a broadcast via a pending intent, get a PendingIntent via the getBroadcast() method of the PendingIntent class. To perform an activity via a pending intent, you receive the activity via PendingIntent.getActivity().

Or

PendingIntent + Alarm Manager + Broadcast Receiver combination can work fine you have to register and unregistere broadcast receiver dynamically not in manifest. Like

public void onResume() { IntentFilter filter = new IntentFilter(); filter.addAction("android.provider.Telephony.SMS_RECEIVED"); registerReceiver(mybroadcast, filter); } public void onPause() { unregisterReceiver(mybroadcast); }

Recommend

  • How to get values from listview android?
  • App is crashing with shared preferences
  • What's the best way to store an id in local?
  • ExifInterface returns null for all Tags
  • My AlarmManager for service update doesn't work
  • How to send a custom event to AccessibilityService?
  • How to Create Forground Service Never Stop when Application is Inactive
  • Android GPS not working in service
  • Timer in Android Service
  • Android - Issue with async tasks
  • using a Looper in a Service is the same as using a separate thread?
  • How to save state of CheckBox while scrolling in ListView?
  • How to stop user from login screen second time if app is already started once?
  • Timer inside Android service
  • send intent from main activity to two fragment
  • How to run background service after every 5 sec not working in android 5.1?
  • how can I key rotate for google cloud storage service account?
  • Fragment gives me an error while inflating 1
  • Correct implementation of List Iterator methods
  • Reading a file into a multidimensional array
  • App restarts from wrong activity
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How to get Eclipse Oxygen to run on Java 9
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Jetty Server not starting: Unable to establish loopback connection
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • sending/ receiving email in Java
  • Trying to switch camera back to front but getting exception
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Apache 2.4 - remove | delete | uninstall
  • XCode can't find symbols for a specific iOS library/framework project
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • How does Linux kernel interrupt the application?
  • How can I use threading to 'tick' a timer to be accessed by other threads?