我正在尝试创建这样的按钮(如果打开移动网络它是绿色,当它关闭时,它是灰色的):
我正在尝试使用此方法识别状态:
public Boolean isDataNetworkEnabled() {
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING) {
return true;
}
if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
return false;
}
return false;
}
它总是返回值' false'。即使移动网络开启后。实际上,如何识别移动网络的状态?
PS。我不是在询问如何打开/关闭移动网络,我问的是状态。我已经找到了打开/关闭的方法:
try {
Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
if (!isDataNetworkEnabled()) {
dataMtd.invoke(dataManager, true);
ivDataNetwork.setImageResource(R.drawable.ic_data_usage_black_48dp);
Toast.makeText(context, "Data network turned on", Toast.LENGTH_SHORT).show();
} else {
dataMtd.invoke(dataManager, false);
ivDataNetwork.setImageResource(R.drawable.ic_data_usage_grey600_48dp);
Toast.makeText(context, "Data network turned off", Toast.LENGTH_SHORT).show();
}
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
最佳做法是使用广播接收器
在manifest.xml中注册接收者
<receiver android:name="com.example.broadcastreciever2.NetworkChangeReciever" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
同时添加
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
定义您的接收者(根据您的使用情况使用)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class NetworkChangeReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (checkInternet(context)) {
Toast.makeText(context, "Network Available Do operations",
Toast.LENGTH_LONG).show();
}
}
boolean checkInternet(Context context) {
ServiceManager serviceManager = new ServiceManager(context);
if (serviceManager.isNetworkAvailable()) {
return true;
} else {
return false;
}
}
}
通过扩展ContextWrapper
来定义ServiceManagerimport android.content.Context;
import android.content.ContextWrapper;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ServiceManager extends ContextWrapper {
public ServiceManager(Context base) {
super(base);
}
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
}
答案 1 :(得分:0)
取代某些值......
if ( conMgr.getNetworkInfo(TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED
|| conMgr.getNetworkInfo(TYPE_WIFI).getState() == NetworkInfo.State.CONNECTING) {
return true;
}
if ( conMgr.getNetworkInfo(TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED
|| conMgr.getNetworkInfo(TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) {
return false;
}
因此,您确实在检查移动数据是否已连接,或者是否正在连接。听起来你不想要wifi,所以将TYPE_WIFI
更改为TYPE_MOBILE
?
事实上,做起来可能稍微好一点:
public boolean isMobileDataNetworkAccessable() {
return conMgr.getNetworkInfo(NetworkType.TYPE_MOBILE).isConnectedOrConnecting();
}