I am in a situation where I need to display the Rate dialog based on the following flow
Open the app (First time )--> display dialog every 2 minutes
If rated --> display dialog next month
If clicked on later button --> display dialog next week.
String rate_value=myPref.getString("rate_value", "later");
Log.e("rate", String.valueOf(rate_value));
if (rate_value=="later") {
initCalendarNextWeek();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
showRateDialog();
}
});
}
}, nextWeekDate);
}
else if (rate_value=="now") {
initCalendarNextMonth();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
showRateDialog();
}
});
}
}, nextMonthDate);
}
else if (rate_value=="no_thanks") {
myTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
showRateDialog();
}
});
}
}, 120000, 120000);
}
public void initCalendarNextMonth(){
cal=Calendar.getInstance();
cal_day=cal.get(Calendar.DATE);
cal_month=cal.get(Calendar.MONTH);
cal_year=cal.get(Calendar.YEAR);
nextMonthDate=new Date();
nextMonthDate.setDate(cal_day);
nextMonthDate.setMonth(cal_month+1);
nextMonthDate.setYear(cal_year);
}
public void initCalendarNextWeek(){
cal=Calendar.getInstance();
cal_day=cal.get(Calendar.DATE);
cal_month=cal.get(Calendar.MONTH);
cal_year=cal.get(Calendar.YEAR);
nextWeekDate=new Date();
nextWeekDate.setDate(cal_day+7);
nextWeekDate.setMonth(cal_month);
nextWeekDate.setYear(cal_year);
}
Ok, so maybe look at Calendar class. The set methods of Date are deprecated so instead of using
nextMonthDate = new Date()
rather maybe use
nextMonthDate = new Calendar();
because then you can use the methods stated in the Calendar documentation such as add instead of using the setDate or setMonth. I hope this helps.
Related
I am just wondering when timeout occurs, onFinish() method is called and we can execute further code there. But for some reason if timer is cancelled either manually or because of error does onFinish() get called? Or it just cancels the timer without calling any method.
Here are My two Counters.
Counter1:
class Counter1 extends CountDownTimer {
public Counter1(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
Log.d(TAG, " Timer1 Finished");
//Add 2min to second timer
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
/
}
}
class Counter2 extends CountDownTimer {
public Counter2(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
Log.d(TAG, "Timer2 Finished");
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}
I have to add 2 min to second timer is its values is less than add 2minutes to its existing else it will continue with its timer.
OnFinish() will not called at timer cancel you have to start it again, You can store time value in shared preference when application get crashed you have start time from saved time.
like
CountDownTimer countDownTimerFixed = new CountDownTimer(Time, Tick) {
#Override
public void onTick(long millisUntilFinished) {
RTSharedPrefUtils.saveStringPrefernce(RTSharedPrefUtils.KEY_DOWNLOAD_TIMER, millisUntilFinished+ "");
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
RTSharedPrefUtils.saveStringPrefernce(RTSharedPrefUtils.KEY_DOWNLOAD_TIMER, 0 + "");
this.start();
}
};
if timer is canceled,
Timer=Long.parseLong(RTSharedPrefUtils.fetchStringPrefernce(RTSharedPrefUtils.KEY_DOWNLOAD_TIMER,RTSharedPrefUtils.DEFAULT_DOWNLOAD_TIMER));
countDownTimer = new CountDownTimer(Timer, Tick) {
#Override
public void onTick(long millisUntilFinished) {
RTSharedPrefUtils.saveStringPrefernce(
RTSharedPrefUtils.KEY_DOWNLOAD_TIMER,millisUntilFinished + "");
Log.d(TAG," Normalcount:\t"+ (Long.parseLong(RTSharedPrefUtils.fetchStringPrefernce(RTSharedPrefUtils.KEY_DOWNLOAD_TIMER,RTSharedPrefUtils.DEFAULT_DOWNLOAD_TIMER)))/ 1000);
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
RTSharedPrefUtils.saveStringPrefernce(
RTSharedPrefUtils.KEY_DOWNLOAD_TIMER, 0 + "");
countDownTimerFixed.start();
}
};
countDownTimer.start();
public void BetTimerFuction()
{
int delay=0;
int period=200;
betTimer = new Timer();
betTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
mHandlerBet.obtainMessage().sendToTarget();
}
}, delay, period);
}
Its my timer code but i didn't work with that timer in view class i m also using canvas .
When I use that timer in view class that timer get a number prosess is very slow when i click the button its increase the the integer two timers
I think u do the null first for your stop function.
public void StopBetTimer()
{
try
{
if(betTimer !=null)
{
betTimer.cancel();
betTimer=null;
}
}
catch(Exception e)
{
}
}
I revised you code because i think in that code no issue. But may be you didn't initialize properly to null your timer when you use first and also stop properly.
public void BetTimerFuction()
{
int delay=0;
int period=100;
betTimer = new Timer();
betTimer.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
mHandlerBet.obtainMessage().sendToTarget();
}
}, delay, period);
}
Here is for you stop function
public void StopBetTimer()
{
try
{
if(betTimer !=null)
{
betTimer.cancel();
betTimer=null;
}
}
catch(Exception e)
{
}
}
This is my code following,I am changing some iamges dynamically in my image view.
public class LoadingScreen extends Activity{
public static Integer[] imageList={R.drawable.food_pics1,R.drawable.food_pics2,R.drawable.food_pics3,
R.drawable.food_pics4,R.drawable.food_pics5,R.drawable.food_pics6,R.drawable.food_pics7,
R.drawable.food_pics8,R.drawable.food_pics9};
Thread thread;
ImageView foodImageView;
final Handler myHandler=new Handler();
public int currentImageIndex=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.load_xml);
foodImageView=(ImageView)findViewById(R.id.imageView_food);
// final int i=0;
final Runnable runnable = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
animateImages();
}
};
final int delay=500;
final long period=1000;
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
myHandler.post(runnable);
}
}, delay, period);
}
private void animateImages() {
// TODO Auto-generated method stub
foodImageView.setImageResource(imageList[currentImageIndex%imageList.length]);
currentImageIndex++;
foodImageView.getAnimation();
}
I want to stop the timer and finish this activity after 20 secs.how can I do that.
Try using this,
View v = new View(this);
v.postDelayed(new Runnable(){
public void run() {
// TODO Auto-generated method stub
//cancel your animation and finish the Activity here.
finish();
}
}, (long) 20000.0);
You can say something like this :
timer.cancel();
timer = null;
Save the TimerTask instance in the class and invoke cancel on it.
Here's a tip I found very useful without using Java's Timer: Try synchronizing things in the UI thread, especially if you want to do UI tasks. Example:
... onCreate() {
getUiThreadHandler().post(new Runnable(){
if (canceled()) {
return;
}
// Actual work
...
// Invoke again after delay
getUiThreadHandler().postDelayed(this, 500);
});
Good luck
Hey im trying to add countdown timers to an arraylist but it is crashing. It is crashing once I try to add one countdowntimer . Was wondering is there anything I could do to fix it?
ArrayList<CountDownTimer> timers;
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
toggleLock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Checker = new CountDownTimer(1200000, 60000)
{
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
//does stuff
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
Checker.start();
timers.add(Checker);
}
}
});
}
I think you need to initialize the arraylist, so might be throwing null pointer exception.
timers=new ArrayList<CountDownTimer>();
You need to initialize your timers arraylist.
ex.
ArrayList<CountDownTimer> timers = new ArrayList<CountDownTimer>();
public Button stb;
static int cnt=0;
public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>();
Timer myt;
TimerTask t;
stb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myt.schedule(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Entering run");
Handler h=new Handler();
h.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
}
});
//rg.getChildAt(cnt).setPressed(true);
}
},1000,2000);
I need to access a group of radio buttons on the ui and set it as checked at regular intervals, but i keep getting different errors, i realized i must use a handler, but its still not working...can anyone please tell me where i am going wrong....am a newbie and am trying out stuff to understand the working better...please help...
You can try to use your own Handler instead of Timer and timed taks.
RefreshHandler mHandler = new RefreshHandler();
With:
class RefreshHandler extends Handler
{
#Override
public void handleMessage(Message msg)
{
postYourElements();
}
public void sleep(long delayMillis)
{
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
}
And than use the function:
private void postYourElements()
{
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
mHandler.sleep(TimerIntervallInMs);
}
To start the Handler just call the postYourElements() function under onClick Method.
I'm not sure if this works for you but you can try.
youractivityname.this.runOnUiThread(new Runnable() {
public void run() {
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});