I am bit confused what is the right place to use Service (for background task).
This is my scenario:
I have a class that extends Broadcast receiver. It receives WiFi state changes. Depending on the state change, I call another class. This is a pure Java class, not extending any class.
This class is instantiated by passing the Context
(received with the broadcast receiver).
I need to pass the Context
because, among other things, I access SharedPreferences
, display a notification, etc. But this not a foreground activity.
Is this the correct way? Or should my class extend Service
and work as a background task?
Is it wrong to pass the Context
to initiate a class?
For example,
public class WifiStateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
...
WifiChangeReceptionClass wifiChanged = new WifiChangeReceptionClass(context);
wifiChanged.showNotification();
...
}
What is wrong with this approach?