Question

Last day I tried to create a countdown for 77 days left. I did it, but the problem is that my countdown is working with milliseconds, and 77 days in milliseconds is exactly 6652800000, and the problem is that if I set my countdown with this value, it shows an error which says: The literal 6652800000 of type int is out of range. How to set it correctly to countdown from the present day to 77 days in the future? Thanks !

Here is my:

MainActivity:

package com.example.dasds;


import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;


public class MainActivity extends Activity {
TextView tv; //textview to display the countdown
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
//5000 is the starting number (in milliseconds)
//1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(6652800000,86400000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("Done");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("Left: " + millisUntilFinished/86400000);
}
}
}
Was it helpful?

Solution

Type L right after number literal to cast it to long:

MyCount counter = new MyCount(6652800000L,86400000L);

OTHER TIPS

try to cast the parameter values to long while calling MYCount constructor.

int year = Integer.parseInt(year);
int month = Integer.parseInt(monthafter77days);
int day = 
Integer.parseInt(dateafter77days);
int hour = Integer.parseInt(hourneeded if );
int minute = Integer.parseInt(minuteneeded if);
GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute);
/**
* Converting the date and time in to
* milliseconds elapsed since epoch
*/
long alarm_time = calendar.getTimeInMillis();
schedulealarm(alarm_time );

enjoy

try changing you onTick method as following. Android textviews tends to mess about when passed double and long values.

 public void onTick(long millisUntilFinished) {
    tv.setText("Left: " + String.valueof(millisUntilFinished/86400000));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top