1 /* 2 * Copyright (C) 2017 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.android; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import androidx.annotation.CallSuper; 23 import dagger.internal.Beta; 24 25 /** 26 * A {@link BroadcastReceiver} that injects its members in every call to {@link #onReceive(Context, 27 * Intent)}. 28 * 29 * <p>This class should only be used for {@link BroadcastReceiver}s that are declared in an {@code 30 * AndroidManifest.xml}. If, instead, the {@link BroadcastReceiver} is created in code, prefer 31 * constructor injection. 32 * 33 * <p>Note: this class is <em>not thread safe</em> and should not be used with multiple {@link 34 * android.os.Handler}s in calls to {@link Context#registerReceiver(BroadcastReceiver, 35 * android.content.IntentFilter, String, android.os.Handler)}. Injection is performed on each 36 * invocation to {@link #onReceive(Context, Intent)} which could result in inconsistent views of 37 * injected dependencies across threads. 38 * 39 * <p>Subclasses should override {@link #onReceive(Context, Intent)} and call {@code 40 * super.onReceive(context, intent)} immediately to ensure injection is performed immediately. 41 */ 42 @Beta 43 public abstract class DaggerBroadcastReceiver extends BroadcastReceiver { 44 @CallSuper 45 @Override onReceive(Context context, Intent intent)46 public void onReceive(Context context, Intent intent) { 47 AndroidInjection.inject(this, context); 48 } 49 } 50