-2

インターネットを使用してデータベースにデータを挿入するコードを作成しましたが、インターネットへの接続が利用できない場合、データベースが空でない場合、アプリケーションはデータベースからデータを取得します。

アプリケーションのインターネット接続をチェックするコードを作成するにはどうすればよいですか?

4

6 に答える 6

2

私はあなたのためのネットワークユーティリティを持っています。

public final class NetworkUtils {
public static final byte CONNECTION_OFFLINE = 1;
public static final byte CONNECTION_WIFI = 2;
public static final byte CONNECTION_ROAMING = 3;
public static final byte CONNECTION_SLOW = 4;
public static final byte CONNECTION_FAST = 5;

private static String sUserId;

private NetworkUtils() {}


/**
 * Check if the device is connected to the internet (mobile network or
 * WIFI).
 */
public static boolean isOnline(Context _context) {
    boolean online = false;

    TelephonyManager tmanager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
    if (tmanager != null) {
        if (tmanager.getDataState() == TelephonyManager.DATA_CONNECTED) {
            // Mobile network
            online = true;
        } else {
            // WIFI
            ConnectivityManager cmanager = (ConnectivityManager) _context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (cmanager != null) {
                NetworkInfo info = cmanager.getActiveNetworkInfo();
                if (info != null)
                    online = info.isConnected();
            }
        }
    }

    return online;
}

/**
 * Get the User Agent String in the format
 * AppName + AppVersion + Model + ReleaseVersion + Locale
 */
public static String getUserAgentString(Context _c, String _appName) {
    if(_appName == null)
        _appName = "";

    String agent = _appName + " " + BackendUtil.getAppVersionString(_c) + " (" + Build.MODEL + "; Android "
            + Build.VERSION.RELEASE + "; " + Locale.getDefault() + ")";

    if(agent.startsWith(" "))
        agent = agent.substring(1);

    return agent;
}

/**
 * Evaluate the current network connection and return the
 * corresponding type, e.g. CONNECTION_WIFI.
 */
public static byte getCurrentNetworkType(Context _context){
    NetworkInfo netInfo = ((ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if(netInfo == null)
        return CONNECTION_OFFLINE;

    if(netInfo.getType() == ConnectivityManager.TYPE_WIFI)
        return CONNECTION_WIFI;

    if(netInfo.isRoaming())
        return CONNECTION_ROAMING;

    if(!(netInfo.getType() == ConnectivityManager.TYPE_MOBILE 
            &&  (netInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_UMTS 
              || netInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_HSDPA
              || netInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_HSUPA
              || netInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_HSPA 
              || netInfo.getSubtype() == 13 // NETWORK_TYPE_LTE
              || netInfo.getSubtype() == 15))) // NETWORK_TYPE_HSPAP  
         {

        return CONNECTION_SLOW;
    }

    return CONNECTION_FAST;
}


/**
 * Return the current IP adresse of the device or null if it could not be
 * found.
 */
public static String getIpAdress() {
    String result = null;
    try {
        for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
            NetworkInterface iface = interfaces.nextElement();
            for (Enumeration<InetAddress> adresses = iface.getInetAddresses(); adresses.hasMoreElements();) {
                InetAddress ip = adresses.nextElement();
                if (!ip.isLoopbackAddress())
                    result = ip.getHostAddress();
            }
        }
    } catch (SocketException _e) {
        LL.error("Could not find device's ip adress");
    }
    return result;
}


/**
 * Return a MD5 hash of the device id.
 */
public static synchronized String getUserId(Context _context) {
    if (sUserId == null) {
        TelephonyManager tm = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
        String id = tm.getDeviceId();
        try {
            MessageDigest digester = MessageDigest.getInstance("MD5");
            digester.update(id.getBytes());
            byte[] digest = digester.digest();

            // Convert to hex string
            BigInteger converter = new BigInteger(1, digest);
            String md5 = converter.toString(16);
            while (md5.length() < 32)
                md5 = "0" + md5;
            sUserId = md5;
        } catch (NoSuchAlgorithmException _e) {
            LL.error("Could not find MD5");
        }
    }
    return sUserId;
}

}

于 2013-08-14T12:04:12.103 に答える
1

http://www.androidhive.info/2012/07/android-detect-internet-connection-status/

チュートリアルに従ってください...あなたを助けます...

于 2013-08-14T12:05:14.867 に答える
1
Boolean online = isOnline();

        if (online == false) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set title
            alertDialogBuilder.setTitle("Cellular Data is Turned Off!");

            // set dialog message
            alertDialogBuilder
                    .setMessage(
                            "Please turn on cellular data or use Wi-Fi to access data!")
                    .setCancelable(false)
                    .setNeutralButton("Settings",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {

                                    startActivity(new Intent(
                                            android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                                }
                            })
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
于 2013-08-14T12:07:22.643 に答える
1

以下のようなメソッドを作成します。

public boolean checkInternet() { 
        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
        State mobile = conMan.getNetworkInfo(0).getState(); 
        State wifi = conMan.getNetworkInfo(1).getState(); 
        if (mobile == NetworkInfo.State.CONNECTED || wifi== NetworkInfo.State.CONNECTED) 
            return true; 
        else 
            return false; 
        }

アクティビティまたはサービスでは、これを行うことができます:

if(CheckInternet)
{
     // do your work
}
else
{
      Thread t = new Thread() {
                            @Override
                            public void run() {
                                try 
                                {
                                    //check if connected!
                                    while (!checkInternet()) 
                                    {
                                        //Wait to connect
                                        Thread.sleep(5000);                     
                                    }
                                    //do Your Work here
                                   } catch (Exception e) {
                                }
                            }
                        };
                        t.start();
}

また、次のアクセス許可を AndroidManifest.xml ファイルに追加します

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
于 2013-08-14T12:10:38.890 に答える