我有一个30秒的倒数计时器,它会在应用程序打开时启动。
每次此计时器开始和结束时,我都想显示新的活动。
我怎么能这样做?
答案 0 :(得分:0)
首先开始Timer
30秒。
计时器的Onfinish
方法更新shared prefferences
中完成计时器的次数。
这是完整的代码。
首先定义
public static final String MyPREFERENCES = "MyPrefs" ;
SharedPreferences sharedpreferences;
并在activity
或fragment
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
现在制作你的计时器
int oneMin= 30 * 1000; // 30 seconds in milli seconds
/** CountDownTimer starts with 30 seconds and every onTick is 1 second */
CountDownTimer cdt = new CountDownTimer(oneMin, 1000) {
public void onTick(long millisUntilFinished) {
Log.e("timer","time");
// this method is invoke in every 1 second (1000 milli second )
}
public void onFinish() {
// you can also check here how many times this timer have been finished in past
// this method will invoke after completion that is 30 seconds
Intent intent = new Intent (context, NewActivity.class);
context.startActivity(intent);
// you can save in SHARED PREFFERENCES and update how many times this method is invoked.
SharedPreferences.Editor editor = sharedpreferences.edit();
int times = sharedpreferences.getInt("times",0);
editor.putInt("times",times);
editor.commit();
}
}.start();
答案 1 :(得分:0)
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
//start new activity here
Intent intent=new Intent(getApplicationContext(),NewActivityName.class);
startActivity(intent)
}
}.start();
答案 2 :(得分:0)
为此你应该有两个Activities
MainActivity:启动器活动
TempActivity: Temp Activity 30秒
默认情况下, TimeFlag
为false,因此您将直接发送到TempActivity
所在的位置30秒,然后TimeFlag
将变为真,并且MainActivity
设置内容。< / p>
<强> MainActivity:强>
public class MainActivity extends Activity {
public static boolean TimeFlag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
if (!TimeFlag) {
Intent intent = new Intent(this, TempActivity.class);
startActivity(intent);
} else {
setContentView(R.layout.activity_main);
// your other task here below
}
}
}
<强> TempActivity:强>
public class TempActivity extends Activity {
public static final int Tick = 1000;
public static final int Complete = 30000; // 30 seconds
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp);
CountDownTimer timmer = new CountDownTimer(Complete, Tick) {
public void onTick(long millisUntilFinished) {
Log.e("timer", "time");
// this method is invoke in every 1 second (1000 milli second )
}
public void onFinish() {
MainActivity.TimeFlag = true;
finish();
}
}.start();
}
}