Android实时监听网络状态(1)
其实手机在网络方面的的监听也比较重要,有时候我们必须实时监控这个程序的实时网络状态,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;
}
}
#3楼 2015-11-25 14:45 Rising_Sun
文章中应该告诉大家,在AndroidManifest.xml 文件中注册广播
<!-- 注册广播 -->
<receiver android:name=".NetBroadcastReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
###################################
Android实时监听网络状态(2)
在开发android应用时,涉及到要进行网络访问,时常需要进行网络状态的检查,以提供给用户必要的提醒。一般可以通过ConnectivityManager来完成该工作。
ConnectivityManager有四个主要任务:
1、监听手机网络状态(包括GPRS,WIFI, UMTS等)
2、手机状态发生改变时,发送广播
3、当一个网络连接失败时进行故障切换
4、为应用程序提供可以获取可用网络的高精度和粗糙的状态
当我们要在程序中监听网络状态时,只要一下几个步骤即可:
1、定义一个Receiver重载其中的onReceive函数,在其中完成所需要的功能,如根据WIFI和GPRS是否断开来改变空间的外观或者提示是否能访问网络
package com.bsharksdk.receiver; import com.bsharksdk.main.BSharkApi;
import com.bsharksdk.ui.BSToast; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo; /**
* @author Javen
*
*/
public class ConnectionChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobNetInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiNetInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
BSToast.showLong(context, "网络不可以用");
//改变背景或者 处理网络的全局变量
}else {
//改变背景或者 处理网络的全局变量
}
}
}
2、在适当的地方注册Receiver,可以在程序中注册,在onCreate中调用如下函数即可:
private void registerReceiver(){
IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
myReceiver=new ConnectionChangeReceiver();
this.registerReceiver(myReceiver, filter);
}
3、在适当时取消注册Receiver,可以在程序中取消,在onDestroye中调用如下函数即可:
private void unregisterReceiver(){
this.unregisterReceiver(myReceiver);
}
记得在 Activity中 设置变量 private ConnectionChangeReceiver myReceiver;
记得添加访问网络需要的权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />