Last active
December 20, 2015 18:49
-
-
Save Alfionte/6178590 to your computer and use it in GitHub Desktop.
Android Activity to help you out with manage connections. Need ACCESS_NETWORK_STATE permission, add this to your Manifest <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> Use your package and enjoy it!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.use.your.package.here; | |
import android.app.Activity; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
/** | |
* @author Gabriele Porcelli | |
* | |
* Extends this Activity and implement connected() && disconnected(). Out of the box! | |
* | |
* Require ACCESS_NETWORK_STATE, add this to your Manifest | |
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > | |
</uses-permission> | |
*/ | |
public abstract class ConnectionActivity extends Activity { | |
private BroadcastReceiver ConnectionReceiver = new BroadcastReceiver() { | |
public void onReceive(Context context, Intent intent) { | |
ConnectivityManager cm = (ConnectivityManager) context | |
.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();// may return null | |
if (activeNetwork != null) { | |
boolean isConnected = activeNetwork.isConnected(); | |
if (isConnected) { | |
connected(activeNetwork.getType()); | |
} else { | |
disconnected(); | |
} | |
} else { | |
disconnected(); | |
} | |
}; | |
}; | |
protected void onResume() { | |
super.onResume(); | |
registerReceiver(ConnectionReceiver, new IntentFilter( | |
ConnectivityManager.CONNECTIVITY_ACTION)); | |
}; | |
@Override | |
protected void onPause() { | |
unregisterReceiver(ConnectionReceiver); | |
super.onPause(); | |
} | |
/** | |
* Handle android network connected | |
* @param Connection type | |
* Hint runInUIThread | |
* | |
* example | |
* | |
* switch (type) { | |
* case ConnectivityManager.TYPE_MOBILE: | |
* Log.i(ServiceConnection.class.getSimpleName(), | |
* "Connection type: Mobile"); break; | |
* case ConnectivityManager.TYPE_WIFI: | |
* Log.i(ServiceConnection.class.getSimpleName(), | |
* "Connection type: Wifi"); break; | |
* // API 13 | |
* case ConnectivityManager.TYPE_ETHERNET: | |
* Log.i(ServiceConnection.class.getSimpleName(), | |
* "Connection type: Ethernet"); break; | |
* // API 8 | |
* case ConnectivityManager.TYPE_WIMAX: | |
* Log.i(ServiceConnection.class.getSimpleName(), | |
* "Connection type: WiMax"); break; | |
* default: throw new | |
* IllegalStateException( | |
* "Connectivity type not supported! ConnectivityManager value:" | |
* + type); | |
* } | |
*/ | |
protected abstract void connected(int type); | |
/** | |
* Handle Android network disconnected | |
* Hint runInUIThread | |
*/ | |
protected abstract void disconnected(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment