1package com.comit.broadcast; 2 3import com.comit.util.AppHpler; 4 5import android.content.BroadcastReceiver; 6import android.content.Context; 7import android.content.Intent; 8import android.net.ConnectivityManager; 9import android.net.NetworkInfo; 10import android.util.Log; 11 12/** 13 * @ClassName: NetworkConnectReceiver 14 * @Description:网络监控广播 15 * @author: RockeyCai 16 * @date: 2015-11-05 上午14:14:59 17 * 18 */ 19public class NetworkConnectReceiver extends BroadcastReceiver { 20 21 @Override 22 public void onReceive(Context context, Intent intent) { 23 24 AppHpler.setAutoUploadImgTask(context, true); 25 26 //不自动上传 27 if (!AppHpler.getAutoUploadImgTask(context)) { 28 return; 29 } 30 31 // 这个监听网络连接的设置,包括wifi和移动数据 的打开和关闭 32 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { 33 34 ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 35 NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 36 37 if (activeNetInfo != null) { 38 // 判断是wifi连接 39 if (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { 40 Log.d(NetworkConnectReceiver.class.getCanonicalName(), "wifi连接:" + activeNetInfo.getState()); 41 // 连接成功 42 if (NetworkInfo.State.CONNECTED == activeNetInfo.getState()) { 43 //启动开始上传图片服务 44 uploadService(context, intent, UploadService.UPLOAD_SERVICE_DO_UPLOAD_TRUE); 45 return; 46 } 47// else if (activeNetInfo.getType() == 1) { 48// if (NetworkInfo.State.DISCONNECTING == activeNetInfo.getState()) { 49// // 未连接成功 50// uploadService(context, intent, true); 51// } 52// } 53 } 54 } 55 // 网络无效 停止上传服务 56 uploadService(context, intent, UploadService.UPLOAD_SERVICE_DO_UPLOAD_FALSE); 57 } 58 } 59 60 /** 61 * 上传服务 62 * @param mContext 63 * @param intent 64 * @param isUpload 65 */ 66 private void uploadService(Context mContext, Intent intent, boolean doUpload) { 67 68 Intent serviceIntent = new Intent(mContext, UploadService.class); 69 serviceIntent.putExtra(UploadService.UPLOAD_SERVICE_DO_UPLOAD_FLAG, doUpload); 70 mContext.startService(serviceIntent); 71 } 72 73 /** 74 * 是否为wifi连接 75 * 76 * @param mContext 77 * @return 78 */ 79 public static boolean isWifi(Context mContext) { 80 ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 81 NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 82 if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { 83 return true; 84 } 85 return false; 86 } 87} 88 89<!-- 网络广播监听 start --> 90 <receiver android:name="com.comit.broadcast.NetworkConnectReceiver" > 91 <intent-filter> 92 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 93 <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 94 <action android:name="android.net.wifi.STATE_CHANGE" /> 95 </intent-filter> 96 </receiver> 97 98 <service 99 android:name="com.comit.broadcast.UploadService" 100 android:enabled="true" > 101 <intent-filter> 102 <action android:name="com.comit.broadcast.UploadService" /> 103 </intent-filter> 104 </service> 105 <!-- 网络广播监听 end -->
Android 监听wifi广播的两种方式
Stella981
2021-10-11
1181 0 0
点赞
收藏
评论区
加载中...