android工具類(2)NetWorkHelper 網路工具類,androidjson工具類

來源:互聯網
上載者:User

android工具類(2)NetWorkHelper 網路工具類,androidjson工具類



import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.net.Uri;import android.net.NetworkInfo.State;import android.telephony.TelephonyManager;import android.util.Log;public class NetWorkHelper {    public static final int NETWORN_NONE = 0;    public static final int NETWORN_WIFI = 1;    public static final int NETWORN_MOBILE = 2;private static String LOG_TAG = "NetWorkHelper";public static Uri uri = Uri.parse("content://telephony/carriers");/** * 判斷是否有網路 */public static boolean isNetworkAvailable(Context context) {ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity == null) {Log.w(LOG_TAG, "couldn't get connectivity manager");} else {NetworkInfo[] info = connectivity.getAllNetworkInfo();if (info != null) {for (int i = 0; i < info.length; i++) {if (info[i].isAvailable()) {Log.d(LOG_TAG, "network is available");return true;}}}}Log.d(LOG_TAG, "network is not available");return false;}/** * 檢查網路狀態 * @param context * @return */public static boolean checkNetState(Context context){    boolean netstate = false;ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);if(connectivity != null){NetworkInfo[] info = connectivity.getAllNetworkInfo();if (info != null) {for (int i = 0; i < info.length; i++){if (info[i].getState() == NetworkInfo.State.CONNECTED) {netstate = true;break;}}}}return netstate;    }/** * 判斷網路是否為漫遊 */public static boolean isNetworkRoaming(Context context) {ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity == null) {Log.w(LOG_TAG, "couldn't get connectivity manager");} else {NetworkInfo info = connectivity.getActiveNetworkInfo();if (info != null&& info.getType() == ConnectivityManager.TYPE_MOBILE) {TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);if (tm != null && tm.isNetworkRoaming()) {Log.d(LOG_TAG, "network is roaming");return true;} else {Log.d(LOG_TAG, "network is not roaming");}} else {Log.d(LOG_TAG, "not using mobile network");}}return false;}/** * 判斷MOBILE網路是否可用 *  * @param context * @return * @throws Exception */public static boolean isMobileDataEnable(Context context) {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);boolean isMobileDataEnable = false;isMobileDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();return isMobileDataEnable;}/** * 判斷wifi 是否可用 * @param context * @return * @throws Exception */public static boolean isWifiDataEnable(Context context) {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);boolean isWifiDataEnable = false;isWifiDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();return isWifiDataEnable;}    public static int getNetworkState(Context context){        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);        //Wifi        State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();        if(state == State.CONNECTED||state == State.CONNECTING){            return NETWORN_WIFI;        }        //3G        state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();        if(state == State.CONNECTED||state == State.CONNECTING){            return NETWORN_MOBILE;        }        return NETWORN_NONE;    }}



問android中在工具類裡怎擷取Activity的Context? 因為我要在工具類裡彈出Dialog,但是getApplication

在工具類的建構函式中添加一個Context的參數,並設定一個全域變數Context,這樣在某個Activity類中執行個體化這個工具類的時候就可以將Context對象傳過去了,希望對你有協助!
 
android開發,怎用在其他工程中使用自訂的工具類的問題

在工程2中把工程1添加為依賴工程就可以了!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.