我正在做一個聖誕節倒計時應用程序,我有應用程序工作的基礎。它倒數到代碼中的指定日期。我想要做的是每當計時器結束時,我想將日期設置到下一年,並再次開始時間,所有這些都不需要用戶做任何事情。連續CountDownTimer循環
我嘗試了一些像循環和cdt.start();
在onFinish()
方法中的東西,但似乎都沒有工作。
如果有人能指導我朝着正確的方向,那將是非常感謝。
感謝
MainActivity.java
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.parse.Parse;
import com.parse.ParseInstallation;
public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the TextView for the countdown
final TextView tv_countdown = (TextView) findViewById(R.id.countdown_text_view);
//Set the Calendar Date and Time
Calendar start_calendar = Calendar.getInstance();
final Calendar end_calendar = Calendar.getInstance();
end_calendar.set(2016, 11, 25, 24, 0, 0);// 10 = November, month start at 0 = January
long start_millis = start_calendar.getTimeInMillis(); //get the start time in milliseconds
long end_millis = end_calendar.getTimeInMillis(); //get the end time in milliseconds
long total_millis = (end_millis - start_millis); //total time in milliseconds
//Convert the time from milliseconds to the correct time specified
//1000 = 1 second interval
CountDownTimer cdt = new CountDownTimer(total_millis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
millisUntilFinished -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
millisUntilFinished -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
millisUntilFinished -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
//Set the TextVew output
tv_countdown.setText(days + " Days" + "\n" + hours + " Hours" + "\n" + minutes + " Minutes" + "\n" + seconds + " Seconds"); //You can compute the millisUntilFinished on hours/minutes/seconds
}
@Override
public void onFinish() {
end_calendar.set(2017, 11, 25, 24, 0, 0);
}
};
cdt.start();
你知道你不應該發佈你的解析鍵嗎? – tynn
謝謝你提醒我@tynn !! –
它並沒有真正消失。這裏的一切都得到修訂。您可能想要創建一個新的密鑰。 – tynn