I need something in the TimerTask or Handler work constantly. But when I run the program, they only work 1 time. Maybe I have a mistake somewhere? Here is my code using Handler

 Timer timer = new Timer(); DB2 db; Cursor cursor; Calendar x = Calendar.getInstance(); int y = x.get(Calendar.YEAR); int m = x.get(Calendar.MONTH) + 1; int d = x.get(Calendar.DAY_OF_MONTH); int h = x.get(Calendar.HOUR_OF_DAY); int min = x.get(Calendar.MINUTE); int s = x.get(Calendar.SECOND); Handler handler; NotificationManager nm; int l, p, a, i, month, hours, minutes, seconds, m22, d22, v, g, t; int[] k = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; class UpdateTimeTask extends TimerTask{ public void run(){ MS2.this.runOnUiThread(new Runnable() { @Override public void run() { db.open(); cursor = db.getAllData(); cursor.moveToFirst(); if (cursor.moveToFirst()) { do { int d110 = cursor.getInt(cursor.getColumnIndex("day")); //here it should do some manipulations with every item in DB. if (m22 == 0) if (d22 == 0) if (v == 0) if (g == 0) not(); } while (cursor.moveToNext());//takes next item }cursor.close(); } }); } } private void runOnUiThread(Runnable runnable) { handler.post(runnable); } public void onCreate() { super.onCreate(); handler = new Handler(); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } public void not() { Intent resultIntent = new Intent(this, TEST2.class); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("My notification") .setContentText("Hello World!"); int mNotificationId = 001; NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); mNotifyMgr.notify(mNotificationId, mBuilder.build()); } public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub db = new DB2(this); db.open(); Toast.makeText(getApplicationContext(), "Service Running ", 1).show(); timer.schedule(new UpdateTimeTask(), 0, 1000); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } 

This is my code using TimerTask

 Timer timer = new Timer(); DB2 db; Cursor cursor; Calendar x = Calendar.getInstance(); int y = x.get(Calendar.YEAR); int m = x.get(Calendar.MONTH) + 1; int d = x.get(Calendar.DAY_OF_MONTH); int h = x.get(Calendar.HOUR_OF_DAY); int min = x.get(Calendar.MINUTE); int s = x.get(Calendar.SECOND); private Handler handler = new Handler(); NotificationManager nm; int l, p, a, i, month, hours, minutes, seconds, m22, d22, v, g, t; int[] k = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private void runOnUiThread(Runnable runnable) { handler.post(runnable); } private Runnable runnable = new Runnable() { @Override public void run() { db.open(); cursor = db.getAllData(); cursor.moveToFirst(); if (cursor.moveToFirst()) { do { int d110 = cursor.getInt(cursor.getColumnIndex("day")); if (m22 == 0) if (d22 == 0) if (v == 0) if (g == 0) not(); } while (cursor.moveToNext());//takes next item cursor.close(); } handler.postDelayed(this, 30000); } }; public void onCreate() { super.onCreate(); handler.postDelayed(runnable, 30000); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } public void not() { Intent resultIntent = new Intent(this, TEST2.class); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("My notification") .setContentText("Hello World!"); int mNotificationId = 001; NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); mNotifyMgr.notify(mNotificationId, mBuilder.build()); } public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub handler.postDelayed(runnable, 30000); db = new DB2(this); db.open(); Toast.makeText(getApplicationContext(), "Service Running ", 1).show(); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } 
  • It seems to me, or did you post two identical pieces of code in a row? .. - YuriiSPb
  • not. 1 using handler, 2 - timer - Danila Golubtsov
  • in the runnable itself (runnable), you need to redeple it yourself. - Vladyslav Matviienko

0