Android仿QQ微信實(shí)時監(jiān)測網(wǎng)絡(luò)狀態(tài)
先簡單說一下思路:網(wǎng)絡(luò)變化時系統(tǒng)會發(fā)出廣播。所以我們監(jiān)聽這個廣播,利用接口回調(diào)通知activity做相應(yīng)的操作就好了。
思路
- 判斷網(wǎng)絡(luò)狀態(tài)(寫個工具類NetUtil)
- 寫個類繼承BroadcastReceiver(不要忘記在清單文件中注冊)
- 需要在清單文件中添加權(quán)限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission>
- 回調(diào)接口(NetEvevt)
- BaseActivity實(shí)現(xiàn)這個接口
直接上代碼
NetUtil
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; /** * Author: lenovo * Date: 2018/2/26 * email: 1271396448@qq.com */ public class NetUtil { /** * 沒有連接網(wǎng)絡(luò) */ public static final int NETWORK_NONE = -1; /** * 移動網(wǎng)絡(luò) */ public static final int NETWORK_MOBILE = 0; /** * 無線網(wǎng)絡(luò) */ public static final int NETWORK_WIFI = 1; public static int getNetWorkState(Context context) { // 得到連接管理器對象 ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager .getActiveNetworkInfo(); if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) { return NETWORK_WIFI; } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) { return NETWORK_MOBILE; } } else { return NETWORK_NONE; } return NETWORK_NONE; } }
NetBroadReceiver
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; /** * Author: lenovo * Date: 2018/2/26 * email: 1271396448@qq.com */ public class NetBroadcastReceiver extends BroadcastReceiver { public NetEvevt evevt = BaseActivity.evevt; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub // 如果相等的話就說明網(wǎng)絡(luò)狀態(tài)發(fā)生了變化 if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { int netWorkState = NetUtil.getNetWorkState(context); // 接口回調(diào)傳過去狀態(tài)的類型 evevt.onNetChange(netWorkState); } } // 自定義接口 public interface NetEvevt { public void onNetChange(int netMobile); } }
BaseActivity
import android.os.Bundle; import android.support.v4.app.FragmentActivity; /** * Author: lenovo * Date: 2018/2/26 * email: 1271396448@qq.com */ abstract public class BaseActivity extends FragmentActivity implements NetBroadcastReceiver.NetEvevt { public static NetBroadcastReceiver.NetEvevt evevt; /** * 網(wǎng)絡(luò)類型 */ private int netMobile; @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub super.onCreate(arg0); evevt = this; inspectNet(); } /** * 初始化時判斷有沒有網(wǎng)絡(luò) */ public boolean inspectNet() { this.netMobile = NetUtil.getNetWorkState(BaseActivity.this); return isNetConnect(); // if (netMobile == 1) { // System.out.println("inspectNet:連接wifi"); // } else if (netMobile == 0) { // System.out.println("inspectNet:連接移動數(shù)據(jù)"); // } else if (netMobile == -1) { // System.out.println("inspectNet:當(dāng)前沒有網(wǎng)絡(luò)"); // // } } /** * 網(wǎng)絡(luò)變化之后的類型 */ @Override public void onNetChange(int netMobile) { // TODO Auto-generated method stub this.netMobile = netMobile; isNetConnect(); } /** * 判斷有無網(wǎng)絡(luò) 。 * * @return true 有網(wǎng), false 沒有網(wǎng)絡(luò). */ public boolean isNetConnect() { if (netMobile == 1) { return true; } else if (netMobile == 0) { return true; } else if (netMobile == -1) { return false; } return false; } }
MainActivity
import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends BaseActivity { private TextView textView,txtView; ListView listView; //聲明一個ListView對象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView= (TextView) findViewById(R.id.textView); txtView=(TextView)this.findViewById(R.id.txtView); //啟動時判斷網(wǎng)絡(luò)狀態(tài) boolean netConnect = this.isNetConnect(); if (netConnect){ textView.setVisibility(View.GONE); txtView.setText("網(wǎng)絡(luò)連接正常"); }else { textView.setVisibility(View.VISIBLE); txtView.setText("網(wǎng)絡(luò)連接異常"); } } @Override public void onNetChange(int netMobile) { super.onNetChange(netMobile); //網(wǎng)絡(luò)狀態(tài)變化時的操作 if (netMobile==NetUtil.NETWORK_NONE){ textView.setVisibility(View.VISIBLE); txtView.setText("網(wǎng)絡(luò)連接異常"); }else { textView.setVisibility(View.GONE); txtView.setText("網(wǎng)絡(luò)連接正常"); } } }
記得在清單文件中注冊
<receiver android:name="cn.broadcastreceiver.NetBroadcastReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android BroadcastReceiver實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)實(shí)時監(jiān)聽
- Android檢查網(wǎng)絡(luò)狀態(tài)工具類詳解
- android 監(jiān)聽網(wǎng)絡(luò)狀態(tài)的變化及實(shí)戰(zhàn)的示例代碼
- Android判斷網(wǎng)絡(luò)狀態(tài)的代碼
- Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路
- Android 監(jiān)聽網(wǎng)絡(luò)狀態(tài)方法詳解
- Android 判斷網(wǎng)絡(luò)狀態(tài)實(shí)例詳解
- Android 廣播監(jiān)聽網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼
- Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時出現(xiàn)空指針(NullPointerException)問題的解決方法
- Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽
相關(guān)文章
Android開發(fā)之完成登陸界面的數(shù)據(jù)保存回顯操作實(shí)例
這篇文章主要介紹了Android開發(fā)之完成登陸界面的數(shù)據(jù)保存回顯操作實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式較為詳細(xì)的分析了Android針對登錄數(shù)據(jù)的保存及回顯操作技巧,需要的朋友可以參考下2015-12-12Android音視頻之視頻采集(系統(tǒng)API預(yù)覽)
這篇文章主要為大家詳細(xì)介紹了Android音視頻之視頻采集,系統(tǒng)API預(yù)覽,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12使用android-apktool來逆向(反編譯)APK包方法介紹
這篇文章主要介紹了使用android-apktool來逆向(反編譯)APK包方法介紹,本文講解了版本問題、使用apktool、反編譯decode、rebuild重打包等內(nèi)容,需要的朋友可以參考下2015-04-04使用Flutter實(shí)現(xiàn)一個走馬燈布局的示例代碼
這篇文章主要介紹了使用 Flutter 實(shí)現(xiàn)一個走馬燈布局的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11