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

  • Home
  • SEARCH
  • 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 7008219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:42:24+00:00 2026-05-27T21:42:24+00:00

First I will explain the current situation. I’ve 2 different threads in 2 services(read

  • 0

First I will explain the current situation.
I’ve 2 different threads in 2 services(read from usb port service and make web requests service). I’m starting them in onCreate of my activity like:

serialServiceIntent = new Intent(NDKSerialActivity.this, SerialService.class);
startService(serialServiceIntent);
webServiceIntent = new Intent(NDKSerialActivity.this, RecordWebService.class);
startService(webServiceIntent);

There is nothing wrong with serial service but in RecordWebService when I make a request my gui stops until response comes.

The code is like that:

public class RecordWebService extends Service
{
public static final String SERVER_ADDRESS = "http://192.168.1.100:8080/MobilHM/rest";
private static final String TAG = RecordWebService.class.getSimpleName();
private RecordWebThread recordWebThread;

@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);
    recordWebThread = new RecordWebThread(true);
    recordWebThread.start();
}

@Override
public void onDestroy()
{
    super.onDestroy();
    Log.i(TAG, "RecordWebService Destroyed");
}

@Override
public IBinder onBind(Intent intent)
{
    return null;
}
}

and

public class RecordWebThread extends Thread
{
private static final String TAG = RecordWebThread.class.getSimpleName();
public boolean always;


public RecordWebThread(boolean always)
{
    this.always = always;
}

@Override
public void run()
{
    PatientRecord patientRecord = new PatientRecord();
    while (always)
    {
        RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello");
        try
        {
            restClient.execute(RequestMethod.GET);
        }
        catch (Exception e1)
        {
            Log.e(TAG, "", e1);
        }
        Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode());
        Log.i(TAG, "Server Response:->" + restClient.getResponse());
        try
        {
            sleep(4 * 1000);
        }
        catch (InterruptedException e)
        {
            Log.e(TAG, "Web service interrupted", e);
        }
    }
}
}

Also I’ve tried to remove sleep part and make the thread to run with timer and timer task like:

public void sendRecord()
{
    scanTask = new TimerTask()
    {
        public void run()
        {
            handler.post(new Runnable()
            {
                public void run()
                {
                    RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello");
                    try
                    {
                        restClient.execute(RequestMethod.GET);
                    }
                    catch (Exception e1)
                    {
                        Log.e(TAG, "", e1);
                    }
                    Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode());
                    Log.i(TAG, "Server Response:->" + restClient.getResponse());
                }
            });
        }
    };
    t.schedule(scanTask, 1000, 4000);
}

but no luck, my gui hangs when it comes to restClient.execute .

You can find RestClient.java @ http://www.giantflyingsaucer.com/blog/?p=1462

How can I make my requests not block my gui thread?

Edit:

public void sendRecord()
{
    scanTask = new TimerTask()
    {
        public void run()
        {
            RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello");
            try
            {
                restClient.execute(RequestMethod.GET);
            }
            catch (Exception e1)
            {
                Log.e(TAG, "", e1);
            }
            Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode());
            Log.i(TAG, "Server Response:->" + restClient.getResponse());
        }
    };
    t.schedule(scanTask, 1000, 4000);
}

Without handler, I call this in onCreate of my activity but still ui hanging.

  • 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-27T21:42:24+00:00Added an answer on May 27, 2026 at 9:42 pm

    Or you can use an IntentService which will handle the thread issues for you.

    This is an example class:

    public class MyService extends IntentService {
    
        public MyService() {
            super("MyService");
        }
    
        public MyService(String name) {
            super(name);        
        }
    
        @Override
        protected void onHandleIntent(Intent arg0) {
    
                    //Do what you want
            }
    }
    

    Then you just call:

    Intent intent = new Intent(getApplicationContext(),MyService.class);
    startService(intent);
    

    Edit:

    To repeat the same thing every 4 seconds you should do something like this:

     PendingIntent serviceIntent= PendingIntent.getService(context,
                    0, new Intent(context, MyService.class), 0);
    
       long firstTime = SystemClock.elapsedRealtime();
       AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    
       long intervalInSec = 4;
    
       am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, intervalInSec*1000, serviceIntent)
    

    ;

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

Sidebar

Related Questions

i have a problem with a asp.net website first i will explain the current
First I will explain the structure of my project: I have a WCF-service public
Before delving into the issue, first I will explain the situation. I have two
First I will try to explain what I want to do. The app loads
I will first explain what I'm trying to do then I will explain why
First let me explain my goal. I am trying to make an Animation that
I have a CSV file. The first row will always contain column headers. Depending
I have two desktop applications. After closing the first application, the first application will
I want to display a UITableView with Multiple Columns. The first column will have
Which command will executed first,If a stored procedure have individual multiple select commands;

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.