android2.3与android4.0访问网络的异同
来源:互联网 发布:机械能守恒实验数据 编辑:程序博客网 时间:2024/05/23 00:50
在android 2.3上设计的下载程序,在android 4.0上运行时报android.os.NetworkOnMainThreadException异常,原来在4.0中,访问网络不能在主程序中进行,有两个方法可以解决,一个是在主程序中增加:
- // 详见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) {
- 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访问网络的异同
- android2.3与android4.0访问网络的异同
- Android4.0 与 android2.3 差异
- Android4.0 与 android2.3 差异
- Android4.0与Android2.3源代码差异
- Android4.0 与 android2.3 差异
- android2与android4在网络连接上的一点差异
- android之MediaPlayer区别(android2.3与android4.0)
- Android2.3与Android4.0有什么区别?
- android之MediaPlayer区别(android2.3与android4.0)
- android2.3实现android4.0风格的edittext
- android2.3实现android4.0风格的edittext
- android2.3实现android4.0风格的edittext
- Android2.2、Android2.3 、Android4.0 audio hardware模块分析
- Android4.0,Android2.0
- 虚拟机VMWARE和Ubuntu下搭建了一个android2.3/android4.2/android4.1/android4.0编译环境的操作过程
- android2.2 与 android2.3的区别
- Android4.0 以后 的网络访问和 数据同步问题
- 更改字符字段的值
- 转载_ google-Protocol-Buffers
- MFC之自定义消息
- 从菜鸟到高手的蜕变,只需四个月!
- php获取当前类名函数名的方法
- android2.3与android4.0访问网络的异同
- Mysql格式化日期函数
- 关于本博客
- 数据库版本升级降级
- MyEclipse 8.5整合Git,并在Github上发布项目
- 使用spring的hibernateTemplate的方法
- 亿万用户网站MySpace的成功秘密
- c# mutex使用示例
- 《软件测试经验与教训》读书笔记(五)