2011-05-02 84 views
0

正在創建一個簡單的應用程序。應用程序必須顯示對話框,在後臺運行計時器。計時器過後,應該發出嘟嘟聲。但是如果在定時器之前點擊對話框,定時器應該被取消。我發佈了迄今爲止所做的,使用此代碼,首先獲取嗶聲,然後是對話框,如何向主線程說明計時器已完成?如何迴應主線程?

公共類主要活動擴展{

static final int DIALOG_ADDPLAYERS = 0; 

Thread backgroundThread; 
TextView myText; 
myCounter counter; 
MediaPlayer mp; 

Handler handler = new Handler(){ 

    @Override 
    public void handleMessage(Message msg) 
    { 
      playAudio(); 
    } 

}; 

protected Dialog onCreateDialog(int id) 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 

     switch(id) 
     { 
     case 0: 

       builder.setTitle("Restart Game"); 
       builder.setMessage("Are you sure to restart the game?"); 
       builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) 
        { 
         counter.cancel(); 
        }}); 
       builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) 
        { 
         counter.cancel(); 
        } 
       }); 


      break; 


     default: 
     } 
     return builder.create();  
    } 

void playAudio() 
{ 
    mp = MediaPlayer.create(this, R.raw.beep); 
    mp.setLooping(false); 
    mp.start(); 
    mp.setOnCompletionListener(new OnCompletionListener(){ 
     public void onCompletion(MediaPlayer arg0) 
     { 
      finish(); 
     } 
    }); 
    mp.setOnCompletionListener(new OnCompletionListener(){ 
     public void onCompletion(MediaPlayer arg0) 
     { 
      finish(); 
     }}); 

} 

@Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    if (mp!=null) 
    { 
     mp.release(); 
     mp=null; 
    } 
} 



/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    myText = (TextView)findViewById(R.id.mytext); 

    //Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();  
    showDialog(0); 
    startMyTimer(); 
} 




protected void startMyTimer() 
{  
    counter = new myCounter((long)10000,(long)1000); 
    backgroundThread = new Thread(new Runnable(){ 

     @Override 
     public void run() 
     {  
       try 
       { 
        //Thread.sleep(10000); 


        counter.startTimer(); 

       } 
       catch (Exception e) { 

        e.printStackTrace(); 
       } 


     } 

    }); 
    backgroundThread.start(); 
} 

}

這是我MyCounter類:

package com.androidbook.bgthread; 

import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnCompletionListener; 
import android.os.CountDownTimer; 

public class myCounter extends CountDownTimer 
{ 
    public myCounter(long millisInFuture, long countDownInterval) 
    { 
     super(millisInFuture, countDownInterval); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onFinish() 
    { 

    } 

    @Override 
    public void onTick(long millisUntilFinished) 
    { 


    } 

    public void startTimer() 
    { 
     start(); 
    } 

} 

這是做正確的方式?請幫忙。

回答

1

使用處理程序:http://developer.android.com/reference/android/os/Handler.html。這使您可以從任何其他線程向主線程發送消息。

一個有用的參考是在http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

+0

我沒有明確地使用倒數計時器? Alsi當時間到了,我必須發出嗶嗶聲。我怎麼做? – Madz 2011-05-02 00:36:43

+0

您可以使用倒數計時器:沒有什麼特別的錯誤。至於嘟嘟聲不確定:還沒有嘗試過。 – Femi 2011-05-02 00:41:22