阅读更多
package com.comit.broadcast;import com.comit.util.AppHpler;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.util.Log;/** * @ClassName: NetworkConnectReceiver * @Description:网络监控广播 * @author: RockeyCai * @date: 2015-11-05 上午14:14:59 *  */public class NetworkConnectReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {AppHpler.setAutoUploadImgTask(context, true);//不自动上传if (!AppHpler.getAutoUploadImgTask(context)) {    return;}// 这个监听网络连接的设置,包括wifi和移动数据 的打开和关闭if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();    if (activeNetInfo != null) {// 判断是wifi连接if (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {    Log.d(NetworkConnectReceiver.class.getCanonicalName(), "wifi连接:" + activeNetInfo.getState());    // 连接成功    if (NetworkInfo.State.CONNECTED == activeNetInfo.getState()) {//启动开始上传图片服务uploadService(context, intent, UploadService.UPLOAD_SERVICE_DO_UPLOAD_TRUE);return;    } //    else if (activeNetInfo.getType() == 1) {//if (NetworkInfo.State.DISCONNECTING == activeNetInfo.getState()) {//    // 未连接成功//    uploadService(context, intent, true);//}//    }}     }    // 网络无效  停止上传服务    uploadService(context, intent, UploadService.UPLOAD_SERVICE_DO_UPLOAD_FALSE);}    }    /**     * 上传服务     * @param mContext     * @param intent     * @param isUpload     */    private void uploadService(Context mContext, Intent intent, boolean doUpload) {Intent serviceIntent = new Intent(mContext, UploadService.class);serviceIntent.putExtra(UploadService.UPLOAD_SERVICE_DO_UPLOAD_FLAG, doUpload); mContext.startService(serviceIntent);    }    /**     * 是否为wifi连接     *      * @param mContext     * @return     */    public static boolean isWifi(Context mContext) {ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {    return true;}return false;    }}

 

                                                                                                                                                                

 

更多相关文章

  1. 【有图】android通过jdbc连接mysql(附文件)
  2. Android(安卓)studio 多线程网络文件下载
  3. android判断网络是否能用
  4. android WIFI的一些属性
  5. android 获取网络上服务器图片
  6. Android(安卓)监控网络状态
  7. android练习一之获取网页源码
  8. android Dialog中SeekBar的使用方法
  9. 【Android】定义HttpPost连接超时

随机推荐

  1. 2011.09.07(2)——— android 跨进程通信之
  2. 未命名 3
  3. Android(安卓)SSL BKS证书生成
  4. 按钮的多样话 selector的使用
  5. Compile C/C++ In Eclipse for Android
  6. 在Android(安卓)Studio上进行OpenCV3.1开
  7. 2011.09.07(4)——— android 跨进程通信之
  8. Android(安卓)Xml解析
  9. Ubuntu10.10下真机调试
  10. android 类库介绍