当前位置: 代码迷 >> Android >> android4出发
  详细解决方案

android4出发

热度:59   发布时间:2016-04-28 05:06:11.0
android4启程

?1.Android4比2.3多了fragment,通常情况controls都写在fragment里面,故main UI的很多 code都写在MainActivity的内部类PlaceholderFragment的onCreateView方法中(2年没整 ? Android,这个更新让惯性思维的我抓狂了)。

?2.Android4.0之后不能再主线程中进行HTTP请求,or throw ? android.os.NetworkOnMainThreadException

? ?解决方法:

(1)http请求前加上如下代码

? ? ? ?

	            if (Build.VERSION.SDK_INT >= 11) {	                StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());	                StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());	            }

?

(2)开启新的thread,建议使用AsyncTask类

? ?

  相关解决方案