2014-02-05 36 views
1

它,我有一個類繼承countdowntimer使土司的活動,並表示其他的一個

我想在onfinish()敬酒,每次最多顯示從其它類稱爲onfinish()函數在從它實例化的任何對象中

我該怎麼做?

package com.fawzyx.exams_countdowntimer; 

import android.os.CountDownTimer; 
import android.text.InputFilter.LengthFilter; 
import android.widget.Toast; 

public class CountDown extends CountDownTimer { 

    public CountDown(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
/* 
millisInFuture : The number of millis in the future from the call to start() until the countdown is done and onFinish() is called. 
countDownInterval : The interval along the way to receive onTick(long) callbacks. 

*/ 


    } 

    @Override 
    public void onFinish() { 
     // TODO Auto-generated method stub 

     Toast.makeText(getApplicationContext() ,"Done", Toast.LENGTH_LONG); 

    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     // TODO Auto-generated method stub 

    } 

} 
+0

使用getApplicationContext(),而不是特定的上下文 –

+2

傳遞活動場景到類的構造函數,並用它那裏顯示敬酒 – Raghunandan

+0

它給了我錯誤 – Fawzinov

回答

2

您需要上下文傳遞給CounteDown類

new CountDown(MyActivityName.this,otherparams); 

現在使用傳遞給顯示onFinish()

Toast.makeText(context," Message", Toast.LENGTH_SHORT).show(); 
1

敬酒

Context context; 
public CountDown(Context context,long millisInFuture, long countDownInterval) { 
super(millisInFuture, countDownInterval); 
this.context = context; 
} 

那麼上下文試試T.他:

public class CountDown extends CountDownTimer { 

    private Context context; 

    public CountDown(Context context, long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
     this.context = context; 
    } 
    @Override 
    public void onFinish() { 
     Toast.makeText(context ,"Done", Toast.LENGTH_LONG).show(); 

    } 
} 
1

兩個問題:

  1. 您需要在Context例如通過作爲構造ARG:

    private Context mContext; 
    
    public CountDown(Context context, long millisInFuture, long countDownInterval) { 
        super(millisInFuture, countDownInterval); 
        mContext = context; 
    

    通行證在此mContextToast

  2. Toast.makeText()之後,請致電show()實際顯示它。

1

使用方法如下:

public class CountDown extends CountDownTimer { 

    Context context; 
    public CountDown(long millisInFuture, long countDownInterval , Context ctx) { 
     super(millisInFuture, countDownInterval); 

    this.context = ctx; 

/* 
millisInFuture : The number of millis in the future from the call to start() until the countdown is done and onFinish() is called. 
countDownInterval : The interval along the way to receive onTick(long) callbacks. 

*/ 
} 

    @Override 
    public void onFinish() { 
     // TODO Auto-generated method stub 

     Toast.makeText(context ,"Done", Toast.LENGTH_LONG); 

    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     // TODO Auto-generated method stub 

    } 

} 

調用其construtor而主叫Activity背景下的地方,我們要倒計時類

0

更簡單的方法做,這是覆蓋onStop和使用getApplicationContext() 。然後,當活動結束時,敬酒將出現。

@Override 
protected void onStop() { 
    super.onStop(); 
    Toast toast = Toast.makeText(getApplicationContext(),"Toast message here",Toast.LENGTH_SHORT); 
    toast.show(); 
} 
相關問題