/**
* Created by zhen on 2016/8/19.
*/
/**
*
*/
public class Utils {
1.
/**
*
*
* @param activity
* @param bgAlpha
*/
public static void backgroundAlpha(Activity activity, float bgAlpha) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
activity.getWindow().setAttributes(lp);
}
2.qq
//http://qun.qq.com/join.html
3.
public static int getVersionCode(Context context) {
int code = 0;
if (context == null) {
return code;
}
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
code = info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
}
return code;
}
4.
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()
&& mNetworkInfo.isConnectedOrConnecting();
}
}
return false;
}
5.
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()
&& mMobileNetworkInfo.isConnectedOrConnecting();
}
}
return false;
}
6.wifi
public static boolean isWifi(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
}
return false;
}
7.ListViewitem
public static void setListViewHeightBasedOnChildren(ListView listView) {
// ListViewAdapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); // View
totalHeight += listItem.getMeasuredHeight(); //
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
8.()
/**
* @param phoneNum
*/
public void call(String phoneNum, Context context) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
context.startActivity(intent);
}
9.
public static boolean checkChinese(String sequence) {
final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]";
boolean result = false;
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(sequence);
result = matcher.find();//
return result;
}
10.assets
public static Drawable loadImageFromAsserts(final Context ctx, String fileName) {
try {
InputStream is = ctx.getResources().getAssets().open(fileName);
return Drawable.createFromStream(is, null);
} catch (IOException e) {
if (e != null) {
e.printStackTrace();
}
} catch (OutOfMemoryError e) {
if (e != null) {
e.printStackTrace();
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return null;
}
11.
public static boolean isNull(String string) {
if (string != null) {
string = string.trim();
if (string.length() != 0) {
return false;
}
}
return true;
}
12.
public void deletFiles(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) {
deletFiles(f);
}
file.delete();
}
}
13.
//http://www.iconfont.cn/collections
14.
/**
*
*
* @param activity
* @return
*/
public static int getDisplayWidth(Activity activity) {
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; //
return width;
}
/**
*
*
* @param activity
* @return
*/
public static int getDisplayHeight(Activity activity) {
DisplayMetrics metric = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
int height = metric.heightPixels; //
return height;
}
/**
* pxdipdp
*
* @param context
* @param pxValue px
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* dipdppx
*
* @param context
* @param dipValue dp
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* pxsp
*
* @param context
* @param pxValue px
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* sppx
*
* @param context
* @param spValue sp
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
15. BitmapOOM
/**
* OOM
*
* @param res
* @param resId id
* @param reqWidth
* @param reqHeight
* @return
*/
public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// inJustDecodeBounds = true ,
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// inJustDecodeBounds false
options.inJustDecodeBounds = false;
//options.inPreferredConfigBitmap.Config.RGB_565
// Bitmap.Config.ARGB_8888
options.inPreferredConfig = Bitmap.Config.RGB_565;
return BitmapFactory.decodeResource(res, resId, options);
}
// BitmapFactpry inSimpleSize
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
if (reqWidth == 0 || reqHeight == 0) {
return 1;
}
//
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
// ,
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
//
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
16.
//REQUEST_ENABLE_BTrequestCode
int REQUEST_ENABLE_BT = 0;
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT) {
}
17.Excel
jxl jar
18.
//1.
//2.
android:theme="@style/FlashStyle"
19.
private static long lastClickTime;
public synchronized static boolean isDoubleClick(long time) {
long currentTime = System.currentTimeMillis();
boolean isClick2;
if (currentTime - lastClickTime > time) {
lastClickTime = currentTime;
isClick2 = false;
} else {
isClick2 = true;
}
return isClick2;
}
20.sd
public static String getSDCardPath() {
File sdcardDir = null;
boolean sdcardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdcardExist) {
sdcardDir = Environment.getExternalStorageDirectory();
return sdcardDir.toString();
} else {
return null;
}
}
21
public static String getImageURI(String networkPath, String dirPath, String s) {
if (networkPath == null) {
return null;
}
String name = networkPath.substring(networkPath.lastIndexOf("/"));
File dirFile = new File(dirPath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
File file = new File(dirFile, name + s);
try {
//
URL url = new URL(networkPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
// URI
return dirPath + File.separator + name + s;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
22
public static void initLanguage() {
try {
Log.d("Appcontext", "");
Class activityManagerNative = Class.forName("android.app.ActivityManagerNative");
Log.i("amnType", activityManagerNative.toString());
Object am = activityManagerNative.getMethod("getDefault").invoke(activityManagerNative);
Log.i("amType", am.getClass().toString());
Object config = am.getClass().getMethod("getConfiguration").invoke(am);
Log.i("configType", config.getClass().toString());
config.getClass().getDeclaredField("locale").set(config, Locale.ENGLISH);
config.getClass().getDeclaredField("userSetLocale").setBoolean(config, true);
am.getClass().getMethod("updateConfiguration", android.content.res.Configuration.class).invoke(am, config);
} catch (Exception e) {
e.printStackTrace();
}
}
23.
public static byte getXor(byte[] datas) {
byte temp = datas[0];
for (int i = 1; i < datas.length; i++) {
temp ^= datas[i];
}
return temp;
}
}