简体   繁体   中英

Android thread doesn't work

I want to run some data in a background thread in my application but when I tried in my app it didn't work.

Please help me.

Below is my code (what I tried so far):

package com.example.mos.androidclientmymobile;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.os.Handler;
import android.widget.Toast;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
  private TextView textView;
  Handler handler=new Handler();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=(TextView)findViewById(R.id.textview);

    Thread t=new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          ServerSocket serverSocket=new ServerSocket(9000);
          log("watinng for client");
          Toast.makeText(getApplication(),"yes",Toast.LENGTH_LONG).show();
          Socket socket =serverSocket.accept();
          log("a new client connected");
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    });
    t.start();
  }

  public void log(final String message){
    handler.post(new Runnable(){
      @Override
      public void run(){
        textView.setText(message);
      }
    });
  }
}

I've searched this topic but I couldn't find the answer.

Try the following code:

public class MainActivity extends AppCompatActivity {

    private Handler handler;

    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //without the Looper.getMainLooper should work too
        handler = new Handler(Looper.getMainLooper());
        startProcessing();
    }

    private void startProcessing() {
        progressDialog = ProgressDialog.show(this, "Loading", "Loading data");
        t.start();
    }


    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(2000);
                log("a new client connected");
            } catch (Exception e) {
                Log.e("TAG", e.getLocalizedMessage());
            }
        }
    });

    public void log(final String message) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        });
    }
}

xml can be as simple as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

//if the above code works for you then the problem is in some other place, but not the handler or UI thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM