您好我正在处理需要通过代码打开/关闭gprs的应用程序。 我正在使用此方法来检查连接是否为On / Off。但我不知道如果关闭gprs怎么打开
码
NetworkInfo i = conMgr.getActiveNetworkInfo();
if (i == null)
return false;
if (!i.isConnected())
return false;
if (!i.isAvailable())
return false;
return true;
答案 0 :(得分:2)
将此代码用于Broadcast
Wifi或GPRS可用性
BroadcastReceiver br;
br = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION))
{
return;
}
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo aNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (!noConnectivity)
{
if (aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
{
Toast.makeText(getApplicationContext(), "Mobile Connected", Toast.LENGTH_LONG).show();
//Handle connected case
}
if (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
Toast.makeText(getApplicationContext(), "WIFI Connected", Toast.LENGTH_LONG).show();
}
}
else
{
if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) ||
(aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
{
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
}
}
}
};
在androidmanifest.xml中提供以下权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
不要忘记 register
和 unregister
BroadcastReceiver
。