1.整个类都被我们封装的比较好,调用的时候非常的方便,全部都是静态型的变量,这样我们使用起也是非常的安逸和棒的!
package com.lee.privatecustom.utils;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.lee.privatecustom.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
/**
* 通用工具类
*
* @author JetWang
*/
public class CommonUtils {
private static final String TAG = "CommonUtils";
/**
* 开启activity
*/
public static void launchActivity(Context context, Class
activity) {
Intent intent = new Intent(context, activity);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intent);
}
public static void launchActivityForResult(Activity context,
Class
activity, int requestCode) {
Intent intent = new Intent(context, activity);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivityForResult(intent, requestCode);
}
/**
* 隐藏软键盘
*/
public static void hideSoftKeybord(Activity activity) {
if (null == activity) {
return;
}
try {
final View v = activity.getWindow().peekDecorView();
if (v != null && v.getWindowToken() != null) {
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
} catch (Exception e) {
}
}
/**
* 判断是否为合法的json
*
* @param jsonContent
* 需判断的字串
*/
public static boolean isJsonFormat(String jsonContent) {
try {
new JsonParser().parse(jsonContent);
return true;
} catch (JsonParseException e) {
LogUtils.i(TAG, "bad json: " + jsonContent);
return false;
}
}
/**
* 判断字符串是否为空
*
* @param text
* @return true null false !null
*/
public static boolean isNull(String text) {
if (text == null || "".equals(text.trim()) || "null".equals(text))
return true;
return false;
}
/**
* 抖动动画
*/
public static void startShakeAnim(Context context, View view) {
Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);
view.startAnimation(shake);
}
/**
* 判断网络是否可用
*/
public static boolean isNetworkAvailable(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) {
return true;
}
}
}
}
return false;
}
/**
* 显示进度
*
* @param context
* @param title
* @param message
* @param indeterminate
* @param cancelable
* @return
*/
public static ProgressDialog showProgress(Context context,
CharSequence title, CharSequence message, boolean indeterminate,
boolean cancelable) {
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setIndeterminate(indeterminate);
dialog.setCancelable(cancelable);
dialog.setCanceledOnTouchOutside(false);
// dialog.setDefaultButton(false);
dialog.show();
return dialog;
}
/**
* 获取我们的应用的信息!
* @param context
* @return
*/
public static String softVersion(Context context) {
PackageInfo info = null;
try {
info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return info.versionName;
}
}
我们可以比较好的管理我们的类,每次使用的时候也是比较的方便的,可以吧很多的东西,封装起来使用,这样不用每次都去重新的书写我们的代码片段了
这个是一个比较简单的基本的技能,我们要随时的掌握好这样的技能!