Generate random number for 5min - java

I want to create a database that basically generates a random 6digit number for 5min (it can expire)and the user to be able to "send" to the database that number if it was within the time limit it would get the users name,email and date when he send it.I am confused though how am i supposed to do that?
In my current code i get the list of all registered users.
Whats the best way to do that should i create a new database that stores that number and the users should i just add that number into my current database?
package com.example.prologue.activities;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.prologue.R;
import com.example.prologue.adapters.UsersRecyclerAdapter;
import com.example.prologue.model.User;
import com.example.prologue.sql.DatabaseHelper;
import java.util.ArrayList;
import java.util.List;
public class UsersListActivity extends AppCompatActivity {
private AppCompatActivity activity = UsersListActivity.this;
private AppCompatTextView textViewName;
private RecyclerView recyclerViewUsers;
private List<User> listUsers;
private UsersRecyclerAdapter usersRecyclerAdapter;
private DatabaseHelper databaseHelper;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users_list);
getSupportActionBar().setTitle("");
initViews();
initObjects();
}
/**
* This method is to initialize views
*/
private void initViews() {
textViewName = (AppCompatTextView) findViewById(R.id.textViewName);
recyclerViewUsers = (RecyclerView) findViewById(R.id.recyclerViewUsers);
}
/**
* This method is to initialize objects to be used
*/
private void initObjects() {
listUsers = new ArrayList<>();
usersRecyclerAdapter = new UsersRecyclerAdapter(listUsers);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewUsers.setLayoutManager(mLayoutManager);
recyclerViewUsers.setItemAnimator(new DefaultItemAnimator());
recyclerViewUsers.setHasFixedSize(true);
recyclerViewUsers.setAdapter(usersRecyclerAdapter);
databaseHelper = new DatabaseHelper(activity);
String emailFromIntent = getIntent().getStringExtra("EMAIL");
textViewName.setText(emailFromIntent);
getDataFromSQLite();
}
/**
* This method is to fetch all user records from SQLite
*/
private void getDataFromSQLite() {
// AsyncTask is used that SQLite operation not blocks the UI Thread.
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
listUsers.clear();
listUsers.addAll(databaseHelper.getAllUser());
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
usersRecyclerAdapter.notifyDataSetChanged();
}
}.execute();
}
}

user handler to repeat a task with a delay
private int counter = 0; // global var
private int random = 0; // global var
final Handler handler = new Handler();
handler.post(r);
final Runnable r = new Runnable() {
public void run() {
//generate random number and do you work
random = (int)(Math.random() * 50 + 1);
counter++;
// call Runnable again after 1 sec if counter <= 3000 (50 min)
if (counter <= 3000){ // 3000 = 50min
handler.postDelayed(this, 1000); // 1000 = 1 sec
}
}
};
Hope this will help!

Try below best solution for your question
It's generate random number for 5 min.
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
long t0 = System.currentTimeMillis();
#Override
public void run() {
if (System.currentTimeMillis() - t0 > 300 * 1000) {
cancel();
} else {
int random = new Random().nextInt(51) + 20; // [0, 50] + 20 => [20, 70]
}
}
},1000,1000);
I hope this can help You!
Thank You.

Related

Not able to use handler properly in Android

