2016-07-19 3 views
0

내 애플리케이션의 배경 스레드에서 데이터를 실행하고 싶지만, 내 애플리케이션에서 해보았을 때 작동하지 않았습니다.안드로이드 쓰레드가 작동하지 않습니다.

도와주세요. 내가이 주제를 검색 한하지만 난 대답을 찾을 수 없습니다

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); 
     } 
    }); 
    } 
} 

:

다음은 (내가 지금까지 뭘하려합니다) 내 코드입니다.

+0

이 더 구체적를 그것은 무엇을해야 하는가? 실제로 무엇을하고 있습니까? 오류가 있습니까? 스택 추적을 게시 할 수 있습니까? 문제가있는 곳을 좁힐 수 있었습니까? –

+0

로그 작업을하고 토스트 –

+0

그것은 스레드 나던 작업을 의미합니다 –

답변

0

는 다음과 같은 코드를 시도해보십시오. "작동하지 않습니다"

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. 
관련 문제