Executing something each X second is not exact - android

I'm testing something and I want print out execution time with this code:
private Runnable runnerLog = new Runnable() {
#Override
public void run() {
String timeStamp = new SimpleDateFormat("HH:mm:ss yyyy-dd-MM").format(Calendar.getInstance().getTime());
System.out.println("Current time: " + timeStamp);
mHandlerLogger.postDelayed(runnerLog, mInterval);
}};
The result should be like:
13:45:10
13:45:12
13:45:14
13:45:16
and so on...
but i noticed that sometimes is not difference 2 seconds, but three:
13:45:10
13:45:12
13:45:15
13:45:17
13:45:19
13:45:21
13:45:24
What is the reason for this situation? Is there better solution for executing something each X seconds?

try this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread myThread = null;
Runnable runnable = new CountDownRunner();
myThread= new Thread(runnable);
myThread.start();
}
public void doWork() {
runOnUiThread(new Runnable() {
public void run() {
try{
TextView txtCurrentTime= (TextView)findViewById(R.id.lbltime);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":" + minutes + ":" + seconds;
txtCurrentTime.setText(curTime);
}catch (Exception e) {}
}
});
}
class CountDownRunner implements Runnable{
// #Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
try {
doWork();
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}catch(Exception e){
}
}
}
}

please try this:
private Handler myhandler ;
private long RETRY_TIME = xx;
private long START_TIME = yy;
//oncreate
myhandler= new Handler();
myhandler.postDelayed(myRunnable, START_TIME);
//your runnable class
private Runnable myRunnable = new Runnable() {
public void run() {
//doing something
myhandler.postDelayed(myRunnable, RETRY_TIME);
}
};

Related

Android thread error

