Reputation: 8297
my Android App is highly depending on an Internet connection.
It's an App for an Internetradio. The MainActivity shows current information like if there is a dj on air or if there is playlist (and refreshes its data from time to time).
also there are two activities that show more data from the internet (tracklist and upcoming shows).
currently i have a check in startup of my mainactivity where i test if wifi, mobile or wimax is connected and if none is connected i show a message and exit the app.
but i feel really bad with that. first of all, i think closing the app is not a good idea and also the check is once. so if i open the app and then turn plane mode the apps keeps running and trying to update the data -> failure...
what i now need, is a suggestion how i could improve the "am i online" tests.
My current idea is a BroadcastReceiver
for ConnectivityManager.CONNECTIVITY_ACTION
that then stores somewhere a boolean connected
that i can lookup in my other code.
someone got an idea on how to solve this problem in an as clean as possible way?
Thanks in advance!
Upvotes: 2
Views: 1754
Reputation: 1336
The cleanest way is registering a programmatically registered Recever for the Action you specified in your Activities.
Add this to your onCreate()
method.
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(AWCMClientConstants.AWCM_STARTED);
getActivity().registerReceiver(mNetworkStateChangedReceiver, filter);
Add this as an inner class to your Activities.
private class NetworkStateChangedReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// Detect from Intent what's the change and Do what you want to.
}
}
Upvotes: 0
Reputation: 2004
public static boolean isConnected(Context context) {
final ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) {
return true;
} else {
return false;
}
}
You can use this method to check Internet status.
Upvotes: 0
Reputation: 1073
Add AppStatus class in your package
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class AppStatus {
private static AppStatus instance = new AppStatus();
private ConnectivityManager connectivityManager;
private static Context context;
private boolean connected = false;
public static AppStatus getInstance(Context ctx) {
context = ctx;
return instance;
}
// Check internet connection available
public Boolean isOnline(Context con) {
try {
connectivityManager = (ConnectivityManager) con
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isAvailable()
&& networkInfo.isConnected();
return connected;
} catch (Exception e) {
Log.v("connectivity", e.toString());
}
return connected;
}
}
Now get this class method to find internet availability as shown below:
if (AppStatus.getInstance(Activity.this).isOnline(
Activity.this)) {
}
Add these permission in AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Upvotes: 1