【问题标题】:check internet connection and show turn on window if unavailble检查互联网连接并在不可用时显示打开窗口
【发布时间】:2016-04-06 17:45:16
【问题描述】:
   private boolean isNetworkConnected() {

            ConnectivityManager cm = (ConnectivityManager)
                                    getSystemService(Context.CONNECTIVITY_SERVICE);

            return cm.getActiveNetworkInfo() != null;  
      } 

我已经尝试了上面的代码,但它没有激活移动数据。

如果互联网连接不可用,我需要检查互联网连接并显示打开窗口。如何做到这一点

【问题讨论】:

  • 不确定是否可以直接打开移动数据,可能不行,但您可以显示一个对话框并将用户发送到设置页面。
  • 是的,我知道我不能直接打开移动数据。

标签: android


【解决方案1】:

你可以这样做 如果您没有连接到 Internet,您可以显示一个警报对话框,如果用户单击 ENABLE 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();

试试这个并告诉我。

【讨论】:

  • 它有效。首先我检查互联网连接并添加此代码
  • @Rukshanwithanage 乐于助人
【解决方案2】:

检查这个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>

【讨论】:

    【解决方案3】:

    尝试以下代码,非常适合我的应用程序

    //在你的Activity文件中放两个方法

    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();    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      相关资源
      最近更新 更多