0

我是 Android 新手,我想知道在等待远程响应时阻止 UI 的最佳方法是什么。

我的问题是我向设备发送消息,我想阻止 UI 直到我得到答案,我不知道如何使用异步任务,因为我不需要在后台计算任何东西,只是等待。

有任何想法吗?

4

1 回答 1

1

这是 asyncTask 的示例。这将阻止 UI 直到使用服务器或下载..

异步任务活动

package com.javasrilankansupport.asynctask;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


public class AsyncTaskActivity extends Activity {

 Button btn_start;
 ProgressBar progressBar;
 TextView txt_percentage;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_task);

        btn_start = (Button) findViewById(R.id.btn_start);
        progressBar =  (ProgressBar) findViewById(R.id.progress);
        txt_percentage= (TextView) findViewById(R.id.txt_percentage);

        btn_start.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {

    btn_start.setEnabled(false);
    new ShowDialogAsyncTask().execute();
   }
  });
    }


    private class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void>{

     int progress_status;

     @Override
  protected void onPreExecute() {
   // update the UI immediately after the task is executed
   super.onPreExecute();

    Toast.makeText(AsyncTaskActivity.this,
            "Invoke onPreExecute()", Toast.LENGTH_SHORT).show();

    progress_status = 0;
    txt_percentage.setText("downloading 0%");

  }

  @Override
  protected Void doInBackground(Void... params) {

   while(progress_status<100){

    progress_status += 2;

    publishProgress(progress_status);
    SystemClock.sleep(300);

   }
   return null;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   super.onProgressUpdate(values);

   progressBar.setProgress(values[0]);
   txt_percentage.setText("downloading " +values[0]+"%");

  }

  @Override
  protected void onPostExecute(Void result) {
   super.onPostExecute(result);

    Toast.makeText(AsyncTaskActivity.this,
            "Invoke onPostExecute()", Toast.LENGTH_SHORT).show();

    txt_percentage.setText("download complete");
    btn_start.setEnabled(true);
  }
    }
}

activity_async_task.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/async_task"
        tools:context=".AsyncTaskActivity" />

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="34dp" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progress"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:minWidth="120dp"
        android:text="@string/start_btn" />

    <TextView
        android:id="@+id/txt_percentage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/progress"
        android:text="downloading  0%"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>
于 2013-06-08T09:29:04.213 回答