License Check Loop? - android

Maybe the code kind of shows what I'm attempting to do, so I'll start with that
private void getLicenseResults() {
if (licensed && didCheck == true) {
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openStartingPoint = new Intent(
"***.MAINACTIVITY");
startActivity(openStartingPoint);
finish();
}
}
};
timer.start();
}
if (didCheck == false) {
Toast.makeText(Splash.this, "Checking License, Please Wait",
Toast.LENGTH_LONG).show();
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
getLicenseResults();
Log.i("LICENSE", "re-checking");
}
}
};
timer.start();
}
}
I ran the license check without trying to loop it before, and it worked except if the checking took more than a couple seconds it skipped over the
if (licensed && didCheck == true)
and the activity just kind of stood and waited without launching my main activity (this check is on the splash screen).
So I want the "if didCheck = true" part to be called only after didCheck is in fact finally true. I'm sure there's an easy solution, but I'm self-taught and I have no experience with loops or callbacks(?).
Thank you for any help!

You are not setting didCheck to true in your second if statement before the call to getLicenseResults

Related

ProgressBar not showing, android

Here is my code:
automaticCountryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
if (ContextCompat.checkSelfPermission(HomeActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
setUpLocationPermission();
return;
}
Log.d(TAG, String.valueOf(gps.canGetLocation()));
Log.d(TAG, String.valueOf(gps.getLocation()));
Log.d(TAG, String.valueOf(gps.getLatitude()));
Log.d(TAG, String.valueOf(gps.getLongitude()));
Geocoder myLocation = new Geocoder(HomeActivity.this);
try
{
myList = myLocation.getFromLocation(gps.getLatitude(), gps.getLongitude(), 1);
}
catch (Exception e)
{
Log.d(TAG, "unable");
e.printStackTrace();
}
if(myList != null) {
try {
String country = myList.get(0).getCountryName();
Log.d(TAG, country);
findCountryInArrayList(country);
}
catch (Exception e)
{
Toast.makeText(HomeActivity.this, "Didn't manage to automatically detect location.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
});
}
I want that immediately after the view is clicked the progressbar will become visible. However, it donesn't become visable until all the code is finished, which is against the whole point.
WHy is this not happening right at the beginning of the click? I have put progressBar.setVisibility(View.VISIBLE) at the top, why is it only executed after all the code is done, which sometimes takes a few seconds.
Thanks very much.
This is because you are trying to do your work on the UI thread - the UI will not actually be updated at all until this method finishes.
Try changing up your call to this:
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
progressBar.post( new Runnable() {
public void run() {
// long running code that has UI interactions
}
});
}
This will show the view immediately, and submit the runnable - long running task - to the message queue; this task will be run on a background thread that can still manipulate the UI, but will not cause it to hang.

Android Simon sequence in UIThread

I am very new in Android, as a project, am doing a simon says game, and I'm having some problems representing the sequence to follow.
I tried to use a normal thread to make it follow a line (for example, the first button is lighten during X seconds, then turns off, then the yellow one....), but it didn't make it, because only the threads that created the view hierarchy can manipulate that view (in this case was the UI thread). So I had to load all the method in that thread with runOnUiThread, and at that point it worked almost perfect, because now the sequence is shown up but the frames don't. The Log tells me:
I/Choreographer: Skipped 116 frames! The application may be doing too much work on its main thread.
I have been looking for everywhere and I don't find alternatives for simulate the sequence. I leave here the code of the method.
public void createSequence(){
new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
sequence.clear();
for(byte i=0;i<rounds;i++){
add=r.nextInt((4 - 1) + 1) + 1;
if (add == 1) {
btn1.setBackgroundResource(R.drawable.button1_pressed);
try {
Log.i("Waiting for 1.","Waiting for 1.");
Thread.sleep(time);
btn1.setBackgroundResource(R.drawable.button1);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("Value:", "1.");
} else if (add == 2) {
btn2.setBackgroundResource(R.drawable.button2_pressed);
try {
Log.i("Waiting for 2.","Waiting for 2.");
Thread.sleep(time);
btn2.setBackgroundResource(R.drawable.button2);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("Value:", "2.");
} else if (add == 3) {
btn3.setBackgroundResource(R.color.colorpressedbutton3);
try {
Log.i("Waiting for 3.","Waiting for 3.");
Thread.sleep(time);
btn3.setBackgroundResource(R.drawable.button3);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("Value:", "3.");
} else {
btn4.setBackgroundResource(R.color.colorpressedbutton4);
try {
Log.i("Waiting for 4.","Waiting for 4.");
Thread.sleep(time);
btn4.setBackgroundResource(R.drawable.button4);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("Value:", "4.");
}
sequence.add(add);
}
}
});
}
}.start();
}
You almost got it.
You need to implement the timing logic in a separate thread, but the update (UI) logic on the UI thread.
Something like this:
void setState(final Button b, final int state) {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
b.setBackgroundResource(state);
}
});
}
void runSequence() {
new Thread() {new Runnable() {
#Override
public void run() {
sequence.clear();
for(byte i=0;i<rounds;i++){
add=r.nextInt((4 - 1) + 1) + 1;
if (add == 1) {
setState(btn1,R.drawable.button1_pressed);
try {
Log.i("Waiting for 1.","Waiting for 1.");
Thread.sleep(time);
setState(btn1, R.drawable.button1);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("Value:", "1.");
} else if (add == 2) {
....
}
}).start();
}

Wait for thread to finish and then move to next position

i am trying to display a Toast on the screen and when Toast fades off then move to the next question. I have tried with Thread but cannot seem to manage.
My code:
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getUserSelection()){
position = position + 3;
if (position < questionsArray.size()) {
curName = questionsArray.get(position).getName();
curArray = questionsArray.get(position).getAnswers();
curIscorrect = questionsArray.get(position).getIscorrect();
setupQuestionView(curName, curArray, curIscorrect);
} else {
StringGenerator.showToast(QuestionsActivity.this, "Your score : " + score + "/" + (questionsArray.size() / 3));
}
}else {
StringGenerator.showToast(QuestionsActivity.this, getString(R.string.noanswerselected));
}
}
});
and the getUserSelectionMethod:
private boolean getUserSelection() {
correct = (RadioButton)findViewById(group.getCheckedRadioButtonId());
if (correct == null){
return false;
}else {
correctAnswerText = correct.getText().toString();
if (map.get(correctAnswerText).equals(Constants.CORRECTANSWER)) {
score++;
setCorrectMessage();
return true;
} else {
setWrongMessage();
return true;
}
}
}
private void setCorrectMessage() {
correctToast = new Toast(QuestionsActivity.this);
correctToastView = getLayoutInflater().inflate(R.layout.correct, (ViewGroup) findViewById(R.id.correctRootLayout));
correctText = (TextView)correctToastView.findViewById(R.id.correctTextView);
correctText.setText(getString(R.string.correctAnswer));
correctToast.setDuration(Toast.LENGTH_SHORT);
correctToast.setView(correctToastView);
correctToast.show();
correctThread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
correctToast.cancel();
}
});
correctThread.start();
}
private void setWrongMessage() {
wrongToast = new Toast(QuestionsActivity.this);
wrongToastView = getLayoutInflater().inflate(R.layout.wrong, (ViewGroup) findViewById(R.id.wrongRootLayout));
wrongText = (TextView)wrongToastView.findViewById(R.id.wrongTextView);
wrongText.setText(getString(R.string.wrongAnswer));
wrongToast.setDuration(Toast.LENGTH_SHORT);
wrongToast.setView(wrongToastView);
wrongToast.show();
wrongThread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
wrongToast.cancel();
}
});
wrongThread.start();
}
Any suggestion on how to do this?
You can determine the toast visibility:
toast.getView().getWindowToken()
If the result is null, than your toast isn't visible anymore, and than you can run any code you want.
as stated in this answer you can start a thread that waits the duration of the Toast:
Thread thread = new Thread(){
#Override
public void run() {
try {
Thread.sleep(3500); // 3.5seconds!
// Do the stuff you want to be done after the Toast disappeared
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Toast.LENGTH_SHORT and Toast.LENGTH_LONG are only flags so you have to either hard code the duration or keep them in a constant. The durations are 3.5s (long) and 2s (short).
If you want to manipulate some of your views, you cannot do this in another thread than the "main" UI thread. So you have to implement a kind of callback/polling mechanism to get notified when the SleepThread has finished.
Check this answer to read about a couple of ways to do this. Probably the easiest of them to understand and implement is this:
After you started your Thread you can check if it is still alive and running by calling thread.isAlive(). In this way you can do a while loop that runs while the thread is running:
// start your thread
while(thread.isAlive()){}
// continue the work. The other thread has finished.
Please note that this is NOT the most elegant way to do this! Check the other possibilities in the answer I've mentioned above for more elegant solutions (especially the last one with the listeners is very interesting and worth reading!)
That's because the Thread class is purely executed in the background and you need to manipulate the view in the Main thread. To solve your issue just replace the Thread with AsynTask.
AsyncTask<Void,Void,Void> a = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
correctToast.cancel();
}
};
a.execute();
If you look at my code you can see my onPostExecute, this method is called in the Main Thread.
My Error was because i was trying to acess UI Elements through another Thread so modifying the code like this:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
QuestionsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
moveToNextQuestion();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
did the trick. I hope my answer helps someone!!!

Releasing the camera within a thread

I am creating a simple Morse code app. The user can enter in text which is then translated to Morse and flashed in sequence on a new thread. I have implemented a for loop which is used to turn on/off the camera flash to represent the Morse sequence.
The problem is that when the user navigates away from the activity the on pause method releases the camera but i sometimes get the error 'method called after release'. I am not sure how to cancel the thread from running when the camera is released. I have already attempted to use a volatile Boolean value which is checked at the start of each loop iteration but if the loop is cancelled at any other time but the start then it results in an error.
Does anyone have any ideas/suggestions as to how i could solve this problem?
public void flashTranslation(String message) {
int offIntervalTime = 50;
char[] cArray = message.toCharArray();
for (int i = 0; i < cArray.length; i++) {
if (cArray[i] == '.') {
turnOn();
try {
Thread.sleep(50);
}catch(Exception e)
{
Log.d("One", "Two");
}
turnOff();
try {
Thread.sleep(offIntervalTime);
}catch(Exception e)
{
}
} else if(cArray[i] == ' ')
{
Log.d("EMPTY!", "EMPTY!");
try{
Thread.sleep(100);
}catch(Exception e)
{
}
}
else {
try{
turnOn();
Thread.sleep(dash);
}catch(Exception e)
{
}
try{
turnOff();
Thread.sleep(offIntervalTime);
}catch(Exception e)
{
}
}
}
}
The easiest way to do this is canceling the thread protecting against concurrent access via semaphore. Every time you try to flash on the thread, you check if the thread is canceled. Pseudocode:
Semaphore sem;
onPause(){
sem.take();
camera.turnOff();
camera.release();
thread.cancel();
sem.give();
}
thread.run() {
//This should be run before every call to turnOn or turnoff
sem.take();
if(isCanceled()) {
return;
}
turnOn();
sem.give();
}
onResume() {
new Thread.start();
}

Trying to get a CountDownTimer to restart after 15 seconds.

I'm doing an app that displays various yoga poses for 60 seconds. I've been pulling my hair out trying to get a countdown timer to work the way I want. I need the timer to be able to pause, and restart automatically after 15 seconds. The CountDownTimer from Android didn't work because it had no pause. I tried 2 different versions of rewritten Android CountDownTimers and the classic version (Android CountDownTimer) and they all seem to have the same bug because they are using Handlers.
I've tried this version: http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm
I've tried this version: Android: CountDownTimer skips last onTick()! - I added an onFinish method to it.
I'm trying to do a Thread.sleep in the onFinish method for 15 seconds, and it is not updating the UI past the last second before moving into the Thread.sleep mode. In the following code, the TextView isn't getting updated to be set to empty string until after the Thread.sleep call.
cdt = new CountDownTimerWithPause(60000,1000,true) {
#Override
public void onTick(long millisUntiFinished) {
tvClock.setText("Remaining:" + millisUntiFinished/1000);
}
#Override
public void onFinish() {
tvClock.setText("");
bell.start();
if (bAutoPlayIsOn) {
tvClock.setText("waiting for next pose...");
try {
//15 second pauses between poses
Thread.sleep(15000);
//start over
listener.onForwardItemSelected();
ResetClocktoMaxTime(60000);
resume();
} catch (InterruptedException e) {
Log.d("Sleep Interrupted", e.toString());
e.printStackTrace();
}
catch (Exception e) {
Log.d("Timer", e.toString());
}
}
else {
tvClock.setText("Done");
btnStart.setText("Start ");
bClockIsTicking = false;
btnStart.setCompoundDrawablesWithIntrinsicBounds(null, playBtn, null, null);
}
}
};
Anyone have any ideas how to do this (restart after 15 seconds) differently?
The onFinish method is being called from the interface thread, so any interface changes you make won't actually redraw until onFinish returns. Try something like this:
cdt = new CountDownTimerWithPause(60000,1000,true) {
#Override
public void onTick(long millisUntiFinished) {
tvClock.setText("Remaining:" + millisUntiFinished/1000);
}
#Override
public void onFinish() {
tvClock.setText("");
bell.start();
if (bAutoPlayIsOn) {
tvClock.setText("waiting for next pose...");
(
new Thread() {
public void run() {
try {
//15 second pauses between poses
Thread.sleep(15000);
getActivity().runOnUiThread
(
new Runnable() {
public void run() {
//start over
listener.onForwardItemSelected();
ResetClocktoMaxTime(60000);
resume();
}
}
);
}
catch (InterruptedException e) {
Log.d("Sleep Interrupted", e.toString());
e.printStackTrace();
}
catch (Exception e) {
Log.d("Timer", e.toString());
}
}
}
).start();
}
else {
tvClock.setText("Done");
btnStart.setText("Start ");
bClockIsTicking = false;
btnStart.setCompoundDrawablesWithIntrinsicBounds(null, playBtn, null, null);
}
}
};

Categories

Resources