Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid service - keep checking the current time if it match a saved time
    text
    copied!<p>My Android service has to keep checking if the current time is matching a saved time. I used a service which has a timer that every minute checks if the current time is matching the saved time. If Android goes to sleep mode, he stops checking. I tried WakeLocker for this problem. It works, but it drains my battery.</p> <p>The code I'm currently using is as follow.</p> <pre><code> public class AlarmService extends Service { private static Timer timer = new Timer(); private Context ctx; private WakeLock wakeLock; @Override public int onStartCommand(Intent intent, int flags, int startId) { return Service.START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show(); super.onCreate(); ctx = this; startService(); /* Wake locker PowerManager mgr = (PowerManager)this.getSystemService(Context.POWER_SERVICE); wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Wake locker"); wakeLock.acquire(); */ } private void startService() { timer.scheduleAtFixedRate(new mainTask(), 0, 60000); } @Override public void onStart(Intent intent, int startId) { Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show(); startService(); } @Override public void onDestroy() { Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); } private class mainTask extends TimerTask { public void run() { toastHandler.sendEmptyMessage(0); } } private final Handler toastHandler = new Handler() { @Override public void handleMessage(Message msg) { Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Amsterdam")); Date currentLocalTime = cal.getTime(); DateFormat date = new SimpleDateFormat("HH:mm", new Locale("nl", "nl")); date.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam")); String currenttime = date.format(currentLocalTime); DateFormat date2 = new SimpleDateFormat("EEEE", new Locale("nl", "nl")); date2.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam")); String currentday = date2.format(currentLocalTime); if(currentday.equals("maandag")) { // "maandag" is dutch for Monday String tijd1 = "16:00"; if(currenttime.equals(tijd1)) { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(AlarmService.this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Test message") .setAutoCancel(true) .setAutoCancel(true) .setContentText("This is a test message"); Intent resultIntent = new Intent(AlarmService.this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(AlarmService.this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) AlarmService.this.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1, mBuilder.build()); } } } }; } </code></pre> <p>Does somebody have a solution for this problem, which doesn't drain my battery? So it has to check if the current time is matching the saved time, even when Android is sleeping.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload