不知不觉中,就收集了超过95条的自己感觉有意思的代码片段,分为五篇文章:android有用代码片段、Android有用代码片段(二)、Android有用代码片段(三)、Android有用代码片段(四)、
Android有用代码片段(五)。这五篇,今天,开始第六篇的整理!这里解释一下,因为一、二、三都是每个有20个片段,但是在四中,由于第70个代码过长,所以在第四篇中,只有10个片段。 第五篇里面有25个。
九十六、java.util.MissingFormatArgumentException 错误
在有站位符的打印语句中,经常会犯下一个错误。
如:System.out.printf( "y=%3d "+y),就会报这个错误。
应修改为:;改为System.out.printf( "y=%3d ",y)
九十七、Android判断是Pad或者手机
public boolean isTabletDevice() { TelephonyManager telephony = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE); int type = telephony.getPhoneType(); if (type == TelephonyManager.PHONE_TYPE_NONE) { return true; } else { return false; } }九十八、判断android网络状态
State mWifiState = null; State mMobileState = null; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); mWifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); mMobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) .getState(); if (mWifiState != null && mMobileState != null && State.CONNECTED != mWifiState && State.CONNECTED == mMobileState) { // 手机网络连接成功 } else if (mWifiState != null && mMobileState != null && State.CONNECTED != mWifiState && State.CONNECTED != mMobileState) { mHandler.sendEmptyMessage(MSG_SHOW_NET_DIALOG); } else if (mWifiState != null && State.CONNECTED == mWifiState) { // 无线网络连接成功 }