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

Android 网络状态实时监听代码实例(一),第1张

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

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

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

<uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE"/><uses-permission androID:name="androID.permission.INTERNET" />

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

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;}@OverrIDepublic voID onCreate() {super.onCreate();mApplication = this;initData();}public voID initData() {mNetWorkState = NetUtil.getNetworkState(this);}}

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

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";@OverrIDepublic 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接口

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{@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);NetbroadcastReceiver.mListeners.add(this);}@OverrIDepublic 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;}@OverrIDepublic voID onNetChange() {// Todo auto-generated method stubif (NetUtil.getNetworkState(this) == NetUtil.NETWORN_NONE) {T.showLong(this,R.string.net_err);}else {T.showLong(this,"网络可以使用");}}}

5、判断网络的工具类

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);// WifiState state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();if (state == State.CONNECTED || state == State.CONNECTING) {return NETWORN_WIFI;}// 3Gstate = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();if (state == State.CONNECTED || state == State.CONNECTING) {return NETWORN_MOBILE;}return NETWORN_NONE;}}

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

总结

以上是内存溢出为你收集整理的Android 网络状态实时监听代码实例(一)全部内容,希望文章能够帮你解决Android 网络状态实时监听代码实例(一)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149630.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存