I am learning the android development which now I am making a simple timer which I needs a handler to keep it running. If I put the handler inside the main activity, everything goods but I would like to put the method outside the main activity so I can reuse the codes and should be more flexible if I would like to change anything.
here is my code:
*public class Timer{
public void runTimer(TextView view, int seconds, boolean running){
TextView timerTextView = (TextView) view;
Handler handler = new Handler();
MyRunnable r = new MyRunnable(handler,view,seconds,running);
handler.post(r);
}
}
class MyRunnable implements Runnable {
private TextView view;
private int seconds;
private boolean running;
private Handler handler;
public MyRunnable(Handler handler, TextView view, int seconds, boolean running){
this.handler= handler;
this.view =view;
this.seconds = seconds;
this.running = running;
}
public void run(){
TextView timerTextView = (TextView) view;
int hours = seconds/3600;
int minutes = (seconds%3600)/60;
int secs = seconds%60;
String time = String.format("%d:%02d:%02d",hours,minutes,secs);
timerTextView.setText(time);
if (running){
seconds++;
}
handler.postDelayed(this,100);
}
}*
And I created an object in the main activity to call this function. The app can run but the timer doesn't work properly. It only runs the first time but then stop at all. It doesn't keep running. Do you know where is the problem? thanks for your help first!
Main code:
*package com.mad.chapter4;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class StopWatchActivity extends AppCompatActivity {
private Timer timer = new Timer();
private int seconds = 0;
private boolean running = false;
private boolean wasRunning;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);
TextView ttv = (TextView) findViewById(R.id.timerTextView);
if(savedInstanceState!=null){
seconds = savedInstanceState.getInt("seconds");
running = savedInstanceState.getBoolean("running");
wasRunning = savedInstanceState.getBoolean("wasRunning");
}
seconds = timer.runTimer(ttv,seconds,running);
}
public void onSaveInstanceState (Bundle savedInstanceState){
savedInstanceState.putInt("seconds",seconds);
savedInstanceState.putBoolean("running",running);
savedInstanceState.putBoolean("wasRunning",wasRunning);
}
protected void onStop(){
super.onStop();
wasRunning = running;
running = false;
}
protected void onStart(){
super.onStart();
if(wasRunning){
running = true;
}
}
public void onClickStartButton(View view){
running = true;
}
public void onClickStopButton (View view){
running = false;
}
public void onClickResetButton (View view){
seconds = 0;
running = false;
}
// public void runTimer(){
//
// final TextView timerTextView = (TextView) findViewById(R.id.timerTextView);
// final Handler handler = new Handler();
//
// Runnable r = new Runnable(){
// #Override
// public void run(){
// int hours = seconds/3600;
// int minutes = (seconds%3600)/60;
// int secs = seconds%60;
// String time = String.format("%d:%02d:%02d",hours,minutes,secs);
// timerTextView.setText(time);
// if (running){
// seconds++;
// }
// handler.postDelayed(this,100);
// }
// };
//
// handler.post(r);
//
//
// }
}*
From the way I see it your running and wasRunning are never set to true which in turn is not updating your UI only first time but not after that. Try setting running and wasRunning to true before you call timer.runTimer(ttv, seconds, running) in onCreate. I think when you were calling this code in activity you were making call to onClickStartButton(View view) which did set the running flag to true and was updating your UI correctly.
Also, your runTimer functions does not return anything but you in your code you are assigning this function result to an int. Is that a copy/paste error?

Google Glass: Can't swipe between cards in immersion

