Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android倒计时,新活动继续计时_Java_Android - Fatal编程技术网

Java Android倒计时,新活动继续计时

Java Android倒计时,新活动继续计时,java,android,Java,Android,我已经创建了一个倒计时,它可以正常工作,但每次活动都会重新开始。我试图保存倒计时值并在第二个活动中使用它。下面是我的主要活动页面上的倒计时代码 //Countdown Start new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { final int j = (int) millisUntilFinished / 1000;

我已经创建了一个倒计时,它可以正常工作,但每次活动都会重新开始。我试图保存倒计时值并在第二个活动中使用它。下面是我的主要活动页面上的倒计时代码

    //Countdown Start


    new CountDownTimer(10000, 1000) {


        public void onTick(long millisUntilFinished) {
        final int j = (int) millisUntilFinished / 1000;

        long timed;
        timed = millisUntilFinished;




            TextView textic = (TextView) findViewById(R.id.textView2);
            textic.setText("" + (millisUntilFinished));



            runOnUiThread(new Runnable() {
              @Override
                public void run() {
                TextView textic = (TextView) findViewById(R.id.textView2);
                textic.setText(Integer.toString((j)));;


                }
            });





            }


            public void onFinish() {        
            }
        }.start();

//Countdown Finish
我曾尝试存储倒计时值,然后使用passextra传递“timed”,但没有成功。我四处搜索,找不到任何有效的解决方案。任何帮助都将不胜感激

谢谢

================

编辑

================

公共类MainActivity扩展了startscreen{

private CountDownTimer timer3;
public void onCreate(){

    timer3 = new CountDownTimer(10000,100){

        public void onTick(long millisUntilFinished) {

            TextView displaytime = (TextView) findViewById(R.id.textView3);
            displaytime.setText("" + (millisUntilFinished));}

         public void onFinish() {
         }

      }.start();




        };;

public void startTimer(){

}

public void cancelTimer(){

}
==============

编辑2 在我的第二个活动中,我得到:

    MainActivity Counter1 = (MainActivity) getApplicationContext();
    Counter1.starttimer();

    TextView mCounter1TextField=(TextView)findViewById(R.id.textView4);
    mCounter1TextField.setText("Seconds left: " + (Counter1));

我建议要么在自己的类中编写
倒计时程序
,并将其扩展为
服务
,然后在需要启动计时器时启动该服务,要么将其设置为单例,以便所有活动都访问同一计时器。

扩展该类,并在其中添加全局计时器。您的类将如下所示

 public class MyApplication extends Application{
    private CountDownTimer timer;
    public void onCreate(){
        super.onCreate();
        timer = new CountDownTimer(10000,100){
                .....
            };
    }
    public void startTimer(){
        ...
    }

    public void cancelTimer(){
        ...
    }
}
通过从活动调用
getApplicationContext()启动此计时器

MyApplication application = (MyApplication) getApplicationContext();
application.startTimer();
另外,确保在Android清单中重命名应用程序

<application ... android:name=".MyApplication" .../>


在我的其他活动中如何调用此计时器?谢谢。我发布了新代码。我尝试显示计时器,但每次都停止响应。我编辑了代码。我尝试在它自己的类中创建它,但倒计时不起作用,它只显示固定值10。我仍在研究如何创建sin格雷顿。我对安卓系统还比较陌生。谢谢你的建议。
<application ... android:name=".MyApplication" .../>