检查android中是否有网络连接?

时间:2017-08-26 04:53:09

标签: java android

  public boolean isNetworkConnectionAvailable(){
        ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null && activeNetwork.isConnected();
        if(isConnected) {

                Log.d("Network", "Connected");
                return true;

        }
        else{
            checkNetworkConnection();
            Log.d("Network","Not Connected");
            return false;
        }
    }

它显示无法解析连接管理器和Networkinfo ....请帮助.. 提前致谢

3 个答案:

答案 0 :(得分:2)

使用此方法就像魅力......

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

}

您还需要:

SELECT t1.createdAt, t1.sellPrice, t1.exchangeId AS sellExchange, t2.buyPrice, t2.exchangeId AS buyExchange, t1.sellPrice - t2.buyPrice AS spread, 
       CASE 
         WHEN t1.sellVolume < t2.buyVolume THEN t1.sellVolume
         ELSE t2.buyVolume
       END AS minVolume
FROM 
    (SELECT a.createdAt, a.sellPrice, a.sellVolume, a.exchangeid, a.quoteId
    FROM quotes a
    INNER JOIN (
        SELECT createdAt, max(sellPrice) AS sellPrice
        FROM quotes
        GROUP BY createdAt
    ) b ON a.createdAt = b.createdAt AND a.sellPrice = b.sellPrice) 
    t1 INNER JOIN
    (SELECT a.createdAt, a.buyPrice, a.buyvolume, a.exchangeid, a.quoteId
    FROM quotes a
    INNER JOIN (
        SELECT createdAt, min(buyPrice) AS buyPrice
        FROM quotes
        GROUP BY createdAt
    ) b ON a.createdAt = b.createdAt AND a.buyPrice = b.buyPrice) 
    t2 ON t1.createdAt = t2.createdAt

答案 1 :(得分:1)

试试这个util方法。我创建了一个util类并将此方法添加到它

public class Utils {
         /**
         * ******************************************
         * Method to check whether the Internet is Connected
         * ******************************************
         */
        public static boolean IsNetworkConnected(Context context) {
            if (context != null) {
                ConnectivityManager connMgr = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                return (networkInfo != null && networkInfo.isConnected());
            } else {
                return false;
            }
        }
}

您可以尝试此方法

boolean isConnected = Utils.IsNetworkConnected(mContext);

在尝试之前,将这些权限添加到AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

答案 2 :(得分:1)

在AndroidManifest.xml中授予权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />