編輯:關於Android編程
package cn.hackcoder.beautyreader.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.util.Log; import android.widget.Toast; import cn.hackcoder.beautyreader.activity.base.BaseActivity; import cn.hackcoder.beautyreader.utils.NetWorkUtils; public class NetWorkStatusReceiver extends BroadcastReceiver { public NetWorkStatusReceiver() { } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { Toast.makeText(context, "network changed", Toast.LENGTH_LONG).show(); BaseActivity.isNetWorkConnected = NetWorkUtils.getAPNType(context)>0; } } }
package cn.hackcoder.beautyreader.utils; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.telephony.TelephonyManager; /** * Created by hackcoder on 15-1-25. */ public class NetWorkUtils { /** * 判斷是否有網絡連接 * @param context * @return */ public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } /** * 判斷WIFI網絡是否可用 * @param context * @return */ public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } /** * 判斷MOBILE網絡是否可用 * @param context * @return */ public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } /** * 獲取當前網絡連接的類型信息 * @param context * @return */ public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } /** * 獲取當前的網絡狀態 :沒有網絡0:WIFI網絡1:3G網絡2:2G網絡3 * * @param context * @return */ public static int getAPNType(Context context) { int netType = 0; ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_WIFI) { netType = 1;// wifi } else if (nType == ConnectivityManager.TYPE_MOBILE) { int nSubType = networkInfo.getSubtype(); TelephonyManager mTelephony = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { netType = 2;// 3G } else { netType = 3;// 2G } } return netType; } }
注冊:
<receiver android:name=".broadcast.NetWorkStatusReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
下面給大家展示了AndroidHttpClient結構:public final classAndroidHttpClientextends Objectimplemen
本文以案例形式分析了Android中TelephonyManager類的用法。分享給大家供大家參考。具體如下:目錄結構:main.xml布局文件:<?xml
今天遇到一些關於在子線程中操作Handler的問題,感覺又要研究源代碼了,但是關於Handler的話,我之前研究過,可以參考這篇文章:http://blog.csdn.n
顯示對話框窗口 1、創建Dialog1項目,在activity_main.xml文件中添加一個Button: 2、在MainActivity.jav