将 Singleton 注入到 BroadcastReceiver 是一种好习惯吗?
更具体地说,假设我有如下单例:
@Singleton
public class UnitProvider {
private boolean mIsUsingCelsius = false;
protected SharedPreferences mSharedPrefs;
@Inject
public UnitProvider(SharedPreferences sharedPrefs) {
mSharedPrefs = sharedPrefs;
mIsUsingCelsius = isUsingCelsiusPref(Locale.getDefault());
}
public void refreshCelsius() {
if (!mSharedPrefs.contains(SharedPreferencesConstants.SP_KEY_USE_CELSIUS)) {
mIsUsingCelsius = isUsingCelsiusBasedOnLocale(Locale.getDefault());
}
}
}
还有一个广播接收器:
public class DummyReceiver extends BroadcastReceiver {
@Inject protected UnitProvider mUnitProvider;
@Override
public void onReceive(Context context, Intent intent) {
DependencyInjectionService.inject(this);
mUnitProvider.refreshCelsius();
}
}
实际上它有效,但我不确定这种情况可能导致的性能和可能的内存泄漏。对注入可能导致的性能和可能的滞后有任何想法吗?