android2.3与android4.0访问网络的异同
2013-10-29 08:44
211 查看
在android 2.3上设计的下载程序,在android 4.0上运行时报android.os.NetworkOnMainThreadException异常,原来在4.0中,访问网络不能在主程序中进行,有两个方法可以解决,一个是在主程序中增加:
Java代码
// 详见StrictMode文档
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
另一种是启动线程执行下载任务:
public void onCreate(Bundle savedInstanceState) {
Java代码
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 启动线程执行下载任务
new Thread(downloadRun).start();
}
/**
* 下载线程
*/
Runnable downloadRun = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
updateListView();
}
};
Java代码
// 详见StrictMode文档
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
另一种是启动线程执行下载任务:
public void onCreate(Bundle savedInstanceState) {
Java代码
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 启动线程执行下载任务
new Thread(downloadRun).start();
}
/**
* 下载线程
*/
Runnable downloadRun = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
updateListView();
}
};
相关文章推荐
- android2.3与android4.0访问网络的异同
- android 2.3 数据流量开关 移动网络的数据访问功能,非APN开关
- “android.os.NetworkOnMainThreadException”---关于Android4.0后无法将访问网络放入主线程中的解决办法
- Android 2.3网络访问时,使用Authorization字段对接口加验证的问题
- android 2.3 开始主线程不能访问网络
- Android 对网络访问的封装
- android2.3实现android4.0风格的edittext
- Android主线程不能访问网络异常解决办法
- Android进阶2之Http操作访问网络
- Android Http协议访问网络实例(3种)
- android编辑访问网络图片
- 关于android主线程异常NetworkOnMainThread不能访问网络
- android典型应用之访问网络
- Android app网络访问控制
- Android仿Volley手写属于自己的万能网络访问框架
- android中使用http协议访问网络
- android 中访问网络介绍 一 (基于httpurlconnection 的中get请求)
- Android 网络访问
- Android Volley网络访问
- Android开发循序渐进实例5--网络访问例子