I have an Android app consisting of several activities. Most of them should check if an active network is available:
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
...
}
I would like to move this code to a helper class to avoid having to write this code in all actions, but calls getSystemServiceare only allowed from Activity.
Another alternative is to transfer this code to inherit the parent action, but:
- Each activity is already distributed on android.app.Activity
- Some of my actions already apply to my.package.BaseActivity (Activity <- BaseActivity <- XXXActivity)
so I don’t really like this idea.
What do you recommend in this case? Is there any other alternative?