ProgressDialog和AlertDialog发生了奇怪的事情

时间:2011-09-12 13:59:50

标签: android alertdialog progressdialog

我正在从我的程序中向FB发帖。 效果很好,除了我想先显示一个ProgressDialog然后再显示一个AlertDialog。

出于某种原因,ProgressDialog根本没有出现,而AlertDialog却没有,但代码的执行仍在继续,并且不会“等待”。

private void uploadToFB(){
    if (settings.getCurrentUploadToFB()){

        ProgressDialog progressDialog = ProgressDialog.show(this, "Posting to Facebook", "Please wait ...", true);
        Bundle parameters = new Bundle();
        parameters.putString("link","https://market.android.com/details?id=il.co.anykey.games.yaniv.lite&feature=search_result");
        parameters.putString("picture","http://www.anykey.co.il/images/launcher_yaniv.png");
        parameters.putString("name","YanivLite on Android");
        parameters.putString("caption","Can you beat my score?");
        parameters.putString("description","I scored "+ ScoringForScoreloop.getScore() + " in YanivLite on Android. Can you beat my score?");
        facebookConnector.postMessageOnWall(parameters);

        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        alertDialog.setTitle(R.string.uploadedToFacebookTitle);
            ScoringForScoreloop.addAssafedScore();
            alertDialog.setMessage(getString(R.string.uploadedToFacebookMessage));   


        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,getString(R.string.ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {            
                   try {
                    alertDialog.dismiss();
                    finishGame();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                   return;
        } });
        alertDialog.setCancelable(false);  // prevent use of back button which would skip the listener
        alertDialog.show();
    }
    else
        finishGame();

}

2 个答案:

答案 0 :(得分:3)

试试这个课程

private void uploadToFB(){
    if (settings.getCurrentUploadToFB())
         new PostBackground.execute();
    else
         finish();
}

private class PostBackground extends AsyncTask<Void, Void, Void>{
    private ProgressDialog progressDialog;
    public void doInBackgound(Void...obj){
         publishUpdate();
         Bundle parameters = new Bundle();
        parameters.putString("link","https://market.android.com/details?id=il.co.anykey.games.yaniv.lite&feature=search_result");
        parameters.putString("picture","http://www.anykey.co.il/images/launcher_yaniv.png");
        parameters.putString("name","YanivLite on Android");
        parameters.putString("caption","Can you beat my score?");
        parameters.putString("description","I scored "+ ScoringForScoreloop.getScore() + " in YanivLite on Android. Can you beat my score?");
        facebookConnector.postMessageOnWall(parameters);

    }

    public void postUpdate(Void arg){
        progressDialog = ProgressDialog.show(this, "Posting to Facebook", "Please wait ...", true);
    }

   public void postExecute(Void result){
        if(progressDialog!=null)
              progressDialog.dismiss();

        progressDialog = null;
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    alertDialog.setTitle(R.string.uploadedToFacebookTitle);
        ScoringForScoreloop.addAssafedScore();
        alertDialog.setMessage(getString(R.string.uploadedToFacebookMessage));   


    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,getString(R.string.ok), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {            
               try {
                alertDialog.dismiss();
                finishGame();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
               return;
    } });
    alertDialog.setCancelable(false);  // prevent use of back button which would skip the listener
    alertDialog.show();

   }
}

答案 1 :(得分:0)

你应该在AsyncTask中发帖到facebook。这应该可以解决您的问题。