Android开发中常用的工具类整理privateL(){thrownewUnsupportedOperationExc eption(Lc annotinstantiated!}public staticvoidv(Stringtag,Stringmsg){if(LEVE L=5)Log.v(tag==null?DEFAULT_TAG:tag,ms g==null? :ms g);}public staticvoidd(Stringtag,Stringmsg){if(LEVE L=4)Log.d(tag==null?DEFAULT_TAG:tag,ms g==null? :ms g);}public staticvoidi(Stringtag,Stringms g){if(LEVE L=3)Log.i(tag==null?DEFAULT_TAG:tag,ms g==null? :ms g);}public staticvoidw(Stringtag,Stringmsg){if(LEVE L=2)Log.w(tag==null?DEFAULT_TAG:tag,ms g==null? :ms g);}public staticvoide(Stringtag,Stringmsg){if(LEVE L=1)Log.e(tag==null?D EFAULT_TAG:tag,ms g==null? :ms g);}
}
Toas tpac kagenet.wujingc hao.android.utilityimportandroid.c ontent.Context;
importandroid.w idget.Toast;publicclassT{privatefinalstaticbooleanis Show=true;privateT(){thrownewUnsupportedOperationExc eption(Tc annotbeinstantiated}public staticvoidshow Short(Contextc ontext,CharSequenc etext){if(is Show)Toast.makeText(c ontext,text,Toast.LENGTH_S HO RT).show();}public staticvoidshowLong(Contextc ontext,CharSequenc etext){if(is Show)Toast.makeText(c ontext,text,Toast.LENGTH_LONG).show();}
}pac kagenet.wujingc hao.android.utilityimportandroid.app.Activ ity;importandroid.c ontent.ComponentName;importandroid.c ontent.Context;importandroid.c ontent.Intent;importandroid.net.Connec tiv ityManag er;importandroid.net.Netw orkInfo;importjav ax.net.s sl.HttpsURLConnection;importjav ax.net.s sl.S SLContext;
importjav ax.net.s s l.TrustManager;importjav ax.net.s s l.X509Trus tManager;publicclassNetworkUtil{privateNetw orkUt il(){thrownewUnsupportedOperationExc eption(Netw orkUtilc annotbeinstantiated
}
/**
*判断网络是否连接
*
*/public staticbooleanisConnected(Contextc ontext){
Connec tivityManagerc onnec tivity=(Connectiv ityManager)c ontext.getSystemS ervic e(Context.CONNECT I VITY_S ERVI CE);if(null!=c onnec tivity){
Netw orkInfoinfo=c onnec tivity.getAc tiveNetw orkInfo();if(null!=info info.isConnected()){if(info.getState()==Netw orkInfo.State.CONNECTED){r eturntrue;
}
}
}
r eturnfals e;
}
/**
*判断是否是wifi连接
*/public staticbooleanisWifi(Contextc ontext){
ConnectivityManagerc onnectivity=(Connectiv ityManager)c ontext.getSystemS ervic e(Context.CONNECT I VITY_S ERVI CE);if(c onnec tivity==null)returnfals e;returnc onnec tivity.getAc tiv eNetw orkInfo().getType()==Connec tivityManager.TYP E_WIF I;
}
/**
*打开网络设置界面
*/public static voidopenS etting(Ac tiv ityactiv ity){
Intentintent=new Intent(/
ComponentNamec omponentName=newComponentName( c om.android.s ettings ,com.android.settings.Wireless Settingsintent.s etComponent(c omponentName);intent.s etAc tion(android.intent.ac tion.VIEWac tivity.s tartAc tiv ityF orResult(intent,0);
}
/**
*使用SSL不信任的证书
*/public staticvoidus eUntrustedCertific ate(){
//Createatrus tmanagerthatdoesnotvalidatec ertific atec hains
Trus tManager[]trus tAllCerts=new Trus tManager[]{new X509TrustManager(){publicjava.s ecurity.c ert.X509Certific ate[]getAc c eptedIssuers(){r eturnnull;
}publicvoidcheckClientTrusted(java.s ecurity.c ert.X509Certific ate[]c erts,StringauthType){
}publicvoidcheckServerTrusted(java.s ecurity.c ert.X509Certific ate[]c erts,StringauthType){
}
}
};
//Ins tallthea ll-trus tingtrus tmanagertry{
S SLContextsc=S SLContext.getInstanc e(SSLs c.init(null,trustAllCerts,newjava.s ec urity.S ecureRandom());
HttpsURLConnection.s etDefaultS SLSocketFactory(sc.getSocketFactory());
}catch(Exc eptione){e.printStackTrac e();
}
}
}
像素单位转换pac kagenet.wujingc hao.android.utilityimportandroid.c ontent.Context;importandroid.util.TypedValue;publicclassDensityUtil{privateDensityUtil(){thrownewUnsupportedOperationExc eption(DensityUtilc annotbeinstantiated}public intdip2px(Contextc ontext,intdip Value){finalfloats c ale=c ontext.getRes ourc es().getDisplayM etric s().dens ity;return(int)(dip Va lue*s c ale+0.5 f);
}public intpx2dip(Contextc ontext,floatpxValue){finalfloats c ale=c ontext.getRes ourc es().getDisplayM etric s().dens ity;return(int)(pxVa lue/s c ale+0.5 f);
}public static floatpx2sp(Contextc ontext,floatpxValue){
return(pxVa lue/c ontext.getRes ourc es().getDis playMetr ic s().sc aledDens ity);}public static intsp2px(Contextc ontext,intspValue){return(int)TypedVa lue.app lyD imens ion(TypedVa lu e.COMPLEX_UNIT_S P,spVa lue,c ontext.getRes ourc es().getDisplayMetr ic s());
}
}pac kagenet.wujingc hao.android.utilityimportandroid.app.Activ ity;importandroid.c ontent.Context;importandroid.graphic s.Bitmap;importandroid.graphic s.Rect;importandroid.util.D isplayMetric s;importandroid.view.View;importandroid.view.WindowManager;publicclassScreenUtil{privateScreenUtil()
{thrownewUnsupportedOperationExc eption(S creenUtilc annotbe instantiated}public static intgetScreenWidth(Contextc ontext)
{
.getSystemS ervic e(Context.WINDOW_SERVICE);
Dis play Metr ic s outMetric s=new Dis playMetr ic s();w m.getDefaultD is play().getMetric s(outMetric s);r eturnoutMetric s.w idthP ixels;
}public static intgetScreenHe ight(Contextc ontext){
WindowManag erwm=(WindowManager)c ontext
.getSystemS ervic e(Context.WINDOW_SERVICE);
Dis play Metr ic s outMetric s=new Dis playMetr ic s();w m.getDefaultD is play().getMetric s(outMetric s);returnoutMetric s.heightP ixels;
}public static intgetStatusHe ight(Contextc ontext){ints tatus He ight=-1;try{
Clas s ?c lazz=Clas s.forName(c om.android.internal.R$dimenObjectobject=clazz.newInstance();inthe ight=Integer.pars eInt(c lazz.getF ie ld( s tatus_bar_height ).get(object).toString());statusHeight=c ontext.getRes ourc es().getDimens ionPix elS iz e(height);}catch(Exc eptione){
e.printStackTrac e();
}r eturn s tatus Height;
}
/**
*获取当前屏幕截图包含状态栏
*/public static BitmapsnapShotWithStatusBar(Activity activity){Viewview=ac tivity.getWindow().getDec orView();view.s etDrawingCacheEnab led(true);view.buildDrawingCache();
Bitmapbmp=v iew.getDraw ingCache();intw idth=g et S c r e enWidth(ac tiv ity);inthe ight=getS c reenHe ight(ac tiv ity);
Bitmapbp=nu ll;bp=Bitmap.c reateBitmap(bmp,0,0,w idth,height);view.destroyDrawingCache();r eturnb p;
}
/**
*获取当前屏幕截图不包含状态栏
*
IT狗为用户提供 在线ping、在线tcping、在线路由追踪、域名被墙检测、域名被污染检测 等实用工具。【工具地址】https://www.itdog.cn/【工具特色】1、目前同类网站中,在线ping 仅支持1次或少量次数的测试,无法客观的展现目标服务器一段时间的网络状况,IT狗Ping工具可持续的进行一段时间的ping测试,并生成更为直观的网络质量柱状图,让用户更容易掌握服务器在各地区、各线...
最近AS9929线路比较火,联通A网,对标电信CN2,HostYun也推出了走联通AS9929线路的VPS主机,基于KVM架构,开设在洛杉矶机房,采用SSD硬盘,分为入门和高带宽型,最高提供500Mbps带宽,可使用9折优惠码,最低每月仅18元起。这是一家成立于2008年的VPS主机品牌,原主机分享组织(hostshare.cn),商家以提供低端廉价VPS产品而广为人知,是小成本投入学习练手首选。...
Friendhosting发布了针对“系统管理日”(每年7月的最后一个星期五)的优惠活动,针对VPS主机提供55%的优惠(相当于四五折),支持1-6个月付款使用,首付折扣非永久,优惠后最低套餐首半年7.18欧元起。这是一家保加利亚主机商,成立于2009年4月,商家提供VDS和独立服务器租用等,数据中心目前可选美国洛杉矶、保加利亚、乌克兰、荷兰、拉脱维亚、捷克和波兰等8个地区机房。下面以最低套餐为例...