last day (14 days later) » 

6:21 AM
1
A: android try/catch execute sequence

PEMapModderNever block the UI thread! All user interface operations are handled in the UI thread. If you block the UI thread with a Thread.sleep call, an ANR (Application Not Responding) will occur. Moreover, Thread.sleep is never the right way to create timers, unless you are writing the core heartbeat of...

 
this method calls my function at a FIXED interval,which is not I want.
 
Then just change the 1000 to your random number. But didn't you say that you want the toast message to show up consistently?
 
yes, I did.so basically my code should be something like: final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { } }, Myrandomnumber); ?
 
Delay it with random.nextInt(). Make the Random object final. Create a class field that stores how many times this runnable has been called. Editing in a minute to show you how.
@tikael Edited.
 
I tested, the toast msg does not show...though the UI keeps updating.
 
6:21 AM
Does it show the first time? Did you add the last line runnable.run()? It is unreasonable that it doesn't show any toasts (unless you have problems your side), because the runnable must at least show a toast either way, postDelayed or not.
 
it does not and I did add the runnable.run()
 
@tikael Add a logcat message in the run method inside the Runnable. If your code is run, that logcat message must show. If the logcat message shows but the toast doesn't show, your toast has a problem.
 
I added Log.i(TAG,"yes"); right under the "please wait..."and I can see TAG:yes in the logcat--info window..still no toast message...so weird.
 
Try changing your context? Maybe you are using the wrong context.
 
wait, I got it ,we all forget add .show().
 
6:21 AM
LOL. DIdn't Android Studio remind you? BTW, please upvote and accept this answer if it works.
 
one more thing, the whole function calls toast for each loop, is there a way to show the "please wait.." constantly? maybe I should customize the Toast.LENGTH?
 
The original method worked!
The original method was to show the Please Wait consistently.
It summed up 20 random.nextInt(5000) numbers
 
in my side..it does not..
 
And then the toast shows up every second, until the desired time ended.
What do you mean?
in my original answer
 
like I said in the thread, the "please.." msg shows every loop, (and then disappear when 'SHORT' expires)
 
6:25 AM
I am wondering, why do you add up 20 random(5000) instead of a single random(100000)?
 
btw, how do you "@" me ? I tried "@" in the anwswer but it does not work
 
Just write @PEMapModder. It is only used for notifications. It doesn't really point to a specific user, just someone who participated in this answer.
 
add up 20 random(5000) does not equal to random(100000)
 
Why not?
 
in my understanding, random(100000) only generates one number
while call random(5000) 20 times gives me 20
 
6:27 AM
But you just want to wait for that amount of time
 
I want to call my function at random interval 20 times
 
Oh, so you are executing something else other than showing toasts?
what about making two handlers?
one handler for showing toasts every second
another handler for calling your function at random intervals
 
now I can call my function,nvm, maybe b/c I am not a native speaker ,so you might misunderstand
 
after the second handler runs the last time, set a class field so that the first handler knows that you finished executing, and then shows the "Done" toast message.
I'm writing you an example
 
I simply put my function in the run() block, and this means it runs at random interval 20 times,right?
 
6:31 AM
yes
but right now your toast would stop displaying
 
so you suggest i use a second handler to deal with the toast msg?
if I want it to show constantly
 
yes
You may also pre-evaluate the intervals in an array
so then you can display the Done toast with less than 1 second of delay.
may complicate it a bit though
 
I see how you do the "trick", LENGH_SHORT is 1s ,and you force it to show every 1s, and then from the users' aspect, it is shown constantly
thank you every much!
 

last day (14 days later) »