35
35
import android .net .Uri ;
36
36
import android .os .Build ;
37
37
import android .os .Bundle ;
38
- import android .os .Looper ;
38
+ import android .os .HandlerThread ;
39
39
import android .view .WindowManager ;
40
40
import androidx .annotation .NonNull ;
41
41
import androidx .annotation .Nullable ;
@@ -120,6 +120,7 @@ public class RNCallKeepModule extends ReactContextBaseJavaModule implements Life
120
120
121
121
private static final String TAG = "RNCallKeep" ;
122
122
private static TelecomManager telecomManager ;
123
+ private HandlerThread handlerThread ;
123
124
private LegacyCallStateListener legacyCallStateListener ;
124
125
private CallStateListener callStateListener ;
125
126
private static TelephonyManager telephonyManager ;
@@ -306,6 +307,9 @@ public void stopListenToNativeCallsState() {
306
307
telephonyManager .listen (legacyCallStateListener , PhoneStateListener .LISTEN_NONE );
307
308
if (Looper .myLooper () != null ) {
308
309
Looper .myLooper ().quit ();
310
+ if (handlerThread != null ) {
311
+ handlerThread .quitSafely ();
312
+ handlerThread = null ;
309
313
}
310
314
}
311
315
}
@@ -317,15 +321,16 @@ public void listenToNativeCallsState() {
317
321
318
322
if (permissionCheck == PackageManager .PERMISSION_GRANTED ) {
319
323
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
320
- callStateListener = new CallStateListener ();
321
- telephonyManager .registerTelephonyCallback (context .getMainExecutor (),callStateListener );
324
+ callStateListener = new CallStateListener ();
325
+ telephonyManager .registerTelephonyCallback (context .getMainExecutor (),callStateListener );
322
326
} else {
323
- if (Looper .myLooper () == null ) {
324
- Looper .prepare ();
325
- }
326
- legacyCallStateListener = new LegacyCallStateListener ();
327
- telephonyManager .listen (legacyCallStateListener , PhoneStateListener .LISTEN_CALL_STATE );
328
- Looper .loop ();
327
+ if (handlerThread == null ) {
328
+ handlerThread = new HandlerThread ("TelephonyManager" );
329
+ handlerThread .start ();
330
+ }
331
+
332
+ legacyCallStateListener = new LegacyCallStateListener ();
333
+ telephonyManager .listen (legacyCallStateListener , PhoneStateListener .LISTEN_CALL_STATE );
329
334
}
330
335
}
331
336
}
0 commit comments