0

我想通过在移动设备中使用数据连接从我的主机服务器获取数据。当我的应用程序打开时,它应该通过数据连接自动连接到主机服务器。在连接丢失的情况下......我的服务应该通过启用来自不使用蓝牙或 WiFi 的移动设备的数据连接来自动连接到主机服务器。请帮助我摆脱这种情况

在我的代码中我用来检查数据连接是否

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

    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()) {


    //some task

        return true;                
    }
    else{

    Toast.makeText(this, "No internet connection", Toast.LENGTH_SHORT).show();

    }
    return false;
4

3 回答 3

2

按原样使用此类,并在您想要启用/禁用 DataConnection 时调用它的方法

package com.AZone.eabc;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import android.net.ConnectivityManager;
import android.util.Log;
import android.content.Context;

public class InternetControl {



    public static void EnableInternet(Context mycontext)
    {
        try {
            Log.i("Reached Enable", "I am here");
            setMobileDataEnabled(mycontext,true);
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void DisableInternet(Context mycontext)
    {
        try {
            Log.i("Reached Disable", "I am here");
            setMobileDataEnabled(mycontext,false);
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void setMobileDataEnabled(Context context , boolean enabled) throws NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
           final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);

           final Class conmanClass = Class.forName(conman.getClass().getName());

           final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
           iConnectivityManagerField.setAccessible(true);
           final Object iConnectivityManager = iConnectivityManagerField.get(conman);
           final Class iConnectivityManagerClass =  Class.forName(iConnectivityManager.getClass().getName());
           final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
           setMobileDataEnabledMethod.setAccessible(true);

           setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
        }

}

像这样从 AnyWhere 调用此类的方法

 InternetControl.EnableInternet(getBaseContext());

AndroidManifest在您的文件中添加以下权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />
于 2013-04-02T06:55:21.517 回答
1

看起来它不适用于某些带有 Android 4.1.X 的手机。我已经使用这种方法很长时间了,但它在三星 5282 Android 4.1.2 上崩溃了:

“NoSuchFieldException:mService”

看起来三星自己删除了 mService 字段,因为它仍然可以在 GrepCode 中找到:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/net/ConnectivityManager.java/

于 2013-08-30T14:12:07.213 回答
0

使用此方法检查您的设备是否已连接到 Internet。公共布尔检查互联网连接(){

    ConnectivityManager conMgr = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE);

    // ARE WE CONNECTED TO THE NET

    if (conMgr.getActiveNetworkInfo() != null

    && conMgr.getActiveNetworkInfo().isAvailable()

    && conMgr.getActiveNetworkInfo().isConnected()) {

        return true;

    } else {
        return false;

    }
} 

把它放在点击事件中:

boolean Connection;
Connection = checkInternetConnection();
if(Connection==false){
            //No internet connection    

            }
            else{
            //Here do what ever you do next
            }
于 2013-04-02T07:12:21.970 回答