I have a little problem...more 'than anything else I need some tips and examples.. I need to run a comparison of 2 strings, one of these is derived from the current date on the device, the other is a date that I write. so this comparison can be made at any time.. this comparison if is true run various command.
I have created a AlarmManager that calls on loop one service and in this service is present the comparison that i need..
private static final int EXEC_INTERVAL = 10 * 1000;
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), EXEC_INTERVAL, pendingIntent);
what is the method better to do what I need?
I use this? or need the BroadcastReceiver?
if you can give me some examples are very grateful.. thanks in advance!
As far as I can see, you are doing a test on time every 10 seconds in "MyAlarmService", am I right?
If that is the case, my suggestion is to use Handler rather than AlarmManager. According to android documentation(http://developer.android.com/reference/android/app/AlarmManager.html), you should use Handler to do the timing operation.
Besides, you may not need to start service every time you run the test. Service is not something that you should continuously start, you may want to put the test code in the looping, and starts the service only when you really need it.
As for the examples for using handler, there is a tutorial here: http://www.vogella.com/articles/AndroidPerformance/article.html
What you should do is:
Handler handler = new Handler();
handler.postDelayed(new Runnable() { /* comparison work */ }, EXEC_INTERVAL);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With