如何在android中的应用程序中运行countdowntimer?

时间:2017-10-10 17:49:19

标签: android countdowntimer

我正在尝试在我的应用程序中添加countdowntimer,我想在整个应用程序中运行计时器5分钟而没有暂停和取消以及重新启动...我尝试了几个代码但我没有得到所需的输出!

面临的问题:    当我试图从MainActivity到Main2Activity时,它工作得很好但是    如果Main2Activity为MainActivity,则计时器重启!

//MainActivity.java

public class MainActivity extends AppCompatActivity  {
    Button button;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button= (Button) findViewById(R.id.button);
        textView= (TextView) findViewById(R.id.id1);
        new CountDownTimer(300000, 1000) { // adjust the milli seconds here

            public void onTick(long millisUntilFinished) {
                textView.setText(""+String.format("%d min, %d sec",
                        TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
            }

            public void onFinish() {
                textView.setText("done!");
            }
        }.start();
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Intent in=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(in);
            }
        });
    }
}

//Main2Activity.java

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Button btn2= (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(Main2Activity.this,MainActivity.class);
                startActivity(i);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

您的Main2Activity正在启动MainActivity的新实例。如果您想要返回原始MainActivity,请使用#include <stdio.h> #include "jni.h" #include "recipeNo039_PassArray2D.h" JNIEXPORT int JNICALL Java_recipeNo039_PassArray2D_displayArray2D (JNIEnv *env, jclass obj) { /* Get objarg's class - objarg is the one we pass from Java */ jclass cls = (*env)->GetObjectClass(env, obj); /* Remember that you can alway get method signature using javap tool > javap -classpath target -s -p recipeNo039.PassArray2D | grep -A +1 getDoubleData public double[][] getDoubleData(); descriptor: ()[[D */ jmethodID mid = (*env)->GetMethodID(env, cls, "getDoubleData", "()[[D"); /* We have to make sure that method exists */ if (mid == NULL) { return -1; /* method not found */ } /* Now, it's time for getting array of arrays */ jobject objArray = (*env)->CallObjectMethod(env, obj, mid); /* We will iterate over the array so we can get into next array */ int arraySize = (*env)->GetArrayLength(env, objArray); /* We want to go over all elements (all sub arrays) */ for (int i=0; i < arraySize; i++) { jdoubleArray array = (*env)->GetObjectArrayElement(env, objArray, i); /* get size of the array */ jsize len = (*env)->GetArrayLength(env, array); /* get the body of array; it will be referecende by C pointer */ jdouble *body = (*env)->GetDoubleArrayElements(env, array, 0); /* do some stuff */ for(int p=0; p < len; p++) { printf("Double value [%d;%d]: %f\n", i, p, body[p]); } /* release body when you decide it is no longer needed */ (*env)->ReleaseDoubleArrayElements(env, array, body, 0); } return 0; } 代替finish()