1

我在我的应用程序中显示下载文件的进度对话框,但如果用户需要取消下载,那么他将不得不按下后退按钮,然后它将弹出带有两个按钮的警报对话框。问题是我必须双击警报对话框的按钮,然后只关闭警报对话框。建议我任何解决方案。

这是和平的代码供您参考..

@Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id)
        {
        case progress_bar_type:
            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Downloading file. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(true);
            pDialog.show();
            pDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    if(keyCode == KeyEvent.KEYCODE_BACK){

                        running = false;
                        /*Intent intent = new Intent(context, NewDialog.class);
                        startActivity(intent);*/
                        AlertDialog.Builder  alertDialog = new AlertDialog.Builder(context);
                        alertDialog.setIcon(R.drawable.ic_launcher);
                        alertDialog.setTitle("Ariisto");
                        alertDialog.setMessage("Do you Want to Cancel the Download ?");
                        alertDialog.setCancelable(true);
                        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                File externalFile = new File(Environment.getExternalStorageDirectory(),"downloadedfile.pdf");
                                externalFile.delete();
                                pDialog.dismiss();
                                running = false;
                                Log.d("External File", "DELETED");
                                pDialog.setProgress(0);
                                count = 2;
                            }
                        });
                        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                new DownloadFileFromURL().execute(file_url);
                                running = true;
                                count = 0;
                            }
                        });
                        AlertDialog alert = alertDialog.create();
                        alert.show();
                    }
                    return false;
                }
            });
4

1 回答 1

1

问题是覆盖onKey()注册你Activity的两个事件,KEY_DOWN以及KEY_UP给定的键。所以碰巧你AlertDialog在这两个事件上触发了两次。我建议您覆盖该onKeyDown()方法并将您的代码移到那里。希望这可以帮助。

于 2012-11-09T05:46:52.103 回答