Trondro Mulligan
Trondro Mulligan

Reputation: 505

Handler not working for 60 seconds delay?

I'm running a Handler to show some notifications in the background. While testing, when I set the delay limit to 5 seconds it works perfectly. But whenever I set it to 60 seconds or more it doesn't work. Why is that?

    int delay = 1000 * 60;
    HandlerThread hThread = new HandlerThread("HandlerThread");
    hThread.start();
    Handler handler = new Handler(hThread.getLooper());

    Runnable task = new Runnable() {
        @Override
        public void run() {
            NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Intent launchIntent = new Intent(getApplicationContext(), AndroidLauncher.class);
            PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, launchIntent, 0);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidLauncher.this);
            //Set notification information
            builder.setSmallIcon(R.drawable.ic_launcher)
                    .setTicker("Hi!")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setContentTitle(mytitle)
                    .setContentText(mytext)
                    .setContentIntent(contentIntent);

            NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(builder);
            style.setSummaryText("4 Task");
            style.addLine("Two");
            style.addLine("Three");
            style.addLine("Four");
            style.addLine("Five");

            Notification note = style.build();
            manager.notify(50, note);
        }
    };

    handler.postDelayed(task, delay);

Upvotes: 1

Views: 676

Answers (1)

Amir
Amir

Reputation: 16587

Import import android.os.Handler;

and use following piece of code :

  new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //do your work here after 60 second
                }
            },60000);

There is good tutorial about Handler, HandlerThread, Looper here.

Upvotes: 1

Related Questions