开发者

Weird things happening with ProgressDialog and AlertDialog

开发者 https://www.devze.com 2023-04-04 12:05 出处:网络
I am posting something to FB from my program. Works great except that I want to show a ProgressDialog before and then an AlertDialog after.

I am posting something to FB from my program. Works great except that I want to show a ProgressDialog before and then an AlertDialog after.

For some reason the ProgressDialog doesn't appear at all whereas the AlertDialog does but the execution of the code continues through and doesn't "wait".

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

}


try this class

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

   }
}


You should really be doing the post to facebook in an AsyncTask. This should fix your issues.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号