日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android 判断网络是否可用

發(fā)布時(shí)間:2024/4/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 判断网络是否可用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

今天也是沒(méi)有什么好寫(xiě)的,但是自己的強(qiáng)迫癥似乎又犯了,覺(jué)得每天寫(xiě)博客的習(xí)慣不能改,所以在網(wǎng)上搜索了資料,寫(xiě)了這些內(nèi)容。


一。檢查網(wǎng)絡(luò)是否可用,并且彈出提示框,用戶可以去設(shè)置網(wǎng)絡(luò)。

package com.example.network;import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo;public class NetworkUtil {/*** 檢查網(wǎng)絡(luò)是否可用* * @param context* @return*/public static boolean isNetworkAvailable(Context context) {ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);if (manager == null) {return false;}NetworkInfo networkinfo = manager.getActiveNetworkInfo();if (networkinfo == null || !networkinfo.isAvailable()) {return false;}return true;}}


這個(gè)主要通過(guò)手機(jī)的ConnectivityManager來(lái)獲取當(dāng)前的網(wǎng)絡(luò)信息,然后判斷是否有網(wǎng)絡(luò)連接。

當(dāng)網(wǎng)絡(luò)不可用的時(shí)候,我們應(yīng)該提供一個(gè) 界面讓用戶區(qū)設(shè)置網(wǎng)絡(luò)連接。Android 2.3以上跟Android 2.3以前的設(shè)置方式是不一樣的,我們需要判斷一下。


package com.example.network;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

?@Override
?protected void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.activity_main);
?}

?@Override
?protected void onStart() {

??Log.i("MainActivity", "onStart");

??if (!NetworkUtil.isNetworkAvailable(this)) {
???showSetNetworkUI(this);
??} else {
???Toast.makeText(this, "網(wǎng)絡(luò)可用...", 0).show();
??}

??super.onStart();
?}

?@Override
?protected void onResume() {

??Log.i("MainActivity", "onStart");

??super.onResume();
?}

?/*
? * 打開(kāi)設(shè)置網(wǎng)絡(luò)界面
? */
?public void showSetNetworkUI(final Context context) {
??// 提示對(duì)話框
??AlertDialog.Builder builder = new Builder(context);
??builder.setTitle("網(wǎng)絡(luò)設(shè)置提示")
????.setMessage("網(wǎng)絡(luò)連接不可用,是否進(jìn)行設(shè)置?")
????.setPositiveButton("設(shè)置", new DialogInterface.OnClickListener() {

?????@Override
?????public void onClick(DialogInterface dialog, int which) {
??????// TODO Auto-generated method stub
??????Intent intent = null;
??????// 判斷手機(jī)系統(tǒng)的版本 即API大于10 就是3.0或以上版本
??????if (android.os.Build.VERSION.SDK_INT > 10) {
???????intent = new Intent(
?????????android.provider.Settings.ACTION_WIFI_SETTINGS);
??????} else {
???????intent = new Intent();
???????ComponentName component = new ComponentName(
?????????"com.android.settings",
?????????"com.android.settings.WirelessSettings");
???????intent.setComponent(component);
???????intent.setAction("android.intent.action.VIEW");
??????}
??????context.startActivity(intent);
?????}
????})
????.setNegativeButton("取消", new DialogInterface.OnClickListener() {

?????@Override
?????public void onClick(DialogInterface dialog, int which) {

??????dialog.dismiss();
?????}
????}).show();
?}

?@Override
?public boolean onCreateOptionsMenu(Menu menu) {
??// Inflate the menu; this adds items to the action bar if it is present.
??getMenuInflater().inflate(R.menu.main, menu);
??return true;
?}

}

這里打當(dāng)沒(méi)有網(wǎng)絡(luò)時(shí),彈出設(shè)置網(wǎng)絡(luò)對(duì)話框,用戶點(diǎn)擊確定時(shí),可以直接跳到設(shè)置網(wǎng)絡(luò)界面。


后面我還找了判斷網(wǎng)絡(luò)類型的代碼,也很簡(jiǎn)單。


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 { ??? /** ??? ?* 判斷是否有網(wǎng)絡(luò)連接 ??? ?* @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網(wǎng)絡(luò)是否可用 ??? ?* @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網(wǎng)絡(luò)是否可用 ??? ?* @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; ??? } ??? /** ??? ?* 獲取當(dāng)前網(wǎng)絡(luò)連接的類型信息 ??? ?* @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; ??? } ??? /** ??? ?* 獲取當(dāng)前的網(wǎng)絡(luò)狀態(tài) :沒(méi)有網(wǎng)絡(luò)0:WIFI網(wǎng)絡(luò)1:3G網(wǎng)絡(luò)2:2G網(wǎng)絡(luò)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; ??? } }

還有一個(gè)判斷網(wǎng)絡(luò)狀態(tài)改變的代碼,就是一個(gè)廣播接收器。

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; ????} ??} }

需要在manifest里注冊(cè)廣播。

<receiver ???android:name=".broadcast.NetWorkStatusReceiver" ???android:enabled="true" ???android:exported="true"> ???<intent-filter> ?????<action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> ???</intent-filter> ?</receiver>


這些是我在網(wǎng)上搜索資料整理得來(lái)的,大家可以自己在網(wǎng)上搜素資料,其實(shí)很簡(jiǎn)單,網(wǎng)上資料也很多。


android 判斷網(wǎng)絡(luò)是否可用就講完了。


就這么簡(jiǎn)單。




總結(jié)

以上是生活随笔為你收集整理的android 判断网络是否可用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。