服务器之家

服务器之家 > 正文

Android 网络状态实时监听代码实例(一)

时间:2021-06-22 14:12     来源/作者:智慧云端日记

其实手机在网络方面的的监听也比较重要,有时候我们必须实时监控这个程序的实时网络状态,android在网络断开与连接的时候都会发出广播,我们通过接收系统的广播就可以实现网络的监听。

1、添加访问网络和获取网络状态的权限

?
1
2
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />

2、新建一个Application类并继承android.app.Application 申明变量保存网络状态

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.javen.application;
import com.javen.netbroadcast.NetUtil;
public class Application extends android.app.Application {
private static Application mApplication;
public static int mNetWorkState;
public static synchronized Application getInstance() {
return mApplication;
}
@Override
public void onCreate() {
super.onCreate();
mApplication = this;
initData();
}
public void initData() {
mNetWorkState = NetUtil.getNetworkState(this);
}
}

3、新建NetBroadcastReceiver类继承BroadcastReceiver 并实现onReceive方法 在onReceive方法通知接口完成加载

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.javen.receiver;
import java.util.ArrayList;
import com.javen.application.Application;
import com.javen.netbroadcast.NetUtil;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NetBroadcastReceiver extends BroadcastReceiver {
public static ArrayList<netEventHandler> mListeners = new ArrayList<netEventHandler>();
private static String NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(NET_CHANGE_ACTION)) {
Application.mNetWorkState = NetUtil.getNetworkState(context);
if (mListeners.size() > 0)// 通知接口完成加载
for (netEventHandler handler : mListeners) {
handler.onNetChange();
}
}
}
public static abstract interface netEventHandler {
public abstract void onNetChange();
}
}

4、在应该的Activity中实现netEventHandler接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.javen.netbroadcast;
import com.javen.receiver.NetBroadcastReceiver;
import com.javen.receiver.NetBroadcastReceiver.netEventHandler;
import com.javen.ui.base.BaseActivity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends BaseActivity implements netEventHandler{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NetBroadcastReceiver.mListeners.add(this);
}
@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;
}
@Override
public void onNetChange() {
// TODO Auto-generated method stub
if (NetUtil.getNetworkState(this) == NetUtil.NETWORN_NONE) {
T.showLong(this, R.string.net_err);
}else {
T.showLong(this, "网络可以使用");
}
}
}

5、判断网络的工具类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.javen.netbroadcast;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
public class NetUtil {
public static final int NETWORN_NONE = 0;
public static final int NETWORN_WIFI = 1;
public static final int NETWORN_MOBILE = 2;
public static int getNetworkState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// Wifi
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (state == State.CONNECTED || state == State.CONNECTING) {
return NETWORN_WIFI;
}
// 3G
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
if (state == State.CONNECTED || state == State.CONNECTING) {
return NETWORN_MOBILE;
}
return NETWORN_NONE;
}
}

以上所述是小编给大家介绍的Android 网络状态实时监听代码实例(一),希望对大家有所帮助!

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部