android-handler

How to set Delay for loop in android?

泪湿孤枕 提交于 2019-12-05 15:49:05
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()

The specified message queue synchronization barrier token has not been posted

蓝咒 提交于 2019-12-05 05:12:32
问题 I have an app which as a binded service. I am sending messages to the service, but sometimes I am getting the following error: E/AndroidRuntime(28216): java.lang.IllegalStateException: The specified message queue synchronization barrier token has not been posted or has already been removed. Sometimes I get this error instead: android.util.AndroidRuntimeException: { what=888 when=0 } This message is already in use. Sometimes the UI just freezes. I am communicating from the service to the

Should I manually close HandlerThreads created by my application when destroying the activity?

落爺英雄遲暮 提交于 2019-12-05 02:45:26
My app is composed of a single Activity . In this activity, I'm creating multiple HandlerThread s which run in a loop to do socket blocking operations. Currently I post a quit message to everyone of these HandlerThread s during my Activity.onDestroy() . Sometimes, when I open my app, close it and relaunch it, it crashes (many time due to posting a message to a handler thread which is not running). My question is: What is the right way to close HandlerThread when I close my app? (Note that those threads might be blocking on a socket operation). EDIT: More information: I have a pool of Handler

What will happen if I use try catch and finally for handler using runnable?

女生的网名这么多〃 提交于 2019-12-04 21:41:49
I use the code below for getting some work done everytime after some time interval, and using post delay in 'finally' clause and oustide of runnable. Here is the code. Runnable runnable = new Runnable() { @Override public void run() { try { // do somthing } catch (Exception e) { // TODO: handle exception } finally { handler.postDelayed(this, 60000); } } }; handler.postDelayed(runnable, 60000); handler.postDelayed(runnable, 60000); will run two times or a single time. it depends! first matter how the each try / catch / finally block completes normally or abruptly? the finally block "always ^ "

Android : Custom adapter fails to reload list view on activity resume

喜欢而已 提交于 2019-12-04 19:21:51
I am trying to display received messages using custom adapter on list view , until now everything is working fine , but when I press back and come back and launch this activity again , only message send works but received messages cannot be displayed on the list view. Steps I have done so far : Use debugger to find out if message is not empty, messages are received and not empty. Use debugger inside receives() function , it turns out on first launch of activity looper.loop() function gives control back to main thread but next time on activity resume this loops doesn't seem to end (may be this

How to use Handler and handleMessage in Kotlin?

北城以北 提交于 2019-12-04 10:56:47
Java code: private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // code here } }; How to convert this java code to Kotlin? I tried this: private val mHandler = object : Handler() { fun handleMessage(msg: Message) { // code here } } But this is seems to be incorrect and gives a compile time error on object Problem: The syntax to override handleMessage() method of the Handler class is incorrect. Solution: Add override keyword before the function that you want to override. private val mHandler = object : Handler() { override fun handleMessage(msg:

different situations to use AlarmManager vs Handler Android

China☆狼群 提交于 2019-12-04 10:20:34
Could someone explain me different situations to use AlarmManager vs Handler with examples please. Any disadvantages of using these two as alternate to each other? Thanks. They have little to do with one another. I am assuming you are referring to using something like postDelayed() on Handler for polling, which is but one small feature of Handler . You would use postDelayed() (also available on any widget or other subclass of View ) in an activity for simple timing events that are within the activity itself. You would use AlarmManager for periodic background operations, much like you would use

Timer time does not change as variable?

左心房为你撑大大i 提交于 2019-12-04 06:19:34
问题 Here is my code, private int V_Time = 1; . . . try { final Timer V_Timer; final Handler V_Handler; V_Timer = new Timer(); V_Handler = new Handler(Looper.getMainLooper()); V_Timer.scheduleAtFixedRate(new TimerTask() { public void run() { V_Handler.post(new Runnable() { public void run() { webservice_method(); V_Time = 2; } }); } }, 0, V_Time * 1000 * 60); } catch (Exception hata) { } V_time changes what returns from web service, it seems right in logs altough, it runs every minute. How can set

What happens to this thread runnable at the end once it is completed?

谁说我不能喝 提交于 2019-12-04 03:23:14
问题 I have this thread which downloads a few images from the server. So once it downloads the images I call the handler and carry on UI updation. So since stop() for thread is deprecated I am not able to use it. I have two questions here. What happens to this thread in the end?(means after I call the handler method what happens to it). OR how do I stop this thread without using stop()? Here is my code. handler=new Handler() { public void handleMessage(Message msg) { if(msg.what==0) { //UI

Is a Handler a Thread or not, and what is the role of a Looper with Handlers and Threads?

喜你入骨 提交于 2019-12-03 20:57:29
Is a Handler a Thread or not? If yes, how can we update the UI from this Handler(thread)? If we use the Looper concept, it may be possible. In this case, does it apply to any threads? I am very much confused about these Threads, Handlers and Loopers. Could anyone please explain them with an example? Is a Handler a Thread or not? If yes, how can we update the UI from this Handler(thread). If we use the Looper concept, it may be possible, in this case does it apply to any threads? I am very much confused about this Thread, Handler and Looper. Could anyone please explain them with an example? The