说明:1.android7.1系统不支持静态广播监听网络变化
2.service和broadcast简单应用
逻辑:开机运行CheckNetService服务,启动BroadcastReceiver广播接收器,NetWorkStateReceiver接收器判断接收到的网络变化并处理
声明
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.networkinfo">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--网络变化的广播-->
<receiver android:name=".NetWorkStateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<!--开机自启动的receiver-->
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name=".CheckNetService" >
</service>
</application>
</manifest>
广播
package com.example.networkinfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
//开机启动服务广播接收器
public class BootBroadcastReceiver extends BroadcastReceiver {
private static String TAG="wp BootBroadcastReceiver";
private static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED"; //开机启动
@Override
public void onReceive(Context context, Intent intent) {
// 开机启动服务监测网络情况
Log.i(TAG, "系统已经启动 ,接收到广播:"+intent.getAction());
String action = intent.getAction();
if (ACTION_BOOT.equals(action)) {
Log.i(TAG,"开启BootBroadcastReceiver");
Intent intentt = new Intent(context, CheckNetService.class);//启动服务器
context.startService(intentt);
}
}
}
package com.example.networkinfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
//网络状态的广播接收器
public class NetWorkStateReceiver extends BroadcastReceiver {
private static String NET_CHANGE="android.net.conn.CONNECTIVITY_CHANGE";
private static String TAG="wp---->";
@Override
public void onReceive(Context context, Intent intent) {
//收到网络变化情况发一条信息
String action=intent.getAction();
if (action.equals(NET_CHANGE)){
// Log.i(TAG,"the net is change...");
// 分3种状态:1无网络,2wifi网络,3.sim卡网络
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo==null){
Log.i(TAG,"无网络");
}
else{
int type2 = networkInfo.getType();
// Log.i(TAG,"已联网,网络类型:"+type2);
switch (type2){
case 1:Log.i(TAG,"已联网,网络类型wifi");break; //wifi网络
case 0:Log.i(TAG,"已联网,网络类型流量");break; //sim卡网络
default:break;
}
}
}
}
}
服务
package com.example.networkinfo;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
public class CheckNetService extends Service {
NetWorkStateReceiver netBroadcastReceiver;
IntentFilter filter;
private static String TAG="wp CheckNetService";
Thread mThread=null;
private boolean mThreadFlag=true;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// 动态注册广播
filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
netBroadcastReceiver=new NetWorkStateReceiver();
//注册广播接收
registerReceiver(netBroadcastReceiver, filter);
// mThread=new Thread(){
// @Override
// public void run() {
// super.run();
// while (mThreadFlag){
// if(true){
网络连通
// }
// else{
网络中断
// }
// try {
// Thread.sleep(10000); //10s后执行
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
// };
}
@Override
public void onDestroy() {
mThreadFlag=false;
if (mThread!=null){
mThread.interrupt();
}
// 注销广播
unregisterReceiver(netBroadcastReceiver);
super.onDestroy();
}
}
主程序
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//开机运行网络监测服务
Intent intentt = new Intent(this, CheckNetService.class);//启动服务器
this.startService(intentt);
}