每当手机的蜂窝数据连接发生更改时,我都试图接收回叫。我基本上需要知道何时关闭和何时打开。
我以前是通过TelephonyManager
和PhoneStateListener.LISTEN_SERVICE_STATE
来做到这一点的,但是现在我想将ConnectionManager
和registerNetworkCallback
一起使用。
以下代码示例已在Android Emulator(API 28),HUAWEI P20 Lite(API 26),HTC One M8(API 23)和Motorola G6(API 26)上试用。当我打开/关闭移动数据或激活飞行模式时,它们都没有调用任何回调。
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
connManager.registerNetworkCallback(builder.build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.d("Conman Test", "onAvailable");
}
@Override
public void onLost(Network network) {
Log.d("Conman Test", "onLost");
}
@Override
public void onUnavailable() {
Log.d("Conman Test", "onUnavailable");
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
Log.d("Conman Test", "onCapabilitiesChanged");
}
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
Log.d("Conman Test", "onLinkPropertiesChanged");
}
});
我尝试过NetworkCapabilities.TRANSPORT_WIFI
,它在所有设备上都能正常工作。那么为什么不NetworkCapabilities.TRANSPORT_CELLULAR
呢?
答案 0 :(得分:-1)
我还没有尝试使用NetworkCapabilities
解决此问题,但是我使用了另一种方法来确定我的设备是通过蜂窝网络还是通过wifi连接。
我刚刚注册了接收者
<receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
我添加了权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
然后我做了以下
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if(activeNetwork ==null){
Log.d("Connectivity","Not connected");
return;
}
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
if(isConnected && isWiFi){
Log.d("Connectivity","WIFI");
}else{
Log.d("Connectivity","CELLULAR");
}
}
}
对于Oreo设备,您可能需要在活动或服务中注册接收者,而不是在清单中注册
例如
public class MainActivity extends Activity {
IntentFilter filter1;
@Override
public void onCreate() {
filter1 = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(myReceiver, filter1);
}
//The BroadcastReceiver that listens for network change
private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("android.net.conn.CONNECTIVITY_CHANGE")) {
Log.d(TAG,"connectivity change");
ConnectivityManager cm =
(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if(activeNetwork ==null){
Log.d("Connectivity","Not connected");
return;
}
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
if(isConnected && isWiFi){
Log.d("Connectivity","WIFI");
}else{
Log.d("Connectivity","CELLULAR");
}
}
}
};
@Override
public void onDestroy() {
unregisterReceiver(myReceiver);
}
}