Questions tagged [android-handler]

A Handler allows you to send and process `Message` and Runnable objects associated with a thread's `MessageQueue`. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

848 questions
8
votes
1 answer

Android handler, perform post() not in the ui thread

From the beginning I though Handler's methods post() and postDelayed() did things in a different thread from UI thread, and I decided to create a TCP Socket on it, but it's not working. I'm receiving NetworkOnMainThreadException was thrown. Is…
Isen
  • 115
  • 1
  • 7
8
votes
2 answers

Why use AsyncTaskLoader with LoaderManager, instead of simple Handler?

Running asynchronous tasks off of the UI thread then modifying the UI is a common issue in android development, so I decided to take some time, research, and play around with different techniques and find what works best for me. What I considered…
Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
7
votes
3 answers

postDelayed() in a Service

I'm trying to restart service from itself in a few time. My code looks like this (inside the onStartCommand(...)) Looper.prepare(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void…
Bolein95
  • 2,947
  • 2
  • 26
  • 31
7
votes
0 answers

Android BLE Scan in Background Service

I'm trying to make an Android app that will scan for a certain Bluetooth device as a background service. Once the phone is within a certain range of the Bluetooth device, measured by reading the RSSI, the background service will start an Activity…
7
votes
2 answers

Does thread die when activity is finished?

If i start a background thread, what will happen if the activity that is started from finishes() before the thread terminates. Will the thread terminate as well or will it stay alive? new Thread(new Runnable() { public void run()…
Jake
  • 2,877
  • 8
  • 43
  • 62
7
votes
2 answers

AudioManager sending message to a Handler on a dead thread?

I am trying to programmatically raise the volume to the STREAM_MUSIC stream's maximum value, but I am having a "Sending message to a Handler on a dead thread" issue when I do. Also, it seems like it does not raise the volume 100% of the time,…
Dwebtron
  • 777
  • 13
  • 27
6
votes
2 answers

Handler or Launching a Coroutine Job to do something in the MainThread

I've been wondering about whether it is a better approach to use a Handler (Looper.getMainLooper()) or launch a new Coroutine Job to do small things on the Main Thread, like updating a View. Handler: val uiHandler =…
6
votes
3 answers

Handler(Handler.Callback) is deprecated

Handler(android.os.Handler.Callback) is deprecated what should I use instead? Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message message) { switch(message.what) { …
Anirudh Mitra
  • 63
  • 1
  • 4
6
votes
2 answers

How to resolve "expecting class body" error in kotlin

The code: var shouldStopLoop = false val handler = object : Handler() val runnable = object: Runnable // The error occurs here { override fun run() { getSubsData() if(!shouldStopLoop) { …
curiousgeek
  • 903
  • 11
  • 18
6
votes
1 answer

Runnable is not being executed completely after the delay is over from Handler's postDelayed() in Oppo F1

The logcat shows the following error when run application in oppo f1 version 5.1: ANR_LOG: >>> msg's executing time is too long Blocked msg = { when=-15s421ms what=0 target=android.view.Choreographer$FrameHandler…
IID
  • 93
  • 2
  • 8
6
votes
3 answers

This message cannot be recycled because it is still in use

I'm trying to use this article to create asynchronous UDP socket. So I've this code: import android.os.Handler; import android.os.HandlerThread; import android.os.Message; import java.net.DatagramSocket; import java.net.SocketException; public…
Pitel
  • 5,334
  • 7
  • 45
  • 72
6
votes
0 answers

java.lang.SecurityException: Requesting code from com.android.providers.downloads to be run in process my.package

The error appears in RecyclerView Adapter on different versions of android and devices. Im retrieving information about download progress every second and show percents on item layout. @Override public void onBindViewHolder(final ItemViewHolder…
6
votes
1 answer

Updating a Widget at short intervals using API 19+

Pre API 19, the go-to method for updating a Widget faster than the updatePeriodMillis minimum time of 30 minutes was to use an AlarmManager and a BroadcastReceiver to receive the Intent after the specified interval used when setting up the…
Orbit
  • 2,985
  • 9
  • 49
  • 106
6
votes
1 answer

Completion Handlers in Android

I am an iOS developer who just recently tried Android development. In iOS I use Completion Handlers in my codes. I am wondering if there is an equivalent of it in Android development? Thank you
JayVDiyk
  • 4,277
  • 22
  • 70
  • 135
6
votes
1 answer

Create a Custom Completion/Callback Handler to return Objects after a HTTP Request is Completed

I am an iOS Developer starting to learn Android. In Swift, creating a completion handler is very simple, but I still can't find a way to do it in Java. I am sorry if this question is too noob for StackOverflow people. Problem I am creating a class…
JayVDiyk
  • 4,277
  • 22
  • 70
  • 135