您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)仿QQ微信實(shí)時監(jiān)測網(wǎng)絡(luò)狀態(tài),小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
思路
判斷網(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>
關(guān)于“Android如何實(shí)現(xiàn)仿QQ微信實(shí)時監(jiān)測網(wǎng)絡(luò)狀態(tài)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。