The thread in my application is giving a weird error on the myThread.start() function specifically the start is wrong the error is "Cannot resolve symbol start" . initially i thought it was a bracket issue but when i adjust them it ends up breaking the OnClicklistner. The thread is suppose just suppose to make the user wait before doing a set text and then the following button will take the user to a website. Can any one see my error?
package com.example.trap.york_hw6;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
public class Main3Activity extends AppCompatActivity implements View.OnClickListener {
Button Cheat;
Button NextPage3;
WebView browser3;
#SuppressLint("HandlerLeak")
Handler handler = new Handler() {
public void handleMessage(Message msg) {
TextView statement = (TextView) findViewById(R.id.snitch);
statement.setText("Checking to see if your a cop");
}
;
};
#SuppressLint("HandlerLeak")
Handler handler2 = new Handler() {
public void handleMessage(Message msg) {
browser3.loadUrl("https://www.youtube.com");
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
browser3 = (WebView) findViewById(R.id.webkit3);
browser3.getSettings().setJavaScriptEnabled(true);
browser3.getSettings().setDomStorageEnabled(true);
Cheat = (Button) findViewById(R.id.CC);
NextPage3 = (Button) findViewById(R.id.Next3);
NextPage3.setOnClickListener(this);
Cheat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Runnable r = new Runnable() {
public void run() {
long futureTime = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < futureTime) {
synchronized (this) {
try {
wait(futureTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
handler.sendEmptyMessage(0);
}
;
Thread myThread = new Thread(r);
myThread.start();
};
}
});
final Runnable r2 = new Runnable() {
public void run() {
long futureTime = System.currentTimeMillis() + 7000;
while (System.currentTimeMillis() < futureTime) {
synchronized (this) {
try {
wait(futureTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
handler.sendEmptyMessage(0);
}
;
Thread myThread2 = new Thread(r2);
myThread2.start();
};
}
public void onClick(View v) {
Intent i = new Intent(this, Main4Activity.class);
i.putExtra("value", "I got this from MainActivity2");
i.putExtra("value1", 5);
startActivityForResult(i, 3);
}
}
You are starting thread inside wrong block. Try the code below .
final Runnable r2 = new Runnable() {
public void run() {
long futureTime = System.currentTimeMillis() + 7000;
while (System.currentTimeMillis() < futureTime) {
synchronized (this) {
try {
wait(futureTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
handler.sendEmptyMessage(0);
};
};
Thread myThread2 = new Thread(r2);
myThread2.start();
AND
Cheat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Runnable r = new Runnable() {
public void run() {
long futureTime = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < futureTime) {
synchronized (this) {
try {
wait(futureTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
handler.sendEmptyMessage(0);
};
};
Thread myThread = new Thread(r);
myThread.start();
}
});
You can not initialise and start Runnable inside its block.
You can write like this.
Cheat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Runnable r = new Runnable() {
public void run() {
long futureTime = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < futureTime) {
synchronized (this) {
try {
wait(futureTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
handler.sendEmptyMessage(0);
}
};
Thread myThread = new Thread(r);
myThread.start();
}
});
and second one
final Runnable r2 = new Runnable() {
public void run() {
long futureTime = System.currentTimeMillis() + 7000;
while (System.currentTimeMillis() < futureTime) {
synchronized (this) {
try {
wait(futureTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
handler.sendEmptyMessage(0);
}
};
Thread myThread2 = new Thread(r2);
myThread2.start();
Suggestion : Also i seen your onClick method, i suggest you check clicked button's id before doing any action like. Because if you have multiple buttons having OnClickListener referring to current class, you will need to check.
public void onClick(View v) {
switch (v.getId()){
case R.id.Next3:
Intent i = new Intent(this, Main4Activity.class);
i.putExtra("value", "I got this from MainActivity2");
i.putExtra("value1", 5);
startActivityForResult(i, 3);
break;
}
}

android, disabling the save button for one hour

i am trying to disable the save button for exactly 1 hour, before the user can save data again. I have come up with some code but since i am still really new to programming i am kind of stuck and any help would be appritiated.
The problem is the button does not lock when i click save.
public void AddData(){
gumb_poslji.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Activity activity = getActivity();
Calendar calander = Calendar.getInstance();
Integer cDay = calander.get(Calendar.DAY_OF_MONTH);
Integer cHour = calander.get(Calendar.HOUR_OF_DAY);
Integer cDayOfTheWeek = calander.get(Calendar.DAY_OF_WEEK)-1;
Integer cMonth = calander.get(Calendar.MONTH)+1;
int seekBarProgressValue = seekBar.getProgress();
String strI = String.valueOf(seekBarProgressValue);
String poslji_data = strI;
boolean pregled_vnosa = myDb.insertData(poslji_data,cHour,cDay,cDayOfTheWeek,cMonth);
if (pregled_vnosa==true)
Toast.makeText(activity, "Vnos uspešen",Toast.LENGTH_LONG).show();
else
Toast.makeText(activity, "Vnos neuspešen!!!",Toast.LENGTH_LONG).show();
}
});
gumb_poslji.setEnabled(true);
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3600000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
gumb_poslji.setEnabled(false);
}
});
}
}).start();
}
Handler mHandler= new Handler();
static Runnable mUpdateTimeTask = new Runnable() {
public void run() {
gumb_poslji.setEnabled(false);
mHandler= .removeCallbacks(mUpdateTimeTask);
}
};
mHandler.postDelayed(this, 3600000);

Toast every X seconds

I have a function that gets executed every 0.~2 seconds (due to lag). However, I would like to perform a toast every 5 seconds. May i know how I can achieve this ?
public void navigation(Coordinate userPosition , Coordinate destination){
if (Math.abs(userPosition.y - destination.y) > 500) {
{Toast.makeText(this, "Walk for " + Math.abs(CheckPoint.y - UserPosition.y) + "mm", Toast.LENGTH_SHORT).show(); }
}
Above is a sample of my current code. The frequency of which the toast gets executed is dependent on the current 'lag'. I would like the toast to be sent every 5 seconds minimum.
You can do it this way:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
navigation(...);
handler.postDelayed(this, 5000);
}
}, 5000);
try this for exactly five second,
int count = 100; //Declare as inatance variable
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
final Toast toast = Toast.makeText(
getApplicationContext(), --count + "",
Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
toast.cancel();
}
}, 5000);
}
});
}
}, 0, 5000);
Create a Thread and loop that thread
Thread myThread = null;
Runnable runnable = new CountDownRunner();
myThread = new Thread( runnable );
myThread.start();
class CountDownRunner implements Runnable
{
// #Override
public void run()
{
while( !Thread.currentThread().isInterrupted() )
{
try
{
navigation(); // do the work here
Thread.sleep( 2000 ); // time interval for the counter. it will run every 2 sec
}
catch( InterruptedException e )
{
Thread.currentThread().interrupt();
}
catch( Exception e )
{
}
}
}
}
public void navigation(Coordinate userPosition , Coordinate destination){
if (Math.abs(userPosition.y - destination.y) > 500) {
{Toast.makeText(this, "Walk for " + Math.abs(CheckPoint.y - UserPosition.y) + "mm", Toast.LENGTH_SHORT).show(); }
}
Maybe with a loop Thread? Try it out:
private Thread loopThread = new Thread(new Runnable() {
public void run() {
while(true){
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(this, "Your text!", Toast.LENGTH_SHORT).show();
});
Thread.Sleep(5000); //Wait 5 seconds, then repeat!
}catch (Exception e) {
return;
}catch (InterruptedException i) {
return;
}
}
}
});
Then start the Thread wherever you want like:
Thread myThread = new Thread(loopThread);
myThread.start();
And stop it with:
myThread.interrupt();
Hope it helps!

How to set Delay for loop in android?

I am trying to display a multiplication table with delay. My code is working fine but I am not able to implement the delay.
Here is My code:
tableButton1 = (Button) findViewById(R.id.Button1);
tableButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
str = tableButton1.getText().toString();
a = Integer.parseInt(str);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10; i++){
sb.append(a + " x " + i + " = " + i * a+ "\n");
}
s = String.valueOf(sb);
Intent intent=new Intent(getApplicationContext(),TextViewActivity.class);
intent.putExtra("MA", s);
startActivity(intent);
}
});
//Toast.makeText(getApplicationContext(), "Hi" +ss, 222).show();
}
});
Any answer is appreciable.
Thank's in advance
Updated-code:- This code is working with help of #theLittleNaruto
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TestActivity extends Activity{
Button tableButton1;
TextView txtView;
int value = 0;
static int count = 0;
Handler handle = new Handler();
StringBuilder sb = new StringBuilder();
Runnable r = new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "onrunnable" +sb, 222).show();
// TODO Auto-generated method stub
updateTable();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_display);
Toast.makeText(getApplicationContext(), "oncreate" , 222).show();
txtView = (TextView) findViewById(R.id.outputTXT);
tableButton1 = (Button) findViewById(R.id.seven);
tableButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "onclick" , 222).show();
value= Integer.parseInt(tableButton1.getText().toString());
updateTable();
}
});
}
public void updateTable(){
count+=1000;
if(count==11000){
//Toast.makeText(getApplicationContext(), "onupdate" , 222).show();
count = 0;
value=0;
handle.removeCallbacks(r);
sb.setLength(0);
}else{
sb.append(value + " x " + count/1000 + " = " + count/1000 * value+ "\n");
handle.postDelayed(r, 1000);
// Toast.makeText(getApplicationContext(), "onupdateElse" +sb, 222).show();
txtView.setText(sb);
}
}
}
Thank you all the supporters and their best try to help me
Why dont you try what the other is saying with this little effort ;)
public class TestActivity extends Activity{
int value = 0;
static int count = 0;
Handler handle = new Handler();
StringBuilder sb = new StringBuilder();
Runnable r = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
updateTable();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oaot_get);
tableButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
value= Integer.parseInt(tableButton1.getText().toString());
updateTable();
}
});
}
public void updateTable(){
count+=1000;
if(count==11000){
count = 0;
value=0;
handle.removeCallbacks(r);
sb.setLength(0);
}else{
sb.append(value + " x " + count/1000 + " = " + count/1000 * value+ "\n");
handle.postDelayed(r, 1000);
}
}
}
Try this
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
str = tableButton1.getText().toString();
a = Integer.parseInt(str);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10; i++){
sb.append(a + " x " + i + " = " + i * a+ "\n");
}
}, 5000);
s = String.valueOf(sb);
Intent intent=new Intent(getApplicationContext(),TextViewActivity.class);
intent.putExtra("MA", s);
startActivity(intent);
Add a handler(). Replace your onClick code with:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
str = tableButton1.getText().toString();
a = Integer.parseInt(str);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10; i++)
{
sb.append(a + " x " + i + " = " + i * a+ "\n");
}s=String.valueOf(sb);
Intent intent=new Intent(getApplicationContext(),TextViewActivity.class);
intent.putExtra("MA", s);
startActivity(intent);
}
}, 5000);
Repalce 5000 with the time you want it to be delayed for in milliseconds
Add a Handler that will execute your runnable:
Runnable runnable = new Runnable() {
#Override
public void run() {
str = tableButton1.getText().toString();
a = Integer.parseInt(str);
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10; i++){
sb.append(a + " x " + i + " = " + i * a+ "\n");
//--ADDED stuff here------------------------------------------------------------
try {
//Sleep will suspend your Thread for 500 miliseconds and resumes afterwards
Thread.sleep(500);
} catch (InterruptedException e) {
Log.e("error, Thread interrupted", e);
}
}
s = String.valueOf(sb);
Intent intent=new Intent(getApplicationContext(),TextViewActivity.class);
intent.putExtra("MA", s);
startActivity(intent);
}
Handler handler = new Handler();
//this will execute your runnable after 500 milliseconds
handler.postDelayed(runnable, 500);
You can use that :
public class Scheduler {
// DATA
private OnScheduleTimeListener mListener;
private Handler mHandler;
private int mInterval; // Between each executions
private static final int DELAY = 100; // before first execution
private boolean mIsTimerRunning;
public static interface OnScheduleTimeListener {
public void onScheduleTime();
}
public Scheduler(int interval) {
super();
mInterval = interval;
mHandler = new Handler();
}
private final Runnable mRunnable = new Runnable() {
#Override
public void run() {
// Do stuff
mListener.onScheduleTime();
// Repeat
mHandler.postDelayed(mRunnable, mInterval);
}
};
public void setOnScheduleTimeListener(OnScheduleTimeListener listener) {
mListener = listener;
}
public void startTimer() {
mIsTimerRunning = true;
mHandler.postDelayed(mRunnable, DELAY);
}
public void stopTimer() {
mIsTimerRunning = false;
mHandler.removeCallbacks(mRunnable);
}
public boolean isTimerRunning() {
return mIsTimerRunning;
}
}
Now to use it :
private void startTimer() {
mScheduler = new Scheduler(INTERVAL);
mScheduler.setOnScheduleTimeListener(new OnScheduleTimeListener() {
#Override
public void onScheduleTime() {
Log.d(TAG, "update");
});
mScheduler.startTimer();
}
private void stopTimer(){
if (mScheduler != null && mScheduler.isTimerRunning()) {
mScheduler.stopTimer();
mScheduler = null;
}
}
Try this....
do {
try {
try {
response_req_sequence = SimpleHttpClient
.sendresponseSequReqRes(response_send_order);
System.out.println("response of sequence request"
+ response_req_sequence);
System.out.println(" i ma in thread");
if (response_req_sequence.trim().length() != 0) {
System.out.println("response in result of sequ"+ response_req_sequence);
break;
}
Thread.sleep(10000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
}
} while (response_req_sequence.trim().equalsIgnoreCase(""));
This is working fine for me. you can customize according to you.

Start Timer on activity create method android

i need to display message to application user when admin pushes message on browser. for that i implemented a timer so that it displays a message to user on application start. timer keeps running to get as message once in 20 minutes if a new message is pushed. my timer is working fine but on button click.
I want my timer to start as soon as activity loads.
Is this proper way to display a message? (it is like banner)
How resource consuming is a timer?
Timer Task
class secondTask extends TimerTask {
#Override
public void run() {
TestBannerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
fl.setVisibility(View.VISIBLE);
long millis = System.currentTimeMillis() - starttime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
text2.setText(String.format("%d:%02d", minutes,
seconds));
}
});
}
};
button click event
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().equals("stop")) {
timer.cancel();
timer.purge();
b.setText("start");
} else {
starttime = System.currentTimeMillis();
timer = new Timer();
timer.schedule(new secondTask(), 8000, 1200000);
b.setText("stop");
}
}
});
you can use this code:
package packagename.timerService;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
public class TimerService extends Service{
public static final String BROADCAST_TIMER_ACTION = "packagename.timerService.TimerService";
private final Handler handler = new Handler();
private final Handler updateUIHandler = new Handler();
Intent intent;
int time = 0;
private int durationTime = 0;
private int starDate;
private int currentDate;
private Date startTaskDate;
private String taskComment;
#Override
public void onCreate() {
// Called on service created
intent = new Intent(BROADCAST_TIMER_ACTION);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
handler.removeCallbacks(sendUpdatesToUI);
handler.post(sendUpdatesToUI); //post(sendUpdatesToUI);
starDate = Calendar.getInstance().get(Calendar.DATE);
durationTime = 0;
startTaskDate = new Date();
}
} catch (Exception e) {}
return START_STICKY;
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
try{
displayLoggingInfo();
time ++;
durationTime ++;
handler.postDelayed(this, 60 * 1000); // 1 minute
}catch (Exception e) { }
}
};
private Runnable sendUpdatesToUIOnResume = new Runnable() {
public void run() {
displayLoggingInfoForOnResume();
}
};
private void displayLoggingInfoForOnResume() {
try{
currentDate = Calendar.getInstance().get(Calendar.DATE);
intent.putExtra("changeDate", String.valueOf(false));
intent.putExtra("time", String.valueOf(time == 0 ? time : time - 1 ));
sendBroadcast(intent);
} catch (Exception e) { }
}
private void displayLoggingInfo() {
try{
currentDate = Calendar.getInstance().get(Calendar.DATE);
intent.putExtra("changeDate", String.valueOf(false));
intent.putExtra("durationTime", String.valueOf(durationTime));
intent.putExtra("time", String.valueOf(time));
sendBroadcast(intent);
}catch (Exception e) {
// TODO: handle exception
}
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
try {
handler.removeCallbacks(sendUpdatesToUI);
updateUIHandler.removeCallbacks(sendUpdatesToUIOnResume);
durationTime = 0;
time = 0;
super.onDestroy();
} catch (Exception e) { }
}
#Override
public boolean stopService(Intent name) {
handler.removeCallbacks(sendUpdatesToUI);
updateUIHandler.removeCallbacks(sendUpdatesToUIOnResume);
durationTime = 0;
time = 0;
return super.stopService(name);
}
}

Categories

Resources