Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 创建线程以运行长时间工作,但不运行响应_Android_Multithreading - Fatal编程技术网

Android 创建线程以运行长时间工作,但不运行响应

Android 创建线程以运行长时间工作,但不运行响应,android,multithreading,Android,Multithreading,Commitdata连接数据并将数据表单datahandler提交到服务器。但我不知道为什么它显示不响应对话框。如果我不关闭它,它将继续提交。为什么在其他线程中提交数据时会影响UI thread = new Thread(new Runnable() { @Override public void run() { synchronized (datahandler) {

Commitdata连接数据并将数据表单datahandler提交到服务器。但我不知道为什么它显示不响应对话框。如果我不关闭它,它将继续提交。为什么在其他线程中提交数据时会影响UI

thread = new Thread(new Runnable() {

            @Override
            public void run() {

                    synchronized (datahandler) {
                        while (true) {
                            try {
                                if (datahandler.getCount() > 0) {                                   

                                     commitData();
                                }
                                datahandler.wait();

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                Log.e("Service", e.toString());
                            }
                        }

                }

            }
        });
        thread.start();
您可以使用处理程序在GUI线程上发布消息的代码进行sea。你可以在这里读到更多关于它的信息
也请阅读此

,以便UI做出响应。您必须在runOnUIThread上运行线程。@user3819094请尝试使用
AsyncTask
。我以前使用AsyncT。由于某些原因,它没有成功。我不知道为什么当我在其他线程中提交数据时UI线程会受到影响
public class ThreadsLifecycleActivity extends Activity {
// Static so that the thread access the latest attribute
private static ProgressDialog dialog;
private static Bitmap downloadBitmap;
private static Handler handler;
private ImageView imageView;
private Thread downloadThread;


/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 // create a handler to update the UI
 handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    imageView.setImageBitmap(downloadBitmap);
    dialog.dismiss();
  }

};
// get the latest imageView after restart of the application
imageView = (ImageView) findViewById(R.id.imageView1);
Context context = imageView.getContext();
System.out.println(context);
  // Did we already download the image?
  if (downloadBitmap != null) {
    imageView.setImageBitmap(downloadBitmap);
  }
  // check if the thread is already running
  downloadThread = (Thread) getLastNonConfigurationInstance();
  if (downloadThread != null && downloadThread.isAlive()) {
   dialog = ProgressDialog.show(this, "Download", "downloading");
  }
 }

public void downloadPicture(View view) {
dialog = ProgressDialog.show(this, "Download", "downloading");
downloadThread = new MyThread();
downloadThread.start();
}

// save the thread
@Override
 public Object onRetainNonConfigurationInstance() {
 return downloadThread;
}

// dismiss dialog if activity is destroyed
@Override
protected void onDestroy() {
if (dialog != null && dialog.isShowing()) {
  dialog.dismiss();
  dialog = null;
}
super.onDestroy();
}
static public class MyThread extends Thread {
@Override
public void run() {
  try {
    // Simulate a slow network
    try {
      new Thread().sleep(5000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    downloadBitmap = downloadBitmap("http://www.devoxx.com/download/attachments/4751369/DV11");
    // Updates the user interface
    handler.sendEmptyMessage(0);
  } catch (IOException e) {
    e.printStackTrace();
  } finally {

  }
}
}
}
//==========================