Android 监听wifi广播的两种方式

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 -->
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )