【问题标题】:How to start an activity from the onFinish method of a countdown?如何从倒计时的 onFinish 方法开始活动?
【发布时间】:2014-04-26 04:38:49
【问题描述】:

我是 Android 开发的新手,在这里遇到了一个问题。

我想在倒计时结束时启动一项活动(另一个班级)。

编辑:

Eclipse 告诉我代码无法运行,并在我的部分代码下划线为红色。

问题不是来自宣言之类的,我只是不知道如何实现我的其他活动的调用。

/编辑

这是我的代码:

public static void launch_countdown(final TextView tv1){
    new CountDownTimer(30000, 1000) {

         public void onTick(long millisUntilFinished) {
             tv1.setText("" + millisUntilFinished / 1000);
         }

         public void onFinish() {
             /* I want to launch my Activity here */
         }
    }.start();
} 

所以基本上,我应该用什么替换我的评论?

PS:我做了一些研究,但在我的情况下似乎不起作用:

How to call an activity from a CountDownTimer?

Android finish Activity and start another one

How to start an activity upon the completion of a timer?

【问题讨论】:

  • 解释它是如何不起作用的。你可以放一个Intent 来开始新的Activity
  • 红色下划线的代码是哪一部分?红色下划线的错误信息是什么?

标签: android android-intent


【解决方案1】:

如果您在活动中创建此CountdownTimer,您应该可以这样做:

public void onFinish() {
    Intent i = new Intent(getApplicationContext(), OtherActivity.class);
    startActivity(i);
    finish(); // if you want this activity to go away
}

(如果您愿意,可以将getApplicationContext() 替换为CurrentActivity.this。您不能使用裸露的this,因为它指的是onFinish() 所属的匿名CountDownTimer 子类的当前实例。)

如果此代码在某个其他类中,则您需要访问Context 作为第一个参数。例如,您可以使用tv1.getContext()

或者,您可以使用不需要Context 的其他Intent 构造函数之一。请参阅API docs 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多