下载查看并显示弹出消息

时间:2017-06-24 03:27:05

标签: android android-alertdialog

如何在showDialog();

中显示消息

我想在freshDownloadView完成后显示showDialog 我的代码如下:

public class MainActivity extends Activity implements View.OnClickListener {

private FreshDownloadView freshDownloadView;
private Button btDownloaded;
private TextView btReset;
private TextView btDownloadError;
private final int FLAG_SHOW_OK = 10;
private final int FLAG_SHOW_ERROR = 11;

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

        int progress = (int) msg.obj;
        freshDownloadView.upDateProgress(progress);

        switch (msg.what) {

            case FLAG_SHOW_OK:


               break;

            case FLAG_SHOW_ERROR:
               freshDownloadView.showDownloadError();
              break;
        }
    }
};

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    freshDownloadView = (FreshDownloadView) findViewById(R.id.pitt);
    btDownloaded = (Button) findViewById(R.id.bt_downloaded);
    btReset = (Button) findViewById(R.id.bt_reset);
    btDownloadError = (Button) findViewById(R.id.bt_download_error);
    btDownloaded.setOnClickListener(this);
   btReset.setOnClickListener(this);
    btDownloadError.setOnClickListener(this);

}

这是按钮freshDownload

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bt_downloaded:
            if (freshDownloadView.using()) return;
            new Thread(new Runnable() {
                @Override
                public void run() {

                    for (int i = 0; i <= 100; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Message message = Message.obtain();
                        message.obj = i;
                        handler.sendMessage(message);

                        showDialog();
                    }

                }

            }).start();

            break;

        case R.id.bt_reset:
            freshDownloadView.reset();
            break;
        case R.id.bt_download_error:
            if (freshDownloadView.using()) return;
            new Thread(new Runnable() {
                @Override
                public void run() {

                    for (int i = 0; i <= 30; i++) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Message message = Message.obtain();
                        if (i == 30) {
                            message.what = FLAG_SHOW_ERROR;
                        }
                        message.obj = i;
                        handler.sendMessage(message);
                    }
                }
            }).start();
            break;
    }
}


public void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setTitle(R.string.dialog_title)
            .setMessage(R.string.download_app)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
    builder.create().show();
}

protected void onPause() {
    //  unregisterReceiver(receiver);
    super.onPause();
}

protected void onResume() {
    //  registerReceiver(receiver, new IntentFilter(
    //  WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}


 }

1 个答案:

答案 0 :(得分:0)

我建议您使用['sometimes', 'yellow', 'red']。您可以使用AsyncTask方法启动下载,并在doInBackground方法中显示警报对话框(根据需要)。这也可以让您免于使用onPostExecute,这在Android上不是一个好习惯。

但是,如果您仍然希望使用Thread.sleep(100),请使用FreshDownloadView的代码更新您的问题,我们可以提供一些建议。