/**
* @Title: checkHasNetwork
* @Description:检查网络连接
* @param context
* @return true 有网络 ;false 无网络
*/
public static boolean checkHasNetwork(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
if (netInfo == null || !netInfo.isAvailable()) {
// 没有网络连接
LogUtil.e("internet__connect", "没有网络连接");
return false;
}
return true;
}
-----------------------------------------------------------
/**
* 设置网络超时
*
* @param clientLoginField
*/
public static void setConnectionTimeOut(DefaultHttpClient clientLoginField) {
HttpParams httpParams = clientLoginField.getParams();
// 设置网络超时参数
HttpConnectionParams.setConnectionTimeout(httpParams, 30 * 1000);
HttpConnectionParams.setSoTimeout(httpParams, 30 * 1000);
}
----------------------------------------------------------------
/**
* @author:
* @Title: checkNetworkConnected
* @Description:是否有可用网络
* @param context
* @return
* @date: 2013-4-2 上午11:03:08
*/
public static boolean checkNetworkConnected(Context context) {
// 获得手机所有连接管理对象(包括对wi-fi等连接的管理)
try {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
// 获得网络连接管理的对象
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
// 判断当前网络是否已连接
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
-----------------------------------------------------------------------------------
/**
* 手机号码检查
*/
public static Boolean phonenumberCheck(String phoneNumber) {
// 表达式对象
Pattern p = Pattern.compile("^(1)[0-9]{10}$");
// 创建 Matcher 对象
Matcher m = p.matcher(phoneNumber);
if (!m.matches()) {
return false;
}
return true;
}
---------------------------------------------------------------------
/**
* 取得应用当前的版本号
*
* @return
*/
public static String getCurrentVersion(Context context) {
String versionName = "";
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
versionName = packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
---------------------------------------------------------------------------
/**
* 隐藏界面输入软键盘
*
* @param activity
*/
public static void hideSoftInputFromWindow(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
View view = activity.getCurrentFocus();
if (view != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
}
}
----------------------------------------------------------------------------------
/**
* 从指定的URL中获取数组
* @param urlPath
* @return
* @throws Exception
*/
public String readParse(String urlPath) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inStream = conn.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
}
inStream.close();
return new String(outStream.toByteArray());//通过out.Stream.toByteArray获取到写的数据
}
----------------------------------------------------------------------------------------
/**
* 将参数列表封装成dopost需要得数据类型
* @param params是传进去map
* @return 返回dopost要求的形式
*/
public List<NameValuePair> getNameValuePairList(Map<String, Object> params) {
List<NameValuePair> nvpsList = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue() + "";
nvpsList.add(new BasicNameValuePair(key, value));
}
return nvpsList;
}
---------------------------------------------------------------------------------------------
/**
* Json数据的解析
* @param jsonStr 是传递进去的jsonStr数据
* @param 解析后以list的形式返回
* @throws JSONException
*/
private ArrayList<HashMap<String, Object>> Analysis(String jsonStr) throws JSONException {
/******************* 解析 ***********************/
JSONArray jsonArray = null;
// 初始化list数组对象
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// 初始化map数组对象
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("logo", jsonObject.getString("logo"));
map.put("logoLunbo", jsonObject.getString("logoLunbo"));
map.put("biaoTi", jsonObject.getString("biaoTi"));
map.put("yuanJia", jsonObject.getString("yuanJia"));
map.put("xianJia", jsonObject.getString("xianJia"));
map.put("id", jsonObject.getInt("id"));
list.add(map);
}
return list;
}
------------------------------------------------------
/**
* 检查对于包名的apk是否安装
*
* @return
*/
private boolean checkAPKInstalled(String packageName) {
PackageInfo packageInfo = null;
try {
packageInfo = this.getPackageManager().getPackageInfo(packageName,
0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
if (packageInfo == null) {
return false;
} else {
return true;
}
}
-------------------------------------------------------------------------------------
去除网页标签的方法,用的是正则表达式
public static String removeTagFromText(String content) {
Pattern p = null;
Matcher m = null;
String value = null;
// 去掉<>标签
p = Pattern.compile("(<[^>]*>)");
m = p.matcher(content);
String temp = content;
while (m.find()) {
value = m.group(0);
temp = temp.replace(value, "");
}
// 去掉换行或回车符号
p = Pattern.compile("(\r+|\n+)");
m = p.matcher(temp);
while (m.find()) {
value = m.group(0);
temp = temp.replace(value, " ");
}
if(temp.length() > 10) {
return temp.substring(0,10) + "...";
} else {
return temp + "...";
}
}
------------------------------------------------------------------------------------------
拼服务器看能否拼通
Process p = Runtime.getRuntime().exec(http://www.baidu.com);
InputStreamReader r = new InputStreamReader(p.getInputStream());
LineNumberReader returnData = new LineNumberReader(r);
String returnMsg = "";
String line = "";
while ((line = returnData.readLine()) != null) {
System.out.println(line);
returnMsg += line;
}
System.out.println("sky for return message"+returnMsg);
--------------------------------------------------------------------------------------------------
/**
* 递归删除文件和文件夹
* @param file 要删除的根目录
*/
public static void RecursionDeleteFile(File file){
if(file.isFile()){
file.delete();
return;
}
if(file.isDirectory()){
File[] childFile = file.listFiles();
if(childFile == null || childFile.length == 0){
file.delete();
return;
}
for(File f : childFile){
RecursionDeleteFile(f);
}
file.delete();
}
}