@@ -66,6 +66,7 @@ public final class SystemEventsBreadcrumbsIntegration
66
66
private volatile boolean isClosed = false ;
67
67
private volatile boolean isStopped = false ;
68
68
private volatile IntentFilter filter = null ;
69
+ private volatile HandlerThread handlerThread = null ;
69
70
private final @ NotNull AtomicBoolean isReceiverRegistered = new AtomicBoolean (false );
70
71
private final @ NotNull AutoClosableReentrantLock receiverLock = new AutoClosableReentrantLock ();
71
72
// Track previous battery state to avoid duplicate breadcrumbs when values haven't changed
@@ -141,13 +142,16 @@ private void registerReceiver(
141
142
filter .addAction (item );
142
143
}
143
144
}
144
- try {
145
- // registerReceiver can throw SecurityException but it's not documented in the
146
- // official docs
147
- final @ NotNull HandlerThread handlerThread =
145
+ if (handlerThread == null ) {
146
+ handlerThread =
148
147
new HandlerThread (
149
148
"SystemEventsReceiver" , Process .THREAD_PRIORITY_BACKGROUND );
150
149
handlerThread .start ();
150
+ }
151
+ try {
152
+ // registerReceiver can throw SecurityException but it's not documented in the
153
+ // official docs
154
+
151
155
// onReceive will be called on this handler thread
152
156
final @ NotNull Handler handler = new Handler (handlerThread .getLooper ());
153
157
ContextUtils .registerReceiver (context , options , receiver , filter , handler );
@@ -204,6 +208,8 @@ public void close() throws IOException {
204
208
try (final @ NotNull ISentryLifecycleToken ignored = receiverLock .acquire ()) {
205
209
isClosed = true ;
206
210
filter = null ;
211
+ handlerThread .quit ();
212
+ handlerThread = null ;
207
213
}
208
214
209
215
AppState .getInstance ().removeAppStateListener (this );
0 commit comments