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 9077853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:28:36+00:00 2026-06-16T19:28:36+00:00

So Android does not allow network operations to run on the main thread, fair

  • 0

So Android does not allow network operations to run on the main thread, fair enough. I am having some trouble with running this code on another thread. Can you please take a look at my code and see if this would work/is correct? Thank you I appreciate any help given.

public class StartActivity extends Activity implements Runnable {
    public static final int timeout = 3000;
    private boolean boolConStatus = false;

    public static final String TAG = "StartActivity";
    public static final String url = "serverIP";

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Log.d(TAG, "onCreated with Bundle: " + bundle);
        setContentView(R.layout.activity_start);
    }

    public void run() {
        boolean boolConStatus; 
        boolConStatus = this.isConnected();
        toaster();
    }

    public boolean getConStatus() {
        return boolConStatus;
    }


    public void toaster() {
                boolConStatus = this.isConnected();
                if (boolConStatus == true) {
                    Toast.makeText(getApplicationContext(),
                            "Connected to Server", Toast.LENGTH_LONG);
                } else {
                    Toast.makeText(getApplicationContext(),
                            "All has failed", Toast.LENGTH_LONG);
                }
}
        public boolean isConnected() {
            try {
                ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo ni = cm.getActiveNetworkInfo();

                if (ni != null && ni.isConnected()) {
                    URL server = new URL(url);
                    HttpURLConnection huc = (HttpURLConnection) server.openConnection();
                    huc.setRequestProperty("Connection", "close");
                    huc.setConnectTimeout(timeout);
                    huc.connect();

                    if (huc.getResponseCode() == 200) {
                        return true;
                    } else {
                        Log.d("NOT CONNECTED TO SERVER", "NOT CONNECTED TO SERVER");
                        return false;
                    }
                } else {
                    Log.d("NO INTERNET CONNECTION", "NO INTERNET CONNECTION");
                    return false;
                }
                } catch (Exception e) {
                    Log.d(TAG, e.getLocalizedMessage(), e);
                e.printStackTrace();
            }
            return false;
        }
    }
  • 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-06-16T19:28:37+00:00Added an answer on June 16, 2026 at 7:28 pm

    A Runnable itself is nothing without a Thread. Try adding this to your onCreate() or onResume():

     new Thread(this).start();
    

    and then probably have to read this https://developer.android.com/reference/java/lang/Thread.html so you know how things should be done…in your case, maybe the usage of AsyncTask is appropriated…but you must first understand how Threads work.

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

Sidebar

Related Questions

Today I started observing some new kind of error which does not allow me
Android does not allow native apps like Phonegap-based apps to write binary files. A
It seems the java.net.NetworkInterface implementation of android does not have a byte[] getHardwareAddress() method
The Android Developer documentation does not mention whether the method below (and similar ones)
Android Emulator Icon in windows 7 does not show in TaskBar..see my Image... I
-webkit-device-pixel-ratio query is supported by both iOS and Android but since iOS does not
I just defined a dialog as android site suggest, but it does not show
How to make spinner to fill empty place in left side? android:layout_width=fill_parent does not
Does Android support virtual memory concept? I read it does use paging but not
Does someone have the source for Android loadable kernel module for libnfc? If not,

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.