为防止更新不及时,github地址如下:https://github.com/SolveBugs/NormalCode
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.ListView根据item计算出实际的高度
public static void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
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){
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phoneNum));
startActivity(intent);
}
- 字符串是否包含汉字
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) { i
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;
}