当前位置: 首页>>代码示例>>Java>>正文


Java NetworkCallback类代码示例

本文整理汇总了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);
    }
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:27,代码来源:NetworkChangeNotifierTest.java

示例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));
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:10,代码来源:LollipopNetworkObservingStrategyTest.java

示例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();
        }
      });
}
 
开发者ID:google,项目名称:samba-documents-provider,代码行数:16,代码来源:SambaProviderApplication.java

示例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();
            }
        }
    };
}
 
开发者ID:nishkarsh,项目名称:android-wificonnect,代码行数:20,代码来源:WifiUtil.java

示例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);
}
 
开发者ID:Piasy,项目名称:AppRTC-Android,代码行数:10,代码来源:NetworkMonitorAutoDetect.java

示例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);
}
 
开发者ID:Piasy,项目名称:AppRTC-Android,代码行数:9,代码来源:NetworkMonitorAutoDetect.java

示例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);
  }
}
 
开发者ID:Piasy,项目名称:AppRTC-Android,代码行数:8,代码来源:NetworkMonitorAutoDetect.java

示例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;
  }
}
 
开发者ID:Piasy,项目名称:AppRTC-Android,代码行数:39,代码来源:NetworkMonitorAutoDetect.java

示例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;
  }
}
 
开发者ID:lgyjg,项目名称:AndroidRTC,代码行数:35,代码来源:NetworkMonitorAutoDetect.java

示例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());
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:8,代码来源:ShadowConnectivityManagerWithCallback.java

示例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());
  }
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:13,代码来源:ShadowConnectivityManagerWithCallback.java

示例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));
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:11,代码来源:LollipopNetworkObservingStrategyTest.java

示例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();
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:16,代码来源:LollipopNetworkObservingStrategyTest.java

示例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));
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:9,代码来源:MarshmallowNetworkObservingStrategyTest.java

示例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);
}
 
开发者ID:ChiangC,项目名称:FMTech,代码行数:14,代码来源:NetworkChangeNotifierAutoDetect.java


注:本文中的android.net.ConnectivityManager.NetworkCallback类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。