My web service response comes in this format: 19/2/2011. I want to compare this web service date to my own calendar date.
Is there any answer and how to highlight that date?
final Thread t = new Thread(new Runnable() {
#Override
public void run() {
parseData();
}
});
t.start();
Thread t1 = new Thread(new Runnable() {
#Override
public void run() {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//Split strings here and check them
}
});
t1.start();
If you have both the date in strings you can break it up into day, time , year token and then can compare the strings....this will be a easy and raw approach to do this
Related
I have a server giving me live data in JSON format which updates every second. I have to display that in my android app.
I am a beginner and I tried Async Task updating every second via a thread and setting a delay on it.
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
// Perform the HTTP request for data and process the response.
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
It runs out of memory and crashes after some time
Are there any alternates?
Try putting your code into handler thread
Runnable runnable = new Runnable() {
#Override
public void run() {
mHandler.postDelayed(this, 1000);
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
};
// start it with:
mHandler.post(runnable);
How I can set dynamically set the progressbar status after reading a value from db SQLite?
I have this code.
int i = 0;
while (!c.isAfterLast()) {
i++;
pb.setProgress(i)
}
But my problem is that progress bar is update only at finish while so without "liveEffect"
You can use runOnUiThread method of Activity class:
runOnUiThread(new Runnable() {
#Override
public void run() {
pb.setProgress(i);
}
});
More here: https://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
Actually, execution time is too low that by live effect not appear . take large cursor around 10000 value then apply loop now you can see progress
I resolved my problem with Handler and Thread.sleep (for simulate live)
new Thread(new Runnable() {
public void run() {
do {
mProgressStatus++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.post(new Runnable() {
public void run() {
pb.setProgress(mProgressStatus);
}
);
} while (c.moveToNext());
}
}).start();
I am currently working on displaying current time on my Android application. I already got the current time but I need it to be dynamic; it should update every second. I have found this solution but there's something wrong:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Thread timerThread = null;
Runnable runnable = new CountDownRunner();
timerThread = new Thread(runnable);
timerThread.start();
}
public void doWork() {
runOnUiThread(new Runnable() {
public void run() {
try {
Date dt = new Date();
int day = dt.getDate();
int month = dt.getMonth();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":" + minutes + ":" + seconds;
time.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) {
}
}
}
}
The error is on this line:
runOnUiThread(new Runnable() {
I think the reason for this error is because I'm implementing it in Fragment. It does not extend to an Activity which is necessary to implement Thread.
I tried to search and found a possible answer wherein I should need an activity to extend it on runOnUiThread but I haven't found any implementation how to do it. I'm somehow confused and stucked at the moment.
Try this: getActivity().runOnUiThread(new Runnable...
It's because:
1) the implicit this in your call to runOnUiThread is referring to AsyncTask, not your fragment.
2) Fragment doesn't have runOnUiThread
getActivity().runOnUiThread(new Runnable() {
If you are using Fragment then try to use:
getActivity().runOnUiThread(new Runnable(){
}
And if you are using this in Activity then use:
YourActivity.this.runOnUiThread(new Runnable() {
}
The amount of time ive spent trying to get methods like Timer, BroadcastReceiver, AlarmManager etc. to work. All i need is a clear way to update the widget or textview every second.
java.util.Date noteTS = Calendar.getInstance().getTime();
String time = "kk:mm";
String date = "dd MMMMM yyyy";
views.setTextViewText(R.id.tvTime, DateFormat.format(time, noteTS));
views.setTextViewText(R.id.tvDate, DateFormat.format(date, noteTS));
I basically need this to set the text either when the time changes or update every second. Every method ive tried has failed. Can someone please give me the best way to do this and how?
You could use a Handler and a Thread:
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
// update your textview here.
}
};
class TickThread extends Thread {
private boolean mRun;
#Override
public void run() {
mRun = true;
while(mRun) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mHandler.sendEmptyMessage(0);
}
}
I am testing a simple producer/ consumer example in android this is what i'm doing.
I have two EditText boxes, one being a producer and the other a consumer. The app also has a single button once this button is pressed two timers start and the producer produces while the consumer consumes. Here is my code:
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Timer producerTimer = new Timer();
producerTimer .schedule(new TimerTask(){
#Override
public void run(){
producer();
}
},100, 300);
Timer consumerTimer = new Timer();
consumerTimer .schedule(new TimerTask(){
#Override
public void run(){
consumer();
}
},100, 300);
}
});
}
Now for the other methods:
public void producer(){
this.runOnUiThread(producer_Tick);
}
public void consumer(){
this.runOnUiThread(consumer_Tick2);
}
private Runnable producer_Tick = new Runnable(){
public void run(){
put(i++);
}
};
private Runnable consumer_Tick= new Runnable(){
public void run(){
int result = get();
consumerBox.append(Integer.toString(result) + "\n");
}
};
Here are my Synchronized methods:
public synchronized void put(int val){
if (!empty){
try{
wait();
}catch (InterruptedException e) {Log.d(TAG,"Error Putting");}
}
producerBox.append(Integer.toString(val) + "\n");
empty = false;
buffer=val;
notify();
}
public synchronized int get(){
if (empty){
try{
wait();
}catch (InterruptedException e) {Log.d(TAG,"Error getting");}
}
empty = true;
notify();
return buffer;
}
This program runs to random points all the time. Sometimes for a couple of minuites it runs fine where consumer reads from producer etc.. However, everytime, at some point, the program will just freeze at producer and consumer at a certain value (random each time). Does anyone see a problem with the above code?
You should use a blocking queue to communicate between producer-consumer threads in more efficient and easy to understand way.
you should use notifyAll(). notify does not gaurantee which thread is notified. Also check hat empty is actually synchronized (i.e. is owned by the status class where get and put reside.).