ConnectivityManager Network 在指定只使用 wifi 时为 null
ConnectivityManager Network is null when specifying to use only wifi
我正在尝试通过 ConnectivityManager 存储 wifi 网络(最终是蜂窝网络),以便稍后在我的程序中使用,以同时通过本地 Wifi 或蜂窝网络专门发送数据。我已按照此 post (Make Android simultaneously use WiFi to talk to a device and mobile data to talk to a server? and ) 尝试获取它 运行。但是 mWifiNetwork
为空。使用调试模式,变量 mWifiNetwork
显示为 "108"
,然后当“'onAvailable'”方法完成时,变量变回 null。不确定为什么变量没有正确存储网络。
我创建了一个 class 在 onResume 中调用的 forceWifiNetwork
forceWifiNetwork.java
public class forceWifiNetwork {
//setting up a context variable and method to use "getSystemService"
// this way we do not need to extend MainActivity class to this class
// But when we initialize this class in main activity we will have to pass in "this" to the class as a parameter
Context mContext;
private ConnectivityManager.NetworkCallback mWifiNetworkCallback;
public Network mWifiNetwork;
final ConnectivityManager manager;
public forceWifiNetwork(Context mContext){
this.mContext = mContext;
manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
}
public void cellConnect(){
if (mWifiNetworkCallback == null){
// Init only once
mWifiNetworkCallback = new ConnectivityManager.NetworkCallback(){
@Override
public void onAvailable(final Network network){
try{
//Save this network for later use
mWifiNetwork = network;
}catch(Exception e){
Log.i("onAvailable ERROR","Error in onAvailable method");
}
}
};
}
NetworkRequest.Builder wifiBuilder;
wifiBuilder = new NetworkRequest.Builder();
//set the transport type to do wifi
wifiBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
//wifiBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
manager.requestNetwork(wifiBuilder.build(),mWifiNetworkCallback);
}
}
onResume(在MainActivity.java)
@Override
public void onResume(){
super.onResume();
try {
forceWifiNetwork myWifiNetwork = new forceWifiNetwork(this);
myWifiNetwork.cellConnect();
if (myWifiNetwork.mWifiNetwork == null) {
Log.i("onResume", "mWifiNetwork is null");
} else {
Log.i("onResume", "mWifiNetwork is not null");
}
}catch(Exception e){
Log.i("ERROR IN ONRESUME","error");
e.printStackTrace();
}
}
我目前在 AndroidManifest.xml
中也有这些权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
将 forceWifiNetwork
的开始移动到 onCreate
而不是 onResume
。我相信 ConnectivityManager.Networkcallback
中的 onAvailable
方法可能具有一些底层异步属性。它看起来像这样。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating network variables that store wifi and cell networks
myWifiNetwork = new forceWifiNetwork(this);
try {
myWifiNetwork.cellConnect();
if (myWifiNetwork.mWifiNetwork == null) {
Log.i("onCreate", "mWifiNetwork is null");
} else {
Log.i("onCreate", "mWifiNetwork is not null");
}
}catch(Exception e){
Log.i("Error in onCreate","error creating network");
e.printStackTrace();
}
}
我正在尝试通过 ConnectivityManager 存储 wifi 网络(最终是蜂窝网络),以便稍后在我的程序中使用,以同时通过本地 Wifi 或蜂窝网络专门发送数据。我已按照此 post (Make Android simultaneously use WiFi to talk to a device and mobile data to talk to a server? and mWifiNetwork
为空。使用调试模式,变量 mWifiNetwork
显示为 "108"
,然后当“'onAvailable'”方法完成时,变量变回 null。不确定为什么变量没有正确存储网络。
我创建了一个 class 在 onResume 中调用的 forceWifiNetwork
forceWifiNetwork.java
public class forceWifiNetwork {
//setting up a context variable and method to use "getSystemService"
// this way we do not need to extend MainActivity class to this class
// But when we initialize this class in main activity we will have to pass in "this" to the class as a parameter
Context mContext;
private ConnectivityManager.NetworkCallback mWifiNetworkCallback;
public Network mWifiNetwork;
final ConnectivityManager manager;
public forceWifiNetwork(Context mContext){
this.mContext = mContext;
manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
}
public void cellConnect(){
if (mWifiNetworkCallback == null){
// Init only once
mWifiNetworkCallback = new ConnectivityManager.NetworkCallback(){
@Override
public void onAvailable(final Network network){
try{
//Save this network for later use
mWifiNetwork = network;
}catch(Exception e){
Log.i("onAvailable ERROR","Error in onAvailable method");
}
}
};
}
NetworkRequest.Builder wifiBuilder;
wifiBuilder = new NetworkRequest.Builder();
//set the transport type to do wifi
wifiBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
//wifiBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
manager.requestNetwork(wifiBuilder.build(),mWifiNetworkCallback);
}
}
onResume(在MainActivity.java)
@Override
public void onResume(){
super.onResume();
try {
forceWifiNetwork myWifiNetwork = new forceWifiNetwork(this);
myWifiNetwork.cellConnect();
if (myWifiNetwork.mWifiNetwork == null) {
Log.i("onResume", "mWifiNetwork is null");
} else {
Log.i("onResume", "mWifiNetwork is not null");
}
}catch(Exception e){
Log.i("ERROR IN ONRESUME","error");
e.printStackTrace();
}
}
我目前在 AndroidManifest.xml
中也有这些权限<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
将 forceWifiNetwork
的开始移动到 onCreate
而不是 onResume
。我相信 ConnectivityManager.Networkcallback
中的 onAvailable
方法可能具有一些底层异步属性。它看起来像这样。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating network variables that store wifi and cell networks
myWifiNetwork = new forceWifiNetwork(this);
try {
myWifiNetwork.cellConnect();
if (myWifiNetwork.mWifiNetwork == null) {
Log.i("onCreate", "mWifiNetwork is null");
} else {
Log.i("onCreate", "mWifiNetwork is not null");
}
}catch(Exception e){
Log.i("Error in onCreate","error creating network");
e.printStackTrace();
}
}