1

在我的程序中,通过 AsynchTask 从服务器收集数据并通过微调器等待用户从数据中选择(AlertDialog.Builder 用于以微调器格式显示数据)。

我的代码是:

public class Splash extends Activity {

    protected boolean _active = true;
    protected int _splashTime = 1500;
    protected long startTime = 0;   


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        startTime = System.currentTimeMillis();
        new InitialSetAsync().execute();

    }

    private class InitialSetAsync extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            InitialSetting ins = new InitialSetting(Splash.this);
            ins.doInitialSetting();

            try {
                while ((System.currentTimeMillis() - startTime) < _splashTime) {
                    Thread.sleep(100);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
                Intent intent = new Intent(getApplicationContext(),
                        MainActivity.class);
                startActivity(intent);
                Splash.this.finish();

        }

    }

}

以微调器格式显示的数据部分:

public class InitialSetting {

    private Context context;

    DbTransaction dbTran;
    Process checkProcess;
    String userCountry;
    String country = "";
    String[] availableCountries;
    int flag=0;
    public InitialSetting(Context context) {
        this.context = context;
    }

    public int doInitialSetting() {
            checkProcess = new Process();
        String hasVisit = checkProcess.retrieveFromPreference(context);
        if (hasVisit.equals("true")) {

        } else {
availableCountries = checkProcess.checkAvailableCountries();

            final AlertDialog.Builder ab = new AlertDialog.Builder(context);

            ((Activity) context).runOnUiThread(new Runnable() {
                public void run() {
                    ab.setSingleChoiceItems(availableCountries, -1,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {



                                    country = availableCountries[whichButton];

                                    if (!country.equals("") && country != null) {

                                        Configuration.setCounty(country);

                                        checkProcess.copyDatabaseFromServer(
                                                country, context);
                                        dbTran.storePreference(country);

                                        dialog.cancel();
                                    }

                                }
                            });
                    ab.show();

                }
            });

        }
        return 0;
    }

}

就我而言,线程在用户从 Spinner 中选择值并导致错误之前正在运行。如何暂停异步任务直到用户选择值。

4

0 回答 0