I have an activity where I can tap a button to start a BroadcastReceiver
, which listens for changes of the wifi connection. Now when the activity is showing or I leave the app (press the home button) while the activity is showing the receiver works fine. If I however end the activity or kill the app as a whole (that is swipe it away in the recent tasks list) the receiver does not work anymore. The structure of my activity which includes the receiver looks like this:
MainActivity.java
...
public class MainActivity extends AppCompatActivity {
...
public BroadcastReceiver wifiStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
...
}
}
}
I have read about Services
and JobSchedulers
and figured that I will probably have to use one of these to get my receiver running at anytime. I just don't really know how to implement these. Can anyone explain to me how to do that?
Also, is it a good idea to include the BroadcastReceiver
in the MainActivity
class or should I rather define it in a separate class?
Thanks a lot!