2014-09-10 43 views
-1

這是我的MainActivity中代碼的一部分。 stopwatch1是調用MainActivity圖像按鈕:故意或意外退出對話後終止音頻

  stopwatch1 = (ImageButton)findViewById(R.id.stopwatch1); 

我試圖尋找關於如何使用dialog.setOnDismissListener線索,但是,也有不正確的答案,幫助我的聲音後故意殺害或意外退出對話框。任何幫助,將不勝感激。

  stopwatch1.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         final Dialog mmDialog1 = new Dialog(context); 
         mmDialog1.setContentView(R.layout.countdowntimer); 
         mmDialog1.setTitle("Stop-watch");    
         Button ddButton1 = (Button) mmDialog1.findViewById(R.id.countdown_exit); 
         final Button ddButton2 = (Button) mmDialog1.findViewById(R.id.countdown_start); 
         final TextView timeview = (TextView) mmDialog1.findViewById(R.id.timeview); 
         ddButton2.setTag(1); 

         ddButton2.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           final int status = (Integer) v.getTag(); 

           if (status == 1) { 
            ddButton2.setText("Stop"); 
            countDownTimer = new CountDownTimer(60000, 1000) { 

             public void onTick(long millisUntilFinished) { 
              int time = (int) (millisUntilFinished/1000); 
              timeview.setText(Integer.toString(time)); 
             } 
             @Override 
             public void onFinish() { 
              timeview.setText("60"); 
              ddButton2.setText("Start"); 
              MediaPlayer stop = MediaPlayer.create(context, R.raw.stop); 
              stop.start(); 
             } 
            }.start(); 
            v.setTag(0); 

           } else { 
            ddButton2.setText("Start"); 
            timeview.setText("60"); 
            countDownTimer.cancel(); 
            v.setTag(1); 
           } 
          } 

         }); 

         ddButton1.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           mmDialog1.dismiss(); 
          } 
         }); 

         mmDialog1.show(); 
        } 
       }); 

回答

2

我的建議是實現一個接口。當對話框關閉時,讓回撥關閉媒體播放器。

一個簡單的例子是這樣的。

接口

public interface DialogClosed { 
    void shutDownMediaPlayer(); 
} 

然後實現在調用活動的界面

活動

public class MainActivity extends Activity implements DialogClosed { 
    .... 
    @Override 
    public void shutDownMediaPlayer() { 
     //shut down media player here 
    } 
} 

此外,您可以添加一個布爾值,以檢查是否對話框顯示像這樣

boolean isShowing = mDialog.isShowing(); 

然後你可以隨時關閉媒體播放器,如果這是錯誤的。

0

所以我用從Jawascript一些指針,並編輯我的代碼一點,現在它的工作原理:

  stopwatch1.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         final Dialog mmDialog1 = new Dialog(context); 
         mmDialog1.setContentView(R.layout.countdowntimer); 
         mmDialog1.setTitle("Stop-watch");    
         Button ddButton1 = (Button) mmDialog1.findViewById(R.id.countdown_exit); 
         final Button ddButton2 = (Button) mmDialog1.findViewById(R.id.countdown_start); 
         final TextView timeview = (TextView) mmDialog1.findViewById(R.id.timeview); 
         final MediaPlayer stop = MediaPlayer.create(context, R.raw.stop); 

         ddButton2.setTag(1); 

         mmDialog1.setCancelable(false); 

         if (mmDialog1.isShowing() == false) { 
          stop.stop(); 
         } 

         ddButton2.setOnClickListener(new OnClickListener(){ 
          @Override 
          public void onClick(View v) { 
           final int status = (Integer) v.getTag(); 

           if (status == 1) { 
            ddButton2.setText("Stop"); 
            countDownTimer = new CountDownTimer(60000, 1000) { 

             public void onTick(long millisUntilFinished) { 
              int time = (int) (millisUntilFinished/1000); 
              timeview.setText(Integer.toString(time)); 
             } 
             @Override 
             public void onFinish() { 
              timeview.setText("60"); 
              ddButton2.setText("Start"); 
              try { 
               stop.prepare(); 
              } catch (IllegalStateException e) { 
               e.printStackTrace(); 
              } catch (IOException e) { 
               e.printStackTrace(); 
              } 
              stop.start(); 
             } 
            }.start(); 
            v.setTag(0); 

           } else { 
            ddButton2.setText("Start"); 
            timeview.setText("60"); 
            countDownTimer.cancel(); 
            v.setTag(1); 
            stop.stop(); 
           } 
          } 

         }); 

         ddButton1.setOnClickListener(new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           mmDialog1.dismiss(); 
          } 
         }); 

         mmDialog1.show(); 
        } 
       });