My problem is I've created an immersion activity with several cards in it. When I try and swipe between the cards, nothing happens. I can swipe down to end the activity, but I can't swipe between cards. The funny thing is if I let the glass go to sleep and then wake it up again, I can then swipe between cards. Because of this oddity, I'm pretty sure I'm missing one line of code that gives the cards focus or something, but I don't know what it is.
I've found a workaround using a gesture detector, but it doesn't look like normal swiping, so I'd like to avoid that. My code is below:
package com.akqa.glass.recipie;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import com.google.android.glass.media.Sounds;
import com.google.android.glass.view.WindowUtils;
import com.google.android.glass.widget.CardBuilder;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.List;
/**
* Created by torti_000 on 11/29/2014.
*/
public class PairingsActivity extends Activity {
private CardAdapter mAdapter;
private CardScrollView mCardScroller;
private int mTapPosition;
private final Handler handler = new Handler();
private boolean mVoiceMenuEnabled = true;
private String ingredientUrl = null;
private static final String TAG = "Ingredients";
private static final String URL_BASE = "http://www.ingredientpairings.com/?i=";
private ArrayList<String> iList;
//Number of ingredients to display per card.
private static final int ITEMS_PER_CARD = 4;
private int numCards;
String object = null;
Context context;
private View mView;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
//Loading
mView = buildView();
mCardScroller = new CardScrollView(this);
mCardScroller.setAdapter(new CardScrollAdapter() {
#Override
public int getCount() {
return 1;
}
#Override
public Object getItem(int position) {
return mView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return mView;
}
#Override
public int getPosition(Object item) {
if (mView.equals(item)) {
return 0;
}
return AdapterView.INVALID_POSITION;
}
});
setContentView(mCardScroller);
Log.d(TAG, "View has been set");
mCardScroller.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Plays sound.
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(Sounds.TAP);
}
});
//Recipes
iList = new ArrayList<String>();
context = this;
Intent pairings = getIntent();
object = pairings.getStringExtra("Object");
Log.i(TAG, "Pairings thinks object is: " + object);
new GetIngredients().execute(object);
}
/**
* Create Loding Page
*/
private View buildView() {
Log.d(TAG, "Creating Loading Card");
CardBuilder card = new CardBuilder(this, CardBuilder.Layout.TEXT);
card.setText("Loading");
return card.getView();
}
/**
* Creates list of cards that showcase different type of {#link com.google.android.glass.widget.CardBuilder} API.
*/
private List<CardBuilder> createCards(Context context) {
ArrayList<CardBuilder> cards = new ArrayList<CardBuilder>();
Log.i(TAG, "Creating Cards");
/*
* One ingredient per card...
*/
for(int i = 0; i < iList.size(); i++){
cards.add(new CardBuilder(context, CardBuilder.Layout.TEXT)
.setText(iList.get(i)));
Log.d(TAG, "Ingredient is: " + iList.get(i));
}
Log.d(TAG, "Done adding pairings?");
return cards;
}
#Override
protected void onResume() {
super.onResume();
mCardScroller.activate();
}
#Override
protected void onPause() {
mCardScroller.deactivate();
super.onPause();
}
public class GetIngredients extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... strings) {
StringBuffer buffer = new StringBuffer();
try {
ingredientUrl = object.replaceAll(" ", "+");
Log.d(TAG, ingredientUrl);
// strings[0].replaceAll(" ", "+");
Log.d(TAG, "Finding matches for " + object);
Document doc = Jsoup.connect(URL_BASE + ingredientUrl).get();
Log.d(TAG, "Matches found for " + object);
Elements ingredients = doc.select("div.main p b");
// Log.d(TAG, "Ingredients are: " + ingredients.toString());
for (Element ingredient : ingredients) {
String data = ingredient.text();
iList.add(data);
buffer.append("Data [" + data + "] \r\n");
Log.d(TAG, "Ingredient is: " + data);
}
}
catch(Throwable t) {
t.printStackTrace();
}
return buffer.toString();
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// Compute the number of cards needed to display the items with 4 per card (rounding up to
// capture the remainder).
numCards = (int) Math.ceil((double) iList.size() / ITEMS_PER_CARD);
startCards();
}
}
private void startCards(){
mAdapter = new CardAdapter(createCards(this));
// mCardScroller = new CardScrollView(this);
mCardScroller.setAdapter(mAdapter);
setContentView(mCardScroller);
}
}
In your onCreate(), add this line before the setContentView() like this:
mCardScroller.activate(); //add this line
setContentView(mCardScroller);
You had this line in your onResume() and that's why it is behaving as is.
I had a problem where I was switching the content view between a CardScrollView and a SurfaceView. After I made sure that onPause() and onResume() were both called in the logically correct places, the scroller was fine.
It seems like if the CardScrollView has lost focus then the scroller loses functionality and calling .activate() only works if .deactivate() has been called beforehand, hence why both onPause() and onResume() need to both be called in the logically correct places as .deactivate() is called within onPause() and .activate() is called within onResume(). I tried just calling .deactivate() and .activate() on their own instead of onPause() and onResume() and the behaviour was the same in both instances, supporting what I have written here.
So, in short, if switching between views with setContentView(), be sure to call onPause() before the switch and onResume() after the switch otherwise the scroller will stop responding.

starting my countdown clock when activity is open

I am a bit of a keen novice at Android! I have a snippet of code for timer I want to use in my app however it works when the button is clicked and I want it to work when I open the activity that it is on. I have tried so many solutions but cannot get it to operate. Would really appreciate any help, I have tried putting an if statement in, I have tried using the onStart, and putting it the OnCreate all with no joy. The closest I can get is that it just starts with "Time Up!" in the field.
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
Button buttonStartTime;
TextView textViewShowTime;
CountDownTimer countDownTimer;
long totalTimeCountInMilliseconds;
long timeBlinkInMilliseconds;
boolean blink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getReferenceOfViews ();
setActionListeners ();
totalTimeCountInMilliseconds = 60 * 1000;
timeBlinkInMilliseconds = 30 * 1000;
}
private void setActionListeners() {
buttonStartTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
#Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 1000;
if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
if ( blink ) {
textViewShowTime.setVisibility(View.VISIBLE);
} else {
textViewShowTime.setVisibility(View.INVISIBLE);
}
blink = !blink;
}
textViewShowTime.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));
}
#Override
public void onFinish()
textViewShowTime.setText("Time up!");
textViewShowTime.setVisibility(View.VISIBLE);
}
}.start();
}
}
);
}
private void getReferenceOfViews() {
buttonStartTime = (Button) findViewById(R.id.btnStartTime);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
}
}
You can modify your onResume() as :
#Override
public void onResume(){
buttonStartTime.performClick();
super.onResume();
}
You should make buttonStartTime a global variable for the class.

