2016-03-31 8 views
0

Androidで行える一連のダイアログメニューを作成して、AndroidでUSSDのやりとりを模擬しようとしています。今、(on hereを手伝ったユーザーのおかげで)私はダイアログの間に、 "USSD code running ..."という進行状況ダイアログが表示され、各ダイアログ間で2秒間実行されるようにしました。しかし、私の問題は、実行後に進行ダイアログをキャンセルできないことです。さて、私がダイアログを終了したら、まだ開いて実行中のプログレスダイアログのいくつかのインスタンスがあります。私はprogress.cancel()コマンドをどこかのコードに含める必要があることを知っていますが、私が試してみるところは進捗ダイアログがすべて実行されないようです。ダイアログ間で実行した後でProgressDialogをキャンセルします

Runnable progressRunnable; 

public void FirstScreen() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    // Get the layout inflater 
    LayoutInflater inflater = this.getLayoutInflater(); 

    // Inflate and set the layout for the dialog; pass null as the parent view because its going in the dialog layout 
    final View dialogView = inflater.inflate(R.layout.number_response_dialog, null); 
    builder.setView(dialogView) 
      .setMessage(R.string.MainMenuText) 
        // Add action buttons 
      .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        // Send number to next dialog 
        String choice = getMenuChoice(dialogView); 
        if (choice.equals("5")) { 
         //Go to FirstTimeUse menu 
         progressRunnable = new Runnable() { 

          @Override 
          public void run() { 
           FirstTimeUse(); 
          } 
         }; 
         USSDprogressDialog(progressRunnable); 
        } else if (choice.equals("6")) { 
         //Something else 
        } else { 
         //Do nothing 
         dialog.dismiss(); 
        } 
       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // End session 
        dialog.dismiss(); 
       } 
      }); 
    AlertDialog dialog = builder.create(); 
    dialog.show(); 
} 

そして、実際の進行状況ダイアログヘルパーメソッド:私はUSSDProgressDialogが実行されたときにUSSDProgressDialog方法の終わりにだけでなく、すべてのダイアログ内を含むしようとした

public void USSDprogressDialog(Runnable runnable) { 
    final ProgressDialog progress = new ProgressDialog(this); 
    progress.setMessage("USSD code running..."); 
    progress.show(); 

    Handler handler = new Handler(); 
    handler.postDelayed(runnable, 2000); 
} 

。タイマーがなくなったたびにキャンセルするように他にどこに入れることができますか?ありがとうございました!

+0

どのタイマーに通知していますか? –

+0

これはUSSDProgressDialogメソッドにあるもので、実行時を2秒間遅らせるハンドラです。 (実際の "タイマー"ではなく動作します) – notchopra

答えて

0
final ProgressDialog progress = new ProgressDialog(this); 

あなたはそれが活動の他のすべての機能からアクセスできるようにUSSDprogressDialog関数の外進捗対話の宣言を配置する必要があります。進捗ダイアログをグローバルに初期化します。

今すぐポジティブボタンのonClickListenerからこのサンプルコードを試してみてください。

if (choice.equals("5")) { 
    //Go to FirstTimeUse menu 
    progressRunnable = new Runnable() { 
     @Override 
     public void run() { 
      FirstTimeUse(); 
      // Now cancel the progress dialog here. 
      if(progress != null) progress.cancel(); 
     } 
    }; 

    USSDprogressDialog(progressRunnable); 
} 
関連する問題