本文整理汇总了Java中android.net.ConnectivityManager.NetworkCallback类的典型用法代码示例。如果您正苦于以下问题:Java NetworkCallback类的具体用法?Java NetworkCallback怎么用?Java NetworkCallback使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NetworkCallback类属于android.net.ConnectivityManager包,在下文中一共展示了NetworkCallback类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testConnectivityManagerDelegateDoesNotCrash
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
@UiThreadTest
@MediumTest
@Feature({"Android-AppBase"})
public void testConnectivityManagerDelegateDoesNotCrash() {
ConnectivityManagerDelegate delegate =
new ConnectivityManagerDelegate(getInstrumentation().getTargetContext());
delegate.getNetworkState();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// getNetworkState(Network) doesn't crash upon invalid Network argument.
Network invalidNetwork = netIdToNetwork(NetId.INVALID);
NetworkState invalidNetworkState = delegate.getNetworkState(invalidNetwork);
assertFalse(invalidNetworkState.isConnected());
assertEquals(-1, invalidNetworkState.getNetworkType());
assertEquals(-1, invalidNetworkState.getNetworkSubType());
Network[] networks = delegate.getAllNetworksUnfiltered();
if (networks.length >= 1) {
delegate.getNetworkState(networks[0]);
}
delegate.getDefaultNetId();
NetworkCallback networkCallback = new NetworkCallback();
NetworkRequest networkRequest = new NetworkRequest.Builder().build();
delegate.registerNetworkCallback(networkRequest, networkCallback);
delegate.unregisterNetworkCallback(networkCallback);
}
}
示例2: shouldRegisterWithDefaultNetworkRequest
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
@Test()
public void shouldRegisterWithDefaultNetworkRequest() {
NetworkRequest defaultRequest = new NetworkRequest.Builder().build();
ConnectivityManager manager = setUpManagerWithNetworkRequest(null);
sut.observe().subscribeWith(testObserver).assertSubscribed();
verify(manager).registerNetworkCallback(eq(defaultRequest), any(NetworkCallback.class));
}
示例3: registerNetworkCallback
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
private void registerNetworkCallback(Context context) {
final ConnectivityManager manager =
(ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
manager.registerNetworkCallback(
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.build(),
new NetworkCallback() {
@Override
public void onAvailable(Network network) {
mSambaClient.reset();
}
});
}
示例4: networkCallback
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
@TargetApi(LOLLIPOP)
NetworkCallback networkCallback(final String networkSSID,
final NetworkStateChangeListener listener) {
return new NetworkCallback() {
@Override
public void onAvailable(Network network) {
NetworkInfo networkInfo = manager.getNetworkInfo(network);
logger.i("Network is Available. Network Info: " + networkInfo);
if (WifiHelper.areEqual(networkInfo.getExtraInfo(), networkSSID)) {
manager.unregisterNetworkCallback(this);
networkCallback = null;
bindToRequiredNetwork(network);
logger.i(format("Bound application to use %s network", networkSSID));
listener.onNetworkBound();
}
}
};
}
示例5: registerNetworkCallback
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
/** Only callable on Lollipop and newer releases. */
@SuppressLint("NewApi")
public void registerNetworkCallback(NetworkCallback networkCallback) {
connectivityManager.registerNetworkCallback(
new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build(),
networkCallback);
}
示例6: requestMobileNetwork
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
/** Only callable on Lollipop and newer releases. */
@SuppressLint("NewApi")
public void requestMobileNetwork(NetworkCallback networkCallback) {
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
connectivityManager.requestNetwork(builder.build(), networkCallback);
}
示例7: releaseCallback
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
@SuppressLint("NewApi")
public void releaseCallback(NetworkCallback networkCallback) {
if (supportNetworkCallback()) {
Logging.d(TAG, "Unregister network callback");
connectivityManager.unregisterNetworkCallback(networkCallback);
}
}
示例8: NetworkMonitorAutoDetect
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
/**
* Constructs a NetworkMonitorAutoDetect. Should only be called on UI thread.
*/
@SuppressLint("NewApi")
public NetworkMonitorAutoDetect(Observer observer, Context context) {
this.observer = observer;
this.context = context;
connectivityManagerDelegate = new ConnectivityManagerDelegate(context);
wifiManagerDelegate = new WifiManagerDelegate(context);
final NetworkState networkState = connectivityManagerDelegate.getNetworkState();
connectionType = getConnectionType(networkState);
wifiSSID = getWifiSSID(networkState);
intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
if (PeerConnectionFactory.fieldTrialsFindFullName("IncludeWifiDirect").equals("Enabled")) {
wifiDirectManagerDelegate = new WifiDirectManagerDelegate(observer, context);
}
registerReceiver();
if (connectivityManagerDelegate.supportNetworkCallback()) {
// On Android 6.0.0, the WRITE_SETTINGS permission is necessary for
// requestNetwork, so it will fail. This was fixed in Android 6.0.1.
NetworkCallback tempNetworkCallback = new NetworkCallback();
try {
connectivityManagerDelegate.requestMobileNetwork(tempNetworkCallback);
} catch (java.lang.SecurityException e) {
Logging.w(TAG, "Unable to obtain permission to request a cellular network.");
tempNetworkCallback = null;
}
mobileNetworkCallback = tempNetworkCallback;
allNetworkCallback = new SimpleNetworkCallback();
connectivityManagerDelegate.registerNetworkCallback(allNetworkCallback);
} else {
mobileNetworkCallback = null;
allNetworkCallback = null;
}
}
示例9: NetworkMonitorAutoDetect
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
/**
* Constructs a NetworkMonitorAutoDetect. Should only be called on UI thread.
*/
@SuppressLint("NewApi")
public NetworkMonitorAutoDetect(Observer observer, Context context) {
this.observer = observer;
this.context = context;
connectivityManagerDelegate = new ConnectivityManagerDelegate(context);
wifiManagerDelegate = new WifiManagerDelegate(context);
final NetworkState networkState = connectivityManagerDelegate.getNetworkState();
connectionType = getConnectionType(networkState);
wifiSSID = getWifiSSID(networkState);
intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver();
if (connectivityManagerDelegate.supportNetworkCallback()) {
// On Android 6.0.0, the WRITE_SETTINGS permission is necessary for
// requestNetwork, so it will fail. This was fixed in Android 6.0.1.
NetworkCallback tempNetworkCallback = new NetworkCallback();
try {
connectivityManagerDelegate.requestMobileNetwork(tempNetworkCallback);
} catch (java.lang.SecurityException e) {
Logging.w(TAG, "Unable to obtain permission to request a cellular network.");
tempNetworkCallback = null;
}
mobileNetworkCallback = tempNetworkCallback;
allNetworkCallback = new SimpleNetworkCallback();
connectivityManagerDelegate.registerNetworkCallback(allNetworkCallback);
} else {
mobileNetworkCallback = null;
allNetworkCallback = null;
}
}
示例10: registerNetworkCallback
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
@Implementation(minSdk = LOLLIPOP)
@RequiresApi(LOLLIPOP)
public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
networkCallbacks.add(networkCallback);
// simulate available connection
networkCallback.onAvailable(getActiveNetwork());
}
示例11: unregisterNetworkCallback
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
@RequiresApi(LOLLIPOP)
@Implementation(minSdk = LOLLIPOP)
public void unregisterNetworkCallback(NetworkCallback networkCallback) {
if (networkCallback == null) {
throw new IllegalArgumentException("Invalid NetworkCallback");
}
if (networkCallbacks.contains(networkCallback)) {
networkCallbacks.remove(networkCallback);
// simulate lost connection
networkCallback.onLost(getActiveNetwork());
}
}
示例12: shouldRegisterWithCustomNetworkRequest
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
@Test()
public void shouldRegisterWithCustomNetworkRequest() {
NetworkRequest customRequest =
new NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).build();
ConnectivityManager manager = setUpManagerWithNetworkRequest(customRequest);
sut.observe().subscribeWith(testObserver).assertSubscribed();
verify(manager).registerNetworkCallback(eq(customRequest), any(NetworkCallback.class));
}
示例13: shouldDisposeWithException_whenObserverDisposed
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
@Test
public void shouldDisposeWithException_whenObserverDisposed() {
ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
doReturn(connectivityManager).when(context).getSystemService(CONNECTIVITY_SERVICE);
doThrow(Exception.class).when(connectivityManager)
.unregisterNetworkCallback(any(NetworkCallback.class));
sut = spy(new LollipopNetworkObservingStrategy(context));
sut.observe().subscribeWith(testObserver).assertSubscribed();
testObserver.dispose();
verify(sut).dispose();
verify(sut).onError(anyString(), any(Exception.class));
testObserver.isDisposed();
}
示例14: setupNetworkCallbackUnregisterException
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
private void setupNetworkCallbackUnregisterException() {
ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
doReturn(connectivityManager).when(context).getSystemService(Context.CONNECTIVITY_SERVICE);
doThrow(Exception.class).when(connectivityManager)
.unregisterNetworkCallback(any(NetworkCallback.class));
sut = spy(new MarshmallowNetworkObservingStrategy(context));
}
示例15: a
import android.net.ConnectivityManager.NetworkCallback; //导入依赖的package包/类
public final void a()
{
if (!this.h) {}
do
{
return;
this.c.unregisterReceiver(this);
this.h = false;
} while (this.f == null);
rja localrja = this.e;
ConnectivityManager.NetworkCallback localNetworkCallback = this.f;
localrja.a.unregisterNetworkCallback(localNetworkCallback);
}