zoukankan      html  css  js  c++  java
  • Android判断网络状态

    package com.ch.services;
    
    import com.ch.utils.NetWorkUtils;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    //这是后台 Service
    public class CopyOfOffLineService extends Service{
        
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
    //new一个新类 判断网络 NetWorkUtils netUtils = new NetWorkUtils(getApplicationContext()); int i = netUtils.netWorkType();//获得当前网络状态类型 //1.wifi 2.移动网路 3.没网
    if(i == 1){ //请求数据,并保存 Log.i("TAG", "开始离线下载......"); }else if(i == 2){ Log.i("TAG", "提示是移动数据,是否继续下载......"); }else{ Log.i("TAG", "没网,不需要下载......"); } return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
    package com.ch.utils;
    
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    
    public class NetWorkUtils {
        Context mcontext;
        public NetWorkUtils(Context mcontext){
            this.mcontext = mcontext;
        }
        
        public int netWorkType(){
    //获取管理 ConnectivityManager mConnectivity
    = (ConnectivityManager)mcontext.getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mTelephony = (TelephonyManager)mcontext.getSystemService(Context.TELEPHONY_SERVICE); //检查网络连接 NetworkInfo info = mConnectivity.getActiveNetworkInfo(); if (info == null || !mConnectivity.getBackgroundDataSetting()) { Log.i("TAG","网络未连接....."); return 3; } int netType = info.getType(); int netSubtype = info.getSubtype(); Log.i("TAG",netType+",,"+netSubtype+",,"+mTelephony.isNetworkRoaming()); if (netType == ConnectivityManager.TYPE_WIFI) { //WIFI Log.i("TAG","wifi连接....."); return 1; } else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { //MOBILE Log.i("TAG","移动数据连接....."); return 2; } return 3; } }
  • 相关阅读:
    FineAdmin.Mvc 使用ok-admin+ASP.NET MVC搭建的通用权限后台管理系统
    ASP.NET MVC Liu_Cabbage 个人博客
    ASP.NET MVC 通用角色权限管理系统
    js/jquery 判断节点是否存在
    jquery myscroll文字上下无缝滚动插件 简单使用
    使用DATEADD() DATEDIFF() 函数获取时间查询条件
    input 设置 display:none后 jquery无法给input赋值
    ASP.NET MVC QQ互联接入
    修改HTTPS加密协议TLS1.0为TLS1.2
    IIS设置网站为HTTPS并且将HTTP重定向到HTTPS
  • 原文地址:https://www.cnblogs.com/1426837364qqcom/p/5311891.html
Copyright © 2011-2022 走看看