欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-23 21:57:34
其实手机在网络方面的的监听也比较重要,有时候我们必须实时监控这个程序的实时网络状态,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;
}
@override
public 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";
@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接口

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、判断网络的工具类

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 网络状态实时监听代码实例(一),希望对大家有所帮助!