安卓通過廣播實時監聽當前網路狀況
阿新 • • 發佈:2019-01-31
需求:
當網路斷開或者連線上的時候,需要你的APP作出一定的響應,有時候還需要判斷當前網路的型別,wifi或者移動資料…
分析:
- 網路發生變化系統會發送廣播
- 通過註冊一個廣播接收器來接收廣播
- 判斷當前變化的網路是什麼狀態(wifi,移動資料,無網路)
- 最後一個介面回調當前的狀態·瞬間爆炸完成單殺
話不多說開始擼碼:
第一步:檢查網路狀態的型別
需要設定許可權–允許檢視網路狀態
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetUtil {
/**
* 沒有連線網路
*/
private static final int NETWORK_NONE = -1;
/**
* 行動網路
*/
private static final int NETWORK_MOBILE = 0;
/**
* 無線網路
*/
private 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;
}
}
第二步:建立狀態回撥介面
public interface NetEvent {
void onNetChange(int netMobile);
}
第三步:建立廣播接收器
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
public class NetBroadcastReceiver extends BroadcastReceiver {
private NetEvent netEvent;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
//檢查網路狀態的型別
int netWrokState = NetUtil.getNetWorkState(context);
if (netEvent != null)
// 介面回傳網路狀態的型別
netEvent.onNetChange(netWrokState);
}
}
public void setNetEvent(NetEvent netEvent) {
this.netEvent = netEvent;
}
}
第四步:在Acitivity中動態註冊廣播,這裡不能靜態註冊哦,原因你懂的
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements NetEvent {
/**
* 網路狀態
*/
private int netMobile;
private TextView mtvNet;
/**
* 監控網路的廣播
*/
private NetBroadcastReceiver netBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtvNet = (TextView) findViewById(R.id.tv_net);
}
@Override
public void onStart() {
super.onStart();
//註冊廣播
if (netBroadcastReceiver == null) {
netBroadcastReceiver = new NetBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(netBroadcastReceiver, filter);
/**
* 設定監聽
*/
netBroadcastReceiver.setNetEvent(this);
}
}
@Override
public void onNetChange(int netMobile) {
this.netMobile = netMobile;
isNetConnect();
}
private void isNetConnect() {
mtvNet.append("\n" + new Date());
switch (netMobile) {
case 1://wifi
mtvNet.append("\n當前網路型別:wifi");
break;
case 0://移動資料
mtvNet.append("\n當前網路型別:移動資料");
break;
case -1://沒有網路
mtvNet.append("\n當前無網路");
break;
}
mtvNet.append("\n------------------------------------------------------------------------------------------");
}
@Override
public void onDestroy() {
super.onDestroy();
if (netBroadcastReceiver != null) {
//登出廣播
unregisterReceiver(netBroadcastReceiver);
}
}
}
activity_main.xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_net"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>