public interface Connection
A connection implementation may be obtained via Connection.PROVIDER.getConnection(Context).
Below are listed methods provided by the this interface:
getConnectionType()getConnectionInfo(Connection.ConnectionType)isConnected()isConnected(Connection.ConnectionType)isConnectedOrConnecting()isConnectedOrConnecting(Connection.ConnectionType)isAvailable(Connection.ConnectionType)
public class ConnectionActivity extends Activity {
// Connection listener.
private final Connection.OnConnectionListener CONNECTION_LISTENER = new Connection.OnConnectionListener() {
@Override
public void onConnectionEstablished(@NonNull ConnectionType type, @Nullable NetworkInfo info, @NonNull Context context) {
// Handle here new established connection.
}
@Override
public void onConnectionLost(@NonNull ConnectionType type, @NonNull Context context) {
// Handle here lost connection.
}
}
// Connection implementation.
private Connection mConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
// Get the connection API implementation.
this.mConnection = Connection.PROVIDER.getConnection(this);
// Register connection listener to handle changes in network connection.
mConnection.registerOnConnectionListener(CONNECTION_LISTENER);
// ...
}
@Override
protected void onResume() {
super.onResume();
// Register connection broadcast receiver so the Connection implementation will receive
// info about the established/lost connection.
mConnection.registerConnectionReceiver(this);
}
@Override
protected void onPause() {
super.onPause();
// Un-register all registered receivers.
mConnection.unregisterConnectionReceiver(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Un-register connection listener.
mConnection.unregisterOnConnectionListener(CONNECTION_LISTENER);
}
}
| Modifier and Type | Interface and Description |
|---|---|
static class |
Connection.ConnectionType
Represents type of the Android device's connection.
|
static interface |
Connection.OnConnectionListener
Listener that may be used to receive callback with info about changed network connection.
|
static interface |
Connection.Provider
Interface for provider that may be used to access implementation of
Connection. |
| Modifier and Type | Field and Description |
|---|---|
static Connection.Provider |
PROVIDER
A
Connection.Provider implementation that may be used to access implementation of Connection. |
| Modifier and Type | Method and Description |
|---|---|
android.net.NetworkInfo |
getConnectionInfo(Connection.ConnectionType connectionType)
Returns the current info about the requested connectionType.
|
Connection.ConnectionType |
getConnectionType()
Returns type of the current established connection.
|
boolean |
isAvailable(Connection.ConnectionType connectionType)
Checks whether the Android device can establish connection for the requested connectionType
|
boolean |
isConnected()
Checks whether the Android device has some connection currently established.
|
boolean |
isConnected(Connection.ConnectionType connectionType)
Checks whether the Android device has established the connection for the requested connectionType.
|
boolean |
isConnectedOrConnecting()
Checks whether the Android device has some connection or is currently in the process to establish
some connection.
|
boolean |
isConnectedOrConnecting(Connection.ConnectionType connectionType)
Checks whether the Android device has established the connection or is currently in the process
to establish the connection for the requested connectionType.
|
boolean |
isConnectionReceiverRegistered()
Returns flag indicating whether
ConnectionStateReceiver is currently registered or not. |
void |
registerConnectionReceiver(android.content.Context context)
Registers
ConnectionStateReceiver to receive broadcasts about the current connection
state. |
void |
registerOnConnectionListener(Connection.OnConnectionListener listener)
Registers a callback to be invoked when some connection change occur.
|
void |
unregisterConnectionReceiver(android.content.Context context)
Un-registers registered
ConnectionStateReceiver. |
void |
unregisterOnConnectionListener(Connection.OnConnectionListener listener)
Un-registers the given connection callback.
|
static final Connection.Provider PROVIDER
Connection.Provider implementation that may be used to access implementation of Connection.boolean isConnectedOrConnecting()
See NetworkInfo.isConnectedOrConnecting() for additional info.
True if some connection is established or will be established in a while,
false otherwise.isConnected(),
getConnectionType()boolean isConnected()
See NetworkInfo.isConnected() for additional info.
True if some connection is established, false otherwise.isConnectedOrConnecting(),
getConnectionType()boolean isConnectedOrConnecting(Connection.ConnectionType connectionType)
See NetworkInfo.isConnectedOrConnecting() for additional info.
connectionType - Type of the connection of which the current state should be checked.True if connection is established or is in the process of being established,
false otherwise.boolean isConnected(Connection.ConnectionType connectionType)
See NetworkInfo.isConnected() for additional info.
connectionType - Type of the connection of which the current state should be checked.True if connection is established, false otherwise.isConnectedOrConnecting(Connection.ConnectionType)boolean isAvailable(Connection.ConnectionType connectionType)
See NetworkInfo.isAvailable() for additional info.
connectionType - Type of the connection of which availability should be checked.True if the requested connection can be established, false
if establishing of the requested connection is not possible due to current network conditions.Connection.ConnectionType getConnectionType()
Connection.ConnectionType values or Connection.ConnectionType.UNAVAILABLE
if there is no connection currently available.android.net.NetworkInfo getConnectionInfo(Connection.ConnectionType connectionType)
connectionType - Type of the connection of which current info should be obtained.getConnectionType()void registerOnConnectionListener(Connection.OnConnectionListener listener)
listener - Callback to register.registerConnectionReceiver(Context)void unregisterOnConnectionListener(Connection.OnConnectionListener listener)
listener - Callback to un-register.void registerConnectionReceiver(android.content.Context context)
ConnectionStateReceiver to receive broadcasts about the current connection
state.context - The main activity of application.registerOnConnectionListener(Connection.OnConnectionListener),
unregisterConnectionReceiver(Context)boolean isConnectionReceiverRegistered()
ConnectionStateReceiver is currently registered or not.True if receiver is registered, false otherwise.void unregisterConnectionReceiver(android.content.Context context)
ConnectionStateReceiver.context - Context in which was connection receiver registered before.registerConnectionReceiver(Context)