Android开发中检测网络和获取手机设备和防暴力点击工具类
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;
public class NetUtils {
/**
* 检测网络,检查是否是wifi状态
*
* @param context
* @return true是wifi状态,false为不是WiFi状态
*
*/
public static boolean checkNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
NetworkInfo netWorkInfo = info[i];
if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
} else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return false;
}
}
}
}
}
return false;
}
/**
* 检查网络是否连接
*
* @param mContext
* @return netStatus true已连接 false未连接
*/
public static boolean networkStatusOK(Context mContext) {
boolean netStatus = false;
try {
ConnectivityManager connectManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectManager
.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
if (activeNetworkInfo.isAvailable()
&& activeNetworkInfo.isConnected()) {
netStatus = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return netStatus;
}
/**
* 检查设备是否存在SDCard的工具方法
*/
public static boolean hasSdcard() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
// 有存储的SDCard
return true;
} else {
return false;
}
}
// 判断网络连接状态
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
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;
}
// 判断移动网络
public static 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;
}
// 获取连接类型
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;
}
/**
* 设置网络
*
* @param paramContext
*/
public static void startToSettings(Context paramContext) {
if (paramContext == null)
return;
try {
if (Build.VERSION.SDK_INT > 10) {
paramContext.startActivity(new Intent(
"android.settings.SETTINGS"));
return;
}
} catch (Exception localException) {
localException.printStackTrace();
return;
}
paramContext.startActivity(new Intent(
"android.settings.WIRELESS_SETTINGS"));
}
/**
* 获取清单文件渠道号
*
* @param context
* @param key
* @return
*/
public static String getMetaData(Context context, String key) {
try {
ApplicationInfo ai = context.getPackageManager()
.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
Object value = ai.metaData.get(key);
if (value != null) {
return value.toString();
}
} catch (Exception e) {
}
return null;
}
private static long lastClickTime;
/**
* 防暴力点击 true则return false则走方法
*
* @author cheny
*
*/
public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < 800) {
return true;
}
lastClickTime = time;
return false;
}
/**
* 获取设备mac地址
*
* @param context
* @return
*/
public static String getLocalMacAddressFromWifiInfo(Context context) {
String macSerial = "";
String str = "";
try {
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 赋予默认值
ex.printStackTrace();
}
return macSerial;
}
/**
* 获取设备imsi
*
* @param context
* @return
*/
public static String getImsi(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String _imsi = tm.getSubscriberId();
if (_imsi != null && !_imsi.equals("")) {
return _imsi;
}
return "";
}
/**
* 获取设备imei
*
* @param context
* @return
*/
public static String getImei(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String _imei = tm.getDeviceId();
if (_imei != null && !_imei.equals("")) {
return _imei;
}
return "";
}
/**
* 获取设备品牌
*
* @return
*/
public static String getPhoneName() {
String name = Build.BRAND;
return name;
}
/**
* 获取设备型号
*
* @return
*/
public static String getPhoneModel() {
String model = Build.MODEL;
return model;
}
/**
* 获取app版本号
*
* @param context
* @return
*/
public static String getversionCode(Context context) {
PackageManager pm = context.getPackageManager();
try {
String versionName = pm.getPackageInfo(context.getPackageName(), 0).versionName;
return versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 判断当前设备是否是模拟器。如果返回TRUE,则当前是模拟器,不是返回FALSE
*
* @param context
* @return
*/
public static boolean isEmulator(Context context) {
try {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
if (imei != null && imei.equals("000000000000000")) {
return true;
}
return (Build.MODEL.equals("sdk"))
|| (Build.MODEL.equals("google_sdk"));
} catch (Exception ioe) {
}
return false;
}
/**
* 获得版本号信息
*
* @param context
* @return
*/
public static String getVersionName(Context context) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
return info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 版本的代码
*
* @param context
* @return
*/
public static int getVersionCode(Context context) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
return info.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return -1;
}
}