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