Android 开发中的一些小技巧
2018-07-20 来源:open-open
dip转px
public int convertDipOrPx(int dip) {
float scale = MarketApplication.getMarketApplicationContext()
.getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
获取当前窗体,并添加自定义view:
getWindowManager()
.addView(
overlay,
new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT));
自定义fastScrollBar图片样式:
try {
Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object o = f.get(listView);
f = f.getType().getDeclaredField("mThumbDrawable");
f.setAccessible(true);
Drawable drawable = (Drawable) f.get(o);
drawable = getResources().getDrawable(R.drawable.ic_launcher);
f.set(o, drawable);
Toast.makeText(this, f.getType().getName(), 1000).show();
} catch (Exception e) {
throw new RuntimeException(e);
}
=网络==================================
判断网络是否可用:
/**
* 网络是否可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
方法二:
/*
* 判断网络连接是否已开 2012-08-20true 已打开 false 未打开
*/
public static boolean isConn(Context context) {
boolean bisConnFlag = false;
ConnectivityManager conManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = conManager.getActiveNetworkInfo();
if (network != null) {
bisConnFlag = conManager.getActiveNetworkInfo().isAvailable();
}
return bisConnFlag;
}
判断是不是Wifi连接:
public static boolean isWifiActive(Context icontext) {
Context context = icontext.getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info;
if (connectivity != null) {
info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getTypeName().equals("WIFI")
&& info[i].isConnected()) {
return true;
}
}
}
}
return false;
}
判断当前网络类型
/**
* 网络方式检查
*/
private static int netCheck(Context context) {
ConnectivityManager conMan = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (wifi.equals(State.CONNECTED)) {
return DO_WIFI;
} else if (mobile.equals(State.CONNECTED)) {
return DO_3G;
} else {
return NO_CONNECTION;
}
}
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