简体   繁体   中英

Download View and show popup message

How to show message in showDialog();

I want when freshDownloadView is finished show showDialog.
My code is as follows:

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);

}

this is button 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();
}


 }

I suggest you use AsyncTask for this. You can initiate the download in the doInBackground method and show the alert dialog (as needed) in the onPostExecute method. This will also free you from using Thread.sleep(100) which isn't a good practice on Android.

However, if you still prefer to use the FreshDownloadView please update your question with the code for FreshDownloadView and we can suggest something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM