Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5979205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:37:36+00:00 2026-05-22T21:37:36+00:00

i am working on a task scheduler applications as my college project, i have

  • 0

i am working on a task scheduler applications as my college project, i have a service that checks the expire time of an task. i had implemented handlers to check the expire time. When the expire time of an application is matched with current time then it sends a status bar notification. At this point i am pausing the Thread using Thread.sleep method for a minute, which is causing my application to hang. At logcat it shows heavy CPU usages by applications.

i am fetching the data from database, but it works fine when Thread.sleep is not called.
Please help.

Here is the code:

package com.apps.niit.taskm;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;


public class ExpireTimeService extends Service {

    DataHelper dh;
    ArrayList<String> tData=new ArrayList<String>();
    String date;
    Calendar c;
    String str;
    String str1;
    String str2;
    String str3;
    String str4;
    String str5;
    int notificationID=1;
    String [][] data;
    NotificationManager notificationManager;
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate(){
        super.onCreate();
        dh = new DataHelper(this);
        fetchData();
        handler.removeCallbacks(updateTimeTask);
        handler.postDelayed(updateTimeTask, 1000);  
    }
    public void fetchData(){
        String eDate = android.text.format.DateFormat.format("d/M/yyyy", new java.util.Date()).toString();
        tData.addAll(this.dh.selectDate(eDate));
        data =new String[tData.size()][4];
        if(!tData.isEmpty()){
            for(int i=0; i<tData.size();i++){
                breakString(tData.get(i));
                data[i][0]=str1;
                data[i][1]=str2;
                data[i][2]=str3;
                data[i][3]=str4;
            }
        }
    }

    public void stopService(){
        stopSelf();
    }
    private Runnable updateTimeTask = new Runnable() {
        public void run(){
            try {
                String time = android.text.format.DateFormat.format("k:m", new java.util.Date()).toString();

                for(int i=0; i<tData.size();i++){
                    if(data[i][3].equals(time)){
                    //send notification code goes here
                        String serName = Context.NOTIFICATION_SERVICE;
                        notificationManager = (NotificationManager)getSystemService(serName);               
                        String ticker= data[i][0]+" "+data[i][1]+" "+data[i][2]+" "+data[i][3];
                        long when= System.currentTimeMillis();
                        int icon = R.drawable.icon;
                        Notification notification = new Notification(icon, ticker,when);
                        Intent intent = new Intent (getApplicationContext(), DisplayTask.class);
                        Bundle myBundle = new Bundle();     
                        myBundle.putString("str1", data[i][0]);
                        myBundle.putString("str2", data[i][1]);Log.i("data1",data[i][1]);
                        myBundle.putString("str3", data[i][2]);Log.i("data1",data[i][2]);
                        myBundle.putString("str4", data[i][3]);Log.i("data1",data[i][3]);
                        intent.putExtras(myBundle);
                        PendingIntent launchIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
                        notification.setLatestEventInfo(getApplicationContext(), "", "", launchIntent);
                        notificationID=1;
                        notificationManager.notify(notificationID, notification);
                        Thread.sleep(10000);                    
                    }
                }
                handler.postDelayed(this, 1000);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e("Error from service", e.toString());
            }
        }       
    };

    private void breakString(String str) {
        // TODO Auto-generated method stub
        str1 = str.substring(0, str.indexOf(";"));
        str = str.substring(str1.length()+1, str.length());
        str2 = str.substring(0, str.indexOf(";"));
        str = str.substring(str2.length()+1, str.length());
        str3 = str.substring(0, str.indexOf(";"));
        str4 = str.substring(str3.length()+1, str.length());
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (handler != null)
            handler.removeCallbacks(updateTimeTask);
        handler = null;
    }
    private Handler handler = new Handler();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:37:36+00:00Added an answer on May 22, 2026 at 9:37 pm

    When you’re using Handler‘s postDelayed function here is what happens:

    1. Handler puts your Runnable into the current thread’s Looper queue.
    2. When time comes, your code in Runnable is run on the UI thread.

    Note that its not that Handler always puts Runnable to UI thread queue. It puts Runnable to current thread’s queue, and your current thread was the UI thread.

    So if you put Thread.sleep or anything time consuming (like network communication) in that updateTimeTask it will hang the whole UI thread.

    In your case you should use ScheduledExecutorService, see scheduleWithFixedDelay function. Or as alternative, you can start AsyncTask from your updateTimeTask function and do all heavy-lifting and Thread.sleep in doInBackgrund function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a schedule task that is making a webrequest. This was all working
I'm working on a Java project where I have created a class that looks
Assume that you're working a x86 32-bits system. Your task is to implement the
For example, I have two tasks: <mbean code=org.jboss.varia.scheduler.Scheduler name=:service=Scheduler> <attribute name=StartAtStartup>true</attribute> <attribute name=SchedulableClass>Scheduler1</attribute> <attribute
I have task schedule for backup directory list of wwwroot. For that I have
I have a working process, behind a web application, that is generating some records
I'm currently working on a cross-platform task scheduler, but I'm having a problem with
I'm working on a script that will run as a scheduled task under a
I have a task scheduler which runs a C# console application every minute. It
I'm working on a task system and I'd like to be able to submit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.