sequential countdowntimer with handler wont update textView correctly

I was trying to build some sort of sequential countdown. Meaning, that I build up a queue of "exercises", each one containing a specific duration, which is the countdown time. In a custom Countdown class, I pop these exercises off the queue and use the duration as countdown.
I want these countdowns to run one after another. For this I built a Countdown class, based on the code basis of the abstract class CountDownTimer.
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Locale;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.widget.Button;
import android.widget.TextView;
public class ExerciseMeCountDownTimer {
private static final int MSG_COUNTDOWN = 100;
private static final int MSG_FINISH = 99;
private ArrayDeque<Exercise> eq;
private long mMillisInFuture;
private int mCountdownInterval;
private String name;
private long mStopTimeInFuture;
CountdownHandler cHandler;
public ExerciseMeCountDownTimer(ArrayList<Exercise> elist,
Button startStopButton, TextView countdownText,
CountdownHandler cHandler) {
this.cHandler = cHandler;
eq = new ArrayDeque<Exercise>(elist);
this.start();
}
public final void cancel() {
mHandler.removeMessages(MSG);
}
private synchronized final ExerciseMeCountDownTimer start() {
if (!eq.isEmpty()) {
Exercise e = eq.pop();
this.mMillisInFuture = Long.parseLong(e.getDuration());
this.mCountdownInterval = 30;
this.name = e.getName();
} else {
onFinish();
return this;
}
if (mMillisInFuture <= 0) {
onFinish();
return this;
}
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
}
public void onTick(long millisUntilFinished) {
Message msg = cHandler.obtainMessage(MSG_COUNTDOWN);
Bundle data = new Bundle();
String text = String.format(Locale.GERMANY, "%02d:%02d:%03d",
millisUntilFinished / 100000, millisUntilFinished / 1000,
millisUntilFinished % 1000);
data.putString("countdown", text);
msg.setData(data);
cHandler.sendMessage(msg);
}
public void onFinish() {
if (!eq.isEmpty()) {
this.start();
}
Message msg = cHandler.obtainMessage(MSG_FINISH);
Bundle data = new Bundle();
String text = String.format(Locale.GERMANY, "00:00:000");
data.putString("finish", text);
msg.setData(data);
cHandler.sendMessage(msg);
}
private static final int MSG = 1;
// handles counting down
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
final long millisLeft = mStopTimeInFuture
- SystemClock.elapsedRealtime();
if (millisLeft <= 0) {
onFinish();
} else if (millisLeft < mCountdownInterval) {
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user's onTick taking time to
// execute
long delay = lastTickStart + mCountdownInterval
- SystemClock.elapsedRealtime();
// special case: user's onTick took more than interval
// to
// complete, skip to next interval
while (delay < 0)
delay += mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
};
#Override
public String toString() {
return this.name;
}
}
The important part is the sendMessage part, where I send the time left on the countdown to a handler of my MainActivity, which then, should update a textview.
import android.os.Handler;
import android.os.Message;
class CountdownHandler extends Handler {
private static final int MSG_COUNTDOWN = 100;
private static final int MSG_FINISH = 99;
private MainActivity mActivity;
CountdownHandler(MainActivity activity) {
this.mActivity = activity;
}
#Override
public void handleMessage(Message msg) {
if (msg.what == MSG_COUNTDOWN) {
String text = msg.getData().getString("countdown");
this.mActivity.sayLog(text);
}
if (msg.what == MSG_FINISH) {
String text = msg.getData().getString("finish");
this.mActivity.sayLog(text);
}
}
And finally updates the textView in MainActivty
public void sayLog(String text) {
countdown.setText(text);
}
ExerciseMeCountDownTimer is called by
new ExerciseMeCountDownTimer(elist, cHandler);
on some onClick().
The problem is, that sometimes (actually most of the time) the textView is not updated properly. It stops updating on random times like 00:05:211 etc.
Would anyone mind telling me why this is keeps happening? Maybe also adding a solution or at least some literature (maybe pointing out some sections) which I should read to understand the problem? I am also upen for alternative approaches, as I am new to this "handler", "threads" thing in android.
EDIT
the textview was updating, but the textview was clickable. Whenever I clicked on the textview it stopped updating!
as the accepted answer shows, I decided to use the direkt approach of updating the appropriate textview inside the onTick() method.
Using Handler and things in this situation is making it overly complicated.
CountDownTimers onTick() and onFinish() both run on the UI Thread so updating TextViews and other Views from either method can be done easily just by passing a reference of the View to the constructor of the class, as you are already doing. Then you simply update it in the method needed.
// could create a member variable for the TextView with your other member variables
...
mTV;
then in your constructor assign it
// removed reference to Handler--you already have reference to TextView here
public ExerciseMeCountDownTimer(ArrayList elist,
Button startStopButton, TextView countdownText) {
mTV = countdownText;
then update in whichever method is needed
public void onTick(long millisUntilFinished) {
String text = String.format(Locale.GERMANY, "%02d:%02d:%03d",
millisUntilFinished / 100000, millisUntilFinished / 1000,
millisUntilFinished % 1000);
mTV.setText(text); // set the text here
}
public void onFinish() {
if (!eq.isEmpty()) {
this.start();
}

Created a simple timer on Android, doesn't work as expected

I'm trying display a timer on my app with a Thread calculating the elapsed time and a Handler to modify the view.
I know that Android doesn't allow other threads that the UI Thread to modify a view, so I use the Handler to do this.
But it seems that it was not effective, because I still have the "android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."
What should I do ?
(Oh, and the code is bellow if this might help)
package com.hangin.arround.emergency;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
public class MainActivity extends SherlockActivity {
private static final String TAG = "MainActivity";
private TextView elapsedTime;
private Button startButton;
private Button stopButton;
private boolean shouldContinue = false;
private Handler mHandler;
private Runnable runnable;
private static final int MESSAGE_ELAPSED_TIME = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Creating the action bar, initializing my views and stuff
// Creating the Handler
mHandler = new Handler() {
#SuppressLint("SimpleDateFormat")
#Override
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what) {
case MESSAGE_ELAPSED_TIME :
// Converting from milliseconds to a String
DateFormat formatter = new SimpleDateFormat("hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(msg.arg1);
String elapsedTimeString = formatter.format(calendar.getTime());
elapsedTime.setText(elapsedTimeString);
break;
default:
break;
}
}
};
// Creating the Runnable
runnable = new Runnable(){
#Override
public void run() {
int startTime, actualTime;
startTime = (int) System.currentTimeMillis();
Log.d(TAG, "startTime = " + startTime);
while(shouldContinue) {
// Calcul du temps écoulé
actualTime = (int)System.currentTimeMillis() - startTime;
Log.d(TAG, "actualTime = " + actualTime);
// Creating the message sent to the Handler
Message elapsedTimeMessage = new Message();
elapsedTimeMessage.what = MESSAGE_ELAPSED_TIME;
elapsedTimeMessage.arg1 = (int) actualTime;
// Sending the message
mHandler.handleMessage(elapsedTimeMessage);
}
}
};
// Adding the OnClickListeners on startButton and stopButton
startButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// Launching Thread
(new Thread(runnable)).start();
shouldContinue = true;
}
});
stopButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// Stopping the thread
shouldContinue = false;
}
});
}
}
Change
Message elapsedTimeMessage = new Message();
elapsedTimeMessage.what = MESSAGE_ELAPSED_TIME;
elapsedTimeMessage.arg1 = (int) actualTime;
// Sending the message
mHandler.handleMessage(elapsedTimeMessage);
to
Message.obtain(mHandler, MESSAGE_ELAPSED_TIME, (int) actualTime, 0).sendToTarget();
From the API docs, it seems handleMessage is something called later in the handling process and shouldn't be invoked directly.
In order to have the message processed on the thread where the handler was created, you need to call sendMessage
You should change mHandler.handleMessage(elapsedTimeMessage);
with mHandler.sendMessage(elapsedTimeMessage);

Categories