Android 下载视图并显示弹出消息

Android 下载视图并显示弹出消息,android,android-alertdialog,Android,Android Alertdialog,如何在showDialog()中显示消息 我想在freshDownloadView完成时显示showDialog。 我的代码如下: public class MainActivity extends Activity implements View.OnClickListener { private FreshDownloadView freshDownloadView; private Button btDownloaded; private TextView btReset; privat

如何在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);

}
这是下载按钮

@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();
}


 }
@覆盖
公共void onClick(视图v){
开关(v.getId()){
案例R.id.bt_下载:
if(freshDownloadView.using())返回;
新线程(newrunnable()){
@凌驾
公开募捐{

对于(int i=0;i我建议您为此使用
AsyncTask
。您可以在
doInBackground
方法中启动下载,并在
onPostExecute
方法中显示警报对话框(根据需要)。这也将使您不再使用
Thread.sleep(100)
,这在Android上不是一个好的做法

但是,如果您仍然喜欢使用
FreshDownloadView
请使用
FreshDownloadView
的代码更新您的问题,我们可以提出一些建议