0

I am trying to develop an alarm system in android that would function after the application closes, making multiple simultaneous alarms possible (which should be repeatable and cancelable, but I have read documentation for such functions).

Currently, I only have the UI (which fires off intents to receiver at scheduled times through AlarmManager) and the Receiver class (which extends BroadCastReceiver). Although the alarm somehow functions, there have been issues like the alarm not sounding at appropriate times until I open app again and not providing the correct alarm.

I have read that a service is commonly used in such apps to provide functionality of the application when the activity is closed. Thus, I am interested in what a service would provide in the context of the application, and whether my problems are solvable through service.

I have searched answers and read that a service is useful for running background operations (i.e. mp3 or alarm) For example, google states that "Another application component can start a service and it will continue to run in the background even if the user switches to another application. ". The following answer states that to use "alarm simultaneously you must use Service class for that".

Therefore, I think that a service may be useful for providing the functionality for the alarm. However, some people seem to represent a service as a either or with AlarmManager, which I am currently using. Most others seem to use AlarmManger to start service or perform an action of that nature. I would be very grateful if someone could shed light on what a service provides (I've already read google's explanation but it did not clear up whether I need it or not). Sending intent from UI

 if (row.get(7) == 1) {
                Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
                alarmIntent.putExtra("id", (long) row.get(0));
                AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                long longValue = (long) row.get(0);
                int intValue = (int) longValue;
                PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, intValue, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                if (row.get(8) == 1) {
                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (long) row.get(6), (long) row.get(9), pendingIntent);
                } else {
                    alarmManager.set(AlarmManager.RTC_WAKEUP, (long) row.get(6), pendingIntent);
                }

            }

AlarmReceiver class

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent)
{
   long id = intent.getLongExtra("id", 0);

    Notification.Builder builder = new Notification.Builder(context)
            .setSmallIcon(R.drawable.ic_action_search)
    PendingIntent pi = PendingIntent.getActivity(context, (int)id, new Intent(), 0);

    NotificationManager nm = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify((int)id, builder.build());
}

}

Community
  • 1
  • 1
Andy
  • 337
  • 5
  • 16

1 Answers1

0

Try to use service like this :

public class UpdateService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
         return START_STICKY;
    }
}

When onStartCommand returns START_STICKY, system restarts service if it's killed. Use this link

Community
  • 1
  • 1
Aryadegari
  • 88
  • 8
  • Thanks for your answer but I'm trying to see what it does/ should I use it rather than actually implementing code. Right now, I'm not sure if I even need a service. – Andy Jul 05 '15 at 00:32
  • no , you don't need to run a service you can just simply use AlarmManager to set alarm. please copy your code here. – Aryadegari Jul 05 '15 at 00:35
  • I put my code here but I'm not sure there is much to debug; I did not entirely finish and am in the process of making some changes. I simply was weighing the value of adding a service vs continuing as is. If there is a blatant error somewhere though I would be glad if you could point it out. Thanks – Andy Jul 05 '15 at 00:53
  • RTC_WAKEUP : Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off. so instead of : `alarmManager.set(AlarmManager.RTC_WAKEUP, (long) row.get(6), pendingIntent);` use : `alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pendingIntent);` for after 10 seconds alarm – Aryadegari Jul 05 '15 at 01:00
  • I used sqLite to keep track of the time and row 6, I had kept the time in millis for wake-up. (using calendar.getTimeInMillis()) of the UI input) – Andy Jul 05 '15 at 01:10