从扩展的CountDownTimer类调用一个Activity

时间:2017-03-08 17:37:22

标签: android countdowntimer

我在45分钟内有一个CountDownTimer扩展课程。 我希望在Activity完成后(CountDownTimer),在用户所在的任何地方(onFinishActivity)拨打新的Fragment

我该怎么做?

这是我的Counter类:

    public class Counter  extends CountDownTimer {

    private static long millis;
    private static Counter instance;

    private Counter(long millisInFuture, long countDownInterval){
        super(millisInFuture,countDownInterval);
    }

    public static Counter initInstance(){
        instance =  new Counter(2700000, 1000);
        return instance;
    }

    public static Counter  getInstance() throws Exception{
        if(instance==null){
            throw new Exception("Parameters not initialized. Initiate with initInstance");
        } else{
            return instance;
        }
    }

    public boolean isFinish(){
        return millis <= 0;
    }


    @Override
    public void onTick(long l) {
        millis = l;
    }

    @Override
    public void onFinish() {
        // THIS IS WHERE I WANT TO CALL A NEW ACTIVITY !
    }
}

我这样打电话给我的柜台:

Counter counterClass = Counter.initInstance();
counterClass.start();

1 个答案:

答案 0 :(得分:1)

您只需要向计数器的构造函数发送一个Context,将其作为属性保存在您的计数器中,然后像这样调用您的计数器:

Counter counterClass = Counter.initInstance(getApplicationContext());
counterClass.start();

然后在你的结束时你可以正常开始一项活动:

context.startActivity()