判断网络情况:
/**
* 检测网络是否连接,如果连接,判断连接方式
*
* @return
*/
public static int getNetworkState() {
boolean flag = false;
// 得到网络连接信息
ConnectivityManager manager = (ConnectivityManager) MainApplication
.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
// 去进行判断网络是否连接
if (manager.getActiveNetworkInfo() != null) {
flag = manager.getActiveNetworkInfo().isAvailable();
}
if (!flag) {
return NONET;
} else {
State gprs = manager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
// 判断为wifi状态下才加载广告,如果是GPRS手机网络则不加载!
if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
// Toast.makeText(this, "wifi is open! wifi",
// Toast.LENGTH_SHORT).show();
return WIFI;
}
if (gprs == State.CONNECTED || gprs == State.CONNECTING) {
// Toast.makeText(this, "wifi is open! gprs",
// Toast.LENGTH_SHORT).show();
return GPRS;
}
return NONET;
// isNetworkAvailable();
}
// return flag;
}
判断手机是否安装了某个应用:
/** * 判断手机是否安装某个应用 * * @param context * @param appPackageName * 应用包名 * @return true:安装,false:未安装 */ public static boolean isApplicationAvilible(Context context, String appPackageName) { try { PackageManager packageManager = context.getPackageManager();// 获取packagemanager List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息 if (pinfo != null) { for (int i = 0; i < pinfo.size(); i++) { String pn = pinfo.get(i).packageName; if (appPackageName.equals(pn)) { return true; } } } } catch (Exception e) { return false; } return false; }
判断文字相似度:
/** * 计算两个字符串的相似度 * * @param str1 * @param str2 * @return 相似度最大为1(相同字符串),最小为0 */ public static float levenshtein(String str1, String str2) { // 计算两个字符串的长度。 int len1 = str1.length(); int len2 = str2.length(); // 建立上面说的数组,比字符长度大一个空间 int[][] dif = new int[len1 + 1][len2 + 1]; // 赋初值,步骤B。 for (int a = 0; a <= len1; a++) { dif[a][0] = a; } for (int a = 0; a <= len2; a++) { dif[0][a] = a; } // 计算两个字符是否一样,计算左上的值 int temp; for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (str1.charAt(i - 1) == str2.charAt(j - 1)) { temp = 0; } else { temp = 1; } // 取三个值中最小的 dif[i][j] = min(dif[i - 1][j - 1] + temp, dif[i][j - 1] + 1, dif[i - 1][j] + 1); } } // System.out.println("字符串"" + str1 + ""与"" + str2 + ""的比较"); // // 取数组右下角的值,同样不同位置代表不同字符串的比较 // System.out.println("差异步骤:" + dif[len1][len2]); // 计算相似度 float similarity = 1 - (float) dif[len1][len2] / Math.max(str1.length(), str2.length()); // System.out.println("相似度:" + similarity); return similarity; }
判断外存是否可用:
/** * Checks if external storage is available for read and write */ public static boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; }
得到屏幕长宽(单位pix):
DisplayMetrics dm = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(dm); int widthPixels = dm.widthPixels;// 1280 int heightPixels = dm.heightPixels;// 752
得到外存或者sd存储的路径:
/** * get sd card path by reflecting.also usable when api is 23 * * @param mContext * @param is_removale false inner storage;true outer storage. * @return */ private static String getStoragePath(Context mContext, boolean is_removale) { StorageManager mStorageManager = (StorageManager) mContext .getSystemService(Context.STORAGE_SERVICE); Class<?> storageVolumeClazz = null; try { storageVolumeClazz = Class .forName("android.os.storage.StorageVolume"); Method getVolumeList = mStorageManager.getClass().getMethod( "getVolumeList"); Method getPath = storageVolumeClazz.getMethod("getPath"); Method isRemovable = storageVolumeClazz.getMethod("isRemovable"); Object result = getVolumeList.invoke(mStorageManager); final int length = Array.getLength(result); for (int i = 0; i < length; i++) { Object storageVolumeElement = Array.get(result, i); String path = (String) getPath.invoke(storageVolumeElement); boolean removable = (Boolean) isRemovable .invoke(storageVolumeElement); if (is_removale == removable) { return path; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }
得到当前api的level:
int apiLevel = Build.VERSION.SDK_INT;