0

モバイルのデータ接続を使用して、ホスト サーバーからデータを取得したいと考えています。私のアプリが開いているとき、データ接続によって自動的にホストサーバーに接続する必要があります。接続が失われた場合...私のサービスは、Bluetoothまたは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());

YouAndroidManifestファイルに次の権限を追加します

<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 に答える
0

この方法を使用して、デバイスがインターネットに接続されているかどうかを確認します。public boolean checkInternetConnection() {

    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 に答える