Have I cut and pasted the code into the wrong spot? I can't seem to get the code to run.
MESSAGE_KEY_REQUEST_FOCUS at line 39 gets highlighted in red. I tried creating a new MESSAGE_KEY_REQUEST_FOCUS variable and also setting the text to a simple string, just to see if it would run, but it didn't.
[CODE lang="java" title="ClickCounter" highlight="39"]package com.example.clickcounter;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.concurrent.atomic.AtomicBoolean;
public class MainActivity extends AppCompatActivity {
int n = 0;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
TextView myText = (TextView) findViewById(R.id.textView);
final String MESSAGE_KEY_UPDATE_TEXT = "com.yourrganisation.yourapp.MSG_UPDATE_TEXT";
AtomicBoolean stopThread = new AtomicBoolean(false);
Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
myText.setText(String.valueOf(msg.getData().getInt(MESSAGE_KEY_REQUEST_FOCUS)));
}
};
final Thread thread = new Thread(() -> {
Looper.prepare();
// The delay after which you wanted to start the thread
try {
Thread.sleep(200);
} catch (InterruptedException ignored) {}
while (!stopThread.get()) {
n++;
Message message = Message.obtain();
Bundle data = new Bundle();
data.putInt(MESSAGE_KEY_UPDATE_TEXT, n);
message.setData(data);
handler.sendMessage(message);
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {}
}
});
button.setOnTouchListener((v, motionEvent) -> {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
stopThread.set(false);
thread.start();
return true;
case MotionEvent.ACTION_UP:
// To stop the thread, you have to change this variable and wait for
// the loop to exit.
stopThread.set(true);
return true;
default:
return false;
}
});
}
}[/CODE]This is the Logcat that I get
[CODE lang="java" title="Logcat" highlight="2,3, 6,7, 10, 14, 24, 25, 29, 30, 33, 37, 46, 47, 49, 50, 54, 55, 58, 62"]2022-12-09 08:46:07.990 30956-30991/com.example.clickcounter D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2022-12-09 08:46:07.994 30956-30991/com.example.clickcounter W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2022-12-09 08:46:07.978 30956-30956/com.example.clickcounter W/RenderThread: type=1400 audit(0.0:166): avc: denied { write } for name="property_service" dev="tmpfs" ino=8445 scontext=u:r:untrusted_app:s0:c136,c256,c512,c768 tcontext=u

bject_r:property_socket:s0 tclass=sock_file permissive=0
2022-12-09 08:46:08.075 30956-30991/com.example.clickcounter D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2022-12-09 08:46:08.086 30956-30991/com.example.clickcounter D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2022-12-09 08:46:08.284 30956-30956/com.example.clickcounter W/le.clickcounte: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2022-12-09 08:46:08.286 30956-30956/com.example.clickcounter W/le.clickcounte: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2022-12-09 08:46:08.682 30956-30989/com.example.clickcounter D/HostConnection: HostConnection::get() New Host Connection established 0xd861fc30, tid 30989
2022-12-09 08:46:08.744 30956-30989/com.example.clickcounter D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
2022-12-09 08:46:08.793 30956-30989/com.example.clickcounter W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2022-12-09 08:46:08.856 30956-30989/com.example.clickcounter D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2022-12-09 08:46:08.856 30956-30989/com.example.clickcounter D/EGL_emulation: eglCreateContext: 0xeed7eda0: maj 2 min 0 rcv 2
2022-12-09 08:46:08.876 30956-30989/com.example.clickcounter D/EGL_emulation: eglMakeCurrent: 0xeed7eda0: ver 2 0 (tinfo 0xe3caa4f0)
2022-12-09 08:46:08.903 30956-30989/com.example.clickcounter W/Gralloc3: mapper 3.x is not supported
2022-12-09 08:46:08.919 30956-30989/com.example.clickcounter D/HostConnection: createUnique: call
2022-12-09 08:46:08.919 30956-30989/com.example.clickcounter D/HostConnection: HostConnection::get() New Host Connection established 0xd8621440, tid 30989
2022-12-09 08:46:08.971 30956-30989/com.example.clickcounter D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
2022-12-09 08:46:08.974 30956-30989/com.example.clickcounter D/eglCodecCommon: allocate: Ask for block of size 0x1000
2022-12-09 08:46:08.974 30956-30989/com.example.clickcounter D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ffff6000 size 0x2000
2022-12-09 08:46:09.013 30956-30989/com.example.clickcounter D/EGL_emulation: eglMakeCurrent: 0xeed7eda0: ver 2 0 (tinfo 0xe3caa4f0)
2022-12-09 08:46:09.021 30956-30989/com.example.clickcounter D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
2022-12-09 08:46:09.321 30956-30956/com.example.clickcounter I/Choreographer: Skipped 42 frames! The application may be doing too much work on its main thread.
2022-12-09 08:47:22.501 31077-31111/com.example.clickcounter D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2022-12-09 08:47:22.488 31077-31077/com.example.clickcounter W/RenderThread: type=1400 audit(0.0:167): avc: denied { write } for name="property_service" dev="tmpfs" ino=8445 scontext=u:r:untrusted_app:s0:c136,c256,c512,c768 tcontext=u

bject_r:property_socket:s0 tclass=sock_file permissive=0
2022-12-09 08:47:22.503 31077-31111/com.example.clickcounter W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2022-12-09 08:47:22.584 31077-31111/com.example.clickcounter D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2022-12-09 08:47:22.590 31077-31111/com.example.clickcounter D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2022-12-09 08:47:22.596 31077-31111/com.example.clickcounter D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2022-12-09 08:47:22.806 31077-31077/com.example.clickcounter W/le.clickcounte: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2022-12-09 08:47:22.806 31077-31077/com.example.clickcounter W/le.clickcounte: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2022-12-09 08:47:23.028 31077-31109/com.example.clickcounter D/HostConnection: HostConnection::get() New Host Connection established 0xdbc582d0, tid 31109
2022-12-09 08:47:23.061 31077-31109/com.example.clickcounter D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
2022-12-09 08:47:23.078 31077-31109/com.example.clickcounter W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2022-12-09 08:47:23.115 31077-31109/com.example.clickcounter D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2022-12-09 08:47:23.116 31077-31109/com.example.clickcounter D/EGL_emulation: eglCreateContext: 0xdbc1a120: maj 2 min 0 rcv 2
2022-12-09 08:47:23.126 31077-31109/com.example.clickcounter D/EGL_emulation: eglMakeCurrent: 0xdbc1a120: ver 2 0 (tinfo 0xdbc0f1e0)
2022-12-09 08:47:23.145 31077-31109/com.example.clickcounter W/Gralloc3: mapper 3.x is not supported
2022-12-09 08:47:23.151 31077-31109/com.example.clickcounter D/HostConnection: createUnique: call
2022-12-09 08:47:23.152 31077-31109/com.example.clickcounter D/HostConnection: HostConnection::get() New Host Connection established 0xdbc58410, tid 31109
2022-12-09 08:47:23.164 31077-31109/com.example.clickcounter D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
2022-12-09 08:47:23.166 31077-31109/com.example.clickcounter D/eglCodecCommon: allocate: Ask for block of size 0x1000
2022-12-09 08:47:23.166 31077-31109/com.example.clickcounter D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ffff6000 size 0x2000
2022-12-09 08:47:23.187 31077-31109/com.example.clickcounter D/EGL_emulation: eglMakeCurrent: 0xdbc1a120: ver 2 0 (tinfo 0xdbc0f1e0)
2022-12-09 08:47:23.193 31077-31109/com.example.clickcounter D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
2022-12-09 08:48:26.621 31195-31195/? I/le.clickcounte: Late-enabling -Xcheck:jni
2022-12-09 08:48:26.632 31195-31195/? E/le.clickcounte: Unknown bits set in runtime_flags: 0x8000
2022-12-09 08:48:26.632 31195-31195/? W/le.clickcounte: Unexpected CPU variant for X86 using defaults: x86
2022-12-09 08:48:27.231 31195-31229/com.example.clickcounter D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2022-12-09 08:48:27.228 31195-31195/com.example.clickcounter W/RenderThread: type=1400 audit(0.0:168): avc: denied { write } for name="property_service" dev="tmpfs" ino=8445 scontext=u:r:untrusted_app:s0:c136,c256,c512,c768 tcontext=u

bject_r:property_socket:s0 tclass=sock_file permissive=0
2022-12-09 08:48:27.236 31195-31229/com.example.clickcounter W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2022-12-09 08:48:27.251 31195-31229/com.example.clickcounter D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2022-12-09 08:48:27.255 31195-31229/com.example.clickcounter D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2022-12-09 08:48:27.259 31195-31229/com.example.clickcounter D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2022-12-09 08:48:27.433 31195-31195/com.example.clickcounter W/le.clickcounte: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2022-12-09 08:48:27.433 31195-31195/com.example.clickcounter W/le.clickcounte: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2022-12-09 08:48:27.653 31195-31227/com.example.clickcounter D/HostConnection: HostConnection::get() New Host Connection established 0xdbc76050, tid 31227
2022-12-09 08:48:27.678 31195-31227/com.example.clickcounter D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
2022-12-09 08:48:27.684 31195-31227/com.example.clickcounter W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2022-12-09 08:48:27.723 31195-31227/com.example.clickcounter D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2022-12-09 08:48:27.723 31195-31227/com.example.clickcounter D/EGL_emulation: eglCreateContext: 0xdbc1a120: maj 2 min 0 rcv 2
2022-12-09 08:48:27.734 31195-31227/com.example.clickcounter D/EGL_emulation: eglMakeCurrent: 0xdbc1a120: ver 2 0 (tinfo 0xdbc0f1e0)
2022-12-09 08:48:27.744 31195-31227/com.example.clickcounter W/Gralloc3: mapper 3.x is not supported
2022-12-09 08:48:27.746 31195-31227/com.example.clickcounter D/HostConnection: createUnique: call
2022-12-09 08:48:27.746 31195-31227/com.example.clickcounter D/HostConnection: HostConnection::get() New Host Connection established 0xdbc762d0, tid 31227
2022-12-09 08:48:27.747 31195-31227/com.example.clickcounter D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2
2022-12-09 08:48:27.749 31195-31227/com.example.clickcounter D/eglCodecCommon: allocate: Ask for block of size 0x1000
2022-12-09 08:48:27.749 31195-31227/com.example.clickcounter D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ffff6000 size 0x2000
2022-12-09 08:48:27.806 31195-31227/com.example.clickcounter D/EGL_emulation: eglMakeCurrent: 0xdbc1a120: ver 2 0 (tinfo 0xdbc0f1e0)
2022-12-09 08:48:27.812 31195-31227/com.example.clickcounter D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
[/CODE]