检测网络是否可用的工具类CheckWebUtil.java
[注意]需要下面的权限申明:
<uses-permission android:name="android.permission.INTERNET"/>
<!-- 查看Wi-Fi 状态 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!-- 查看网络状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
package com.***.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class CheckWebUtil
{
// 检测网络是否可用,Mobile网络 + WiFi网络
public static boolean isNetworkConnected(Context context)
{
if (context !=null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] mNetworkInfo =mConnectivityManager.getAllNetworkInfo();
if (mNetworkInfo !=null)
{
for (int i =0;i <mNetworkInfo.length;i++)
{
if (mNetworkInfo[i].getState() ==NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
// 检测WiFi是否可用
public static Boolean isWifiConnected(Context context)
{
if (context !=null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo !=null)
{
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
}
// 检测Mobil网络是否可用,产生流量费用
public boolean isMobileConnected(Context context)
{
if (context !=null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo =mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo !=null)
{
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}
// 检测网络连接的类型
/**
* @return ConnectivityManager.TYPE_MOBIL, ConnectivityManager.TYPE_WIFI等等
*/
public static int getConnectedType(Context context)
{
if (context !=null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo =mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo !=null &&mNetworkInfo.isAvailable())
{
return mNetworkInfo.getType();
}
}
return -1;
}
}
px和dip数据单位的转换DensityUtils.java
[说明]Android开发过程中为了适用各种不同分辨率的设备,官方建议我们在xml中书写布局的时候采用dip/dp来代替px单位,但是要是我们想在java文件中动态的设置控件的大小,java文件中的数字为px单位,此时就需要先使用DensityUtils工具类中的px2dip函数和dip2px函数,对px单位和dip/dp单位的数据进行转化了。
package com.***.utils;
import android.content.Context;
publicclassDensityUtils
{
public static int px2dip(Context context,float pxValue)
{
float m = context.getResources().getDisplayMetrics().density;
return (int) (pxValue /m +0.5f);
}
public static int dip2px(Context context,float dipValue)
{
float scale =context.getResources().getDisplayMetrics().density;
return (int) (dipValue *scale +0.5f);
}
}
Toast显示的工具类ShowHintInfoUtils.java
[说明]Android开发的过程中,我们经常会使用Toast来显示提示信息,下面的自定义的工具类,可以方便在需要显示Toast的时候调用
package com.***.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class ShowHintInfoUtils
{
/**
* @param infoString
* 提示信息内容
* @param picID
* 提示信息的图标
* @param context
*/
public static void showToastInfo(String infoString,int picID,Context context)
{
Toasttoast;
if (picID == -1)
{
toast =Toast.makeText(context,infoString,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}else
{
try
{
toast =Toast.makeText(context,infoString,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
LinearLayouttoastView = (LinearLayout)toast.getView();
ImageViewtoastImageView =new ImageView(context);
toastImageView.setImageResource(picID);
toastView.addView(toastImageView,0);
toast.show();
}catch (Exceptione)
{
toast =Toast.makeText(context,infoString,Toast.LENGTH_SHORT);
toast.show();
}
}
}
// 只显示文字提示
public static void showTextToastInfo(String infoString,Context context)
{
try
{
Toast toast;
toast =Toast.makeText(context,infoString,1);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}catch (Exceptione)
{
}
}
/**
*@see 显示网络连接失败的信息
*@param context
*/
public static void showNetworkUnConnect(Context context)
{
try
{
Toasttoast;
toast =Toast.makeText(context,"网络连接错误\n请检查网络链接!",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}catch (Exceptione)
{
}
}
}