@@ -43,11 +43,22 @@ public final class AppLifecycleIntegration implements Integration, Closeable {
4343
4444 private @ Nullable SentryAndroidOptions options ;
4545
46- private final @ NotNull Context context ;
47- private final @ NotNull BuildInfoProvider buildInfoProvider ;
46+ // Both null when created through the deprecated no-arg constructor, which skips ANR warnings.
47+ private final @ Nullable Context context ;
48+ private final @ Nullable BuildInfoProvider buildInfoProvider ;
4849 private @ Nullable Consumer <AnrWarningResult > anrWarningConsumer ;
4950 private boolean isClosed ;
5051
52+ /**
53+ * @deprecated Without a {@link Context} the integration cannot listen for ANR warnings. Use
54+ * {@link #AppLifecycleIntegration(Context, BuildInfoProvider)} instead.
55+ */
56+ @ Deprecated
57+ public AppLifecycleIntegration () {
58+ this .context = null ;
59+ this .buildInfoProvider = null ;
60+ }
61+
5162 public AppLifecycleIntegration (
5263 final @ NotNull Context context , final @ NotNull BuildInfoProvider buildInfoProvider ) {
5364 this .context = context .getApplicationContext ();
@@ -128,6 +139,13 @@ private void removeObservers() {
128139 */
129140 private void scheduleRegisterAnrWarningListener (
130141 final @ NotNull IScopes scopes , final @ NotNull SentryAndroidOptions options ) {
142+ final @ Nullable Context context = this .context ;
143+ if (context == null || buildInfoProvider == null ) {
144+ options
145+ .getLogger ()
146+ .log (SentryLevel .DEBUG , "ANR warning listener needs a Context, not installed." );
147+ return ;
148+ }
131149 if (buildInfoProvider .getSdkInfoVersion () < Build .VERSION_CODES .CINNAMON_BUN ) {
132150 options
133151 .getLogger ()
@@ -136,7 +154,9 @@ private void scheduleRegisterAnrWarningListener(
136154 }
137155
138156 try {
139- options .getExecutorService ().submit (() -> registerAnrWarningListener (scopes , options ));
157+ options
158+ .getExecutorService ()
159+ .submit (() -> registerAnrWarningListener (context , scopes , options ));
140160 } catch (RejectedExecutionException e ) {
141161 options
142162 .getLogger ()
@@ -171,7 +191,9 @@ private void scheduleUnregisterAnrWarningListener() {
171191 // before this ever runs. The suppression is scoped so that register() stays lint-checked.
172192 @ SuppressLint ("NewApi" )
173193 private void registerAnrWarningListener (
174- final @ NotNull IScopes scopes , final @ NotNull SentryAndroidOptions options ) {
194+ final @ NotNull Context context ,
195+ final @ NotNull IScopes scopes ,
196+ final @ NotNull SentryAndroidOptions options ) {
175197 final @ Nullable ActivityManager activityManager =
176198 (ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE );
177199 if (activityManager == null ) {
@@ -216,7 +238,8 @@ private void unregisterAnrWarningListener(final @NotNull SentryAndroidOptions op
216238 anrWarningConsumer = null ;
217239 }
218240
219- if (consumerRef == null ) {
241+ final @ Nullable Context context = this .context ;
242+ if (consumerRef == null || context == null ) {
220243 return ;
221244 }
222245
0 commit comments