关闭progressdialog并触发另一个

时间:2014-10-02 20:29:37

标签: android alertdialog progressdialog timertask

我的目标是,如果没有互联网连接(让我们说10秒后),则关闭初始progressdialog,然后触发另一个警告,提示用户检查他的互联网连接,然后重试。

这是我的RemoteDataTask类:

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        /*
        Create the progressdialog
         */
        mProgressDialog = new ProgressDialog(MainActivity.this);
        //title :
        mProgressDialog.setTitle("SmartShop. Shopping made easy !");
        //message :
        mProgressDialog.setMessage("Chargement...");
        mProgressDialog.setIndeterminate(false);
        //show the progressdialog...Only if gpslocation is available !! :)
        if (gps.canGetlocation()){
            mProgressDialog.show();
            }
        //mProgressDialog.show();
    }

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

        long delayInMillis = 3000;
        list_of_articles = new ArrayList<Articles>();
        try {
            timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mProgressDialog.dismiss();
            }
        },delayInMillis );
            // Locate the class table named "Article" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                    "Article");
            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending
            //query.orderByAscending("ranknum");
            query.whereWithinKilometers("Localisation_Vendeur",device_location,rayon);
            ob = query.find();
            for (ParseObject article : ob) {
                // Locate images in article_image column
                ParseFile image = (ParseFile) article.get("Image_Article");

                Articles map = new Articles();
                map.setArticle_name((String) article.get("Nom_Article"));
                map.setArticle_vendor((String) article.get("Nom_Vendeur"));
                //map.setArticle_vendor((String) article.get("reduction"));
                map.setArticle_image(image.getUrl());
                list_of_articles.add(map);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(MainActivity.this,
                list_of_articles);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }



}

我的progressdialog不会忽略此代码。它出什么问题了 ?我应该在哪里拨打第二个alertdialog“检查互联网连接,然后再试一次”?

谢谢!

1 个答案:

答案 0 :(得分:0)

您应该仅从UI线程执行UI修改。 Timer在其自己的线程中运行其任务,而不是在UI线程中运行。你可以这样做:

runOnUiThread(new Runnable() { public void run() {
    mProgressDialog.dismiss();
}});

以相同的方式开始一个新的对话框。