private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
我已经尝试过上面的代码,但它没有激活移动数据。
如果互联网连接不可用,我需要检查互联网连接并显示开启窗口。该怎么做
答案 0 :(得分:0)
你可以这样做 如果您未连接到Internet,则可以显示“警报”对话框,如果用户单击启用INTERNET ,则打开如下所示的Internet页面
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("YOU ARE NOT CONNECTED WITH INTERNET")
.setCancelable(false)
.setPositiveButton("ENABLE INTERNET",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent callInternetsettingIntent = new Intent(
android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
startActivity(callInternetsettingIntent);
}
});
builder.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
试试这个并告诉我。
答案 1 :(得分:0)
检查Check network available in android 并在按钮上添加监听器或使用以下代码启用wifi:
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
确保将这些权限添加到AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
答案 2 :(得分:0)
尝试以下适合我的应用程序的代码工作
//在您的活动文件中添加两个方法
public boolean isMobileDataEnable()
{
boolean mobileDataEnabled = false; // Assume disabled
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
try
{
Class cmClass = Class.forName(cm.getClass().getName());
@SuppressWarnings("unchecked")
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true); // Make the method callable
// get the setting for "mobile data"
mobileDataEnabled = (Boolean)method.invoke(cm);
}
catch (Exception e)
{
// Some problem accessible private API and do whatever error handling you want here
}
return mobileDataEnabled;
}
public boolean toggleMobileDataConnection(boolean ON)
{
try
{
//create instance of connectivity manager and get system connectivity service
final ConnectivityManager conman = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
//create instance of class and get name of connectivity manager system service class
final Class conmanClass = Class.forName(conman.getClass().getName());
//create instance of field and get mService Declared field
final Field iConnectivityManagerField= conmanClass.getDeclaredField("mService");
//Attempt to set the value of the accessible flag to true
iConnectivityManagerField.setAccessible(true);
//create instance of object and get the value of field conman
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
//create instance of class and get the name of iConnectivityManager field
final Class iConnectivityManagerClass= Class.forName(iConnectivityManager.getClass().getName());
//create instance of method and get declared method and type
final Method setMobileDataEnabledMethod= iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
//Attempt to set the value of the accessible flag to true
setMobileDataEnabledMethod.setAccessible(true);
//dynamically invoke the iConnectivityManager object according to your need (true/false)
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON);
}
catch (Exception e)
{
}
return true;
}
//如果关闭,请检查您的互联网连接是否关闭,然后打开移动数据
if(isMobileDataEnable())
{
//Toast.makeText(getApplicationContext(), "Enable", Toast.LENGTH_SHORT).show();
}
else
{
toggleMobileDataConnection(true);
//Toast.makeText(getApplicationContext(), "Disable", Toast.LENGTH_SHORT).show();
}