Android---性能优化方案分享,高级android开发强化实战pdf

本文介绍了Android应用性能优化中的插桩技术,包括如何使用Debug类进行插桩,生成.trace文件以及如何通过Android Studio和traceview查看分析数据。同时,文章对比了AS的CPU Profiler和traceview,推荐了AS作为首选分析工具,并分享了在优化onRebuild()方法时的实践经验。
摘要由CSDN通过智能技术生成
一、插桩

插桩需要使用到 Debug 类,并且会在 sdcard 中生成 .trace 文件,所以你必须首先保证你的应用具有写外部存储( WRITE_EXTERNAL_STORAGE )的权限。

在想要跟踪的代码逻辑开头和结尾处分别插桩:

// Starts recording a trace log with the name you provide. For example, the

// following code tells the system to start recording a .trace file to the

// device with the name “sample.trace”.

Debug.startMethodTracing(“sample”);

// The system begins buffering the generated trace data, until your

// application calls stopMethodTracing(), at which time it writes

// the buffered data to the output file.

Debug.stopMethodTracing();

生成的 .trace 文件会被保存在固定目录下,与 getExternalFilesDir() 返回的目录相同,即 /sdcard/Android/data/[YOUR_PACKAGE_NAME]/files 下。
请注意,如果您的应用在未更改跟踪日志名称的情况下再次调用 startMethodTracing(),则会覆盖已保存至设备的现有日志。如果希望每次运行都保存至不同的日志文件,可以使用如下代码:

// Uses the SimpleDateFormat class to create a String with

// the current date and time.

SimpleDateFormat date =

new SimpleDateFormat(“dd_MM_yyyy_hh_mm_ss”);

String logDate = date.format(new Date());

// Applies the date and time to the name of the trace log.

Debug.startMethodTracing(

“sample-” + logDate);

如果系统在您调用 stopMethodTracing() 之前达到最大缓冲值,则会停止跟踪并向管理中心发送通知。 开始和停止跟踪的函数在您的整个应用流程内均有效。 也就是说࿰

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Build fast and efficient Android apps that run as reliably as clockwork in a multi-device world About This Book Wide coverage of various topics that help in developing optimal applications Explore the concepts of Advanced Native Coding in depth A must-have for professional-standard Android developers for whom performance failures and the sloppy use of resources are simply unacceptable Who This Book Is For This book is aimed at developers with an advanced knowledge of Android and who want to test their skills and learn new techniques to increase the performance of their applications. We assume they are comfortable working with the entire Android SDK, and have been doing it for a few years. They need to be familiar with frameworks such as NDK to use native code, which is crucial for app performance What You Will Learn Create Android applications that squeeze the most from the limited resource capacity of devices Swap code that isn't performing Efficient memory management by identifying problems such as leaks Reap the benefits of multithreaded and asynchronous programming Maximize the security and encryption mechanisms natively provided by Android Perform efficient network operations and techniques to retrieve data from servers Master the NDK to write native code that can perform faster operations In Detail Performant applications are one of the key drivers of success in the mobile world. Users may abandon an app if it runs slowly. Learning how to build applications that balance speed and performance with functionality and UX can be a challenge; however, it's now more important than ever to get that balance right. Android High Performance will start you thinking about how to wring the most from any hardware your app is installed on, so you can increase your reach and engagement. The book begins by providing an introduction to state–of-the-art Android techniques and the importance of performance in an Android application. Then, we will explain the Android SDK tools regularly used to debug and profile Android applications. We will also learn about some advanced topics such as building layouts, multithreading, networking, and security. Battery life is one of the biggest bottlenecks in applications; and this book will show typical examples of code that exhausts battery life, how to prevent this, and how to measure battery consumption from an application in every kind of situation to ensure your apps don't drain more than they should. This book explains techniques for building optimized and efficient systems that do not drain the battery, cause memory leaks, or slow down with time. Style and approach The book follows a tutorial-based approach to take the reader from the basic fundamentals of debugging to advanced performance-improvement concepts. Table of Contents Chapter 1: Introduction: Why High Performance? Chapter 2: Efficient Debugging Chapter 3: Building Layouts Chapter 4: Memory Chapter 5: Multithreading Chapter 6: Networking Chapter 7: Security Chapter 8: Optimizing Battery Consumption Chapter 9: Native Coding in Android Chapter 10: Performance Tips
目录 ······ 第1章 Android简介 1.1 一些背景信息 1.1.1 不远的过去 1.1.2 未来的前景 1.2 对Android的误解 1.3 开放的移动开发平台 1.4 自带的Android应用程序 1.5 Android SDK功能 1.5.1 对包括摄像头、GPS和加速计在内的硬件的访问 1.5.2 自带的Google地图、地理编码和基于位置的服务 1.5.3 后台服务 1.5.4 SQLite数据存储和检索数据库 1.5.5 共享数据和应用程序间通信 1.5.6 使用Google Talk的P2P服务 1.5.7 扩展的数据支持和2D/3D图形 1.5.8 优化的内存和进程管理 1.6 开放手机联盟简介 1.7 运行Android的环境 1.8 从事Android开发的原因 1.8.1 推动Android普及的因素 .1.8.2 Android的独到之处 1.8.3 改变移动开发格局 1.9 开发框架简介 1.9.1 开发包中的资源 1.9.2 理解Android软件栈 1.9.3 Dalvik虚拟机 1.9.4 Android应用程序架构 1.9.5 Android库 1.9.6 高级Android库 1.10 小结 第2章 开始入手 2.1 Android开发 2.1.1 开始前的准备工作 2.1.2 创建第一个Android活动 2.1.3 Android应用程序的类型 2.2 面向移动设备的开发 2.2.1 关于硬件设计的考虑事项 2.2.2 考虑用户环境 2.2.3 Android开发 2.3 To-Do List示例 2.4 Android开发工具 2.4.1 Android模拟器 2.4.2 Dalvik调试监控服务(DDMS) 2.4.3 Android调试桥(ADB) 2.5 小结 第3章 创建应用程序和活动 3.1 Android应用程序的组成部分 3.2 程序清单简介 3.3 使用清单编辑器 3.4 Android应用程序生命周期 3.5 理解应用程序的优先级和进程状态 3.6 分离资源 3.6.1 创建资源 3.6.2 使用资源 3.6.3 To-DoList资源示例 3.6.4 为不同的语言和硬件创建资源 3.6.5 运行时配置更改 3.7 深入探讨Android活动 3.7.1 创建一个活动 3.7.2 活动生命周期 3.7.3 Android活动类 3.8 小结 第4章 创建用户界面 4.1 Android UI基本设计 4.2 View简介 4.2.1 使用View创建Activity(活动)用户界面 4.2.2 Android Widget工具箱 4.3 布局简介 4.4 创建新的View 4.4.1 修改现有的View 4.4.2 创建复合控件 4.4.3 创建定制的Widget和控件 4.4.4 使用定制的控件 4.5 创建和使用菜单 4.5.1 Android菜单系统简介 4.5.2 定义活动的菜单 4.5.3 动态更新菜单项 4.5.4 处理菜单选择 4.5.5 子菜单和上下文菜单 4.5.6 To-Do List示例续 4.6 小结 第5章 Intent、广播接收器、 Adapter和Internet 5.1 Intent简介 5.1.1 使用Intent来启动活动 5.1.2 使用Intent Filter来为隐式Intent提供服务 5.1.3 使用Intent Filter作为插件和扩展 5.1.4 使用Intent来广播事件 5.2 Adapter简介 5.2.1 Android提供的部分Adapter简介 5.2.2 使用Adapter绑定数据 5.3 使用Internet资源 5.3.1 连接到Internet资源 5.3.2 利用Internet资源 5.4 Dialog简介 5.4.1 Dialog类简介 5.4.2 使用活动作为对话框 5.5 创建一个地震查看器 5.6 小结 第6章 数据存储、检索和共享 6.1 Android中的数据保存技术 6.2 保存简单的应用程序数据 6.2.1 创建和保存preference 6.2.2 检索共享的preference 6.2.3 保存活动状态 6.2.4 为地震查看器创建一个Preference页 6.3 保存和载入文件 6.3.1 将静态文件作为资源添加 6.3.2 文件管理工具 6.4 Android中的数据库 6.4.1 SQLite简介 6.4.2 Cursor和内容值 6.4.3 使用Android数据库 6.5 内容提供器简介 6.5.1 使用内容提供器 6.5.2 本地Android内容提供器 6.5.3 创建一个新的内容提供器 6.5.4 创建和使用地震内容提供器 6.6 小结 第7章 地图、地理编码和基于位置的服务 7.1 使用基于位置的服务 7.2 使用Test Proyider构建模拟器 7.2.1 更新模拟位置提供器中的位置 7.2.2 创建一个应用程序来管理Test Location Provider 7.3 选择——个Location Provider 7.3.1 查找可用的提供器 7.3.2 根据要求标准查找提供器 7.4 确定自己所在的位置 7.4.1 “Where Am I”示例 7.4.2 追踪移动 7.4.3 更新“WhereAmI”示例中的位置 7.5 使用邻近提醒 7.6 使用Geocoder 7.6.1 反向地理编码 7.6.2 前向地理编码 7.6.3 对“Where Am I”示例进行地理编码 7.7 创建基于地图的活动 7.7.1 MapView和MapActivity简介 7.7.2 创建一个基于地图的活动 7.7.3 配置和使用MapView 7.7.4 使用MapController 7.7.5 对“Where Am I”示例使用地图 7.7.6 创建和使用覆盖(Overlay) 7.7.7 MyLocationOverlay简介 7.7.8 ItemizedOverlay和Overlayltem简介 7.7.9 将View固定到地图和地图的某个位置上 7.8 对Earthquake示例添加地图功能 7.9 小结 第8章 后台工作 8.1 服务简介 8.1.1 创建和控制服务 8.1.2 将活动和服务绑定 8.2 使用后台工作线程 8.2.1 创建新的线程 8.2.2 为GUI操作同步线程 8.2.3 将Earthquake Service移动到后台线程 8.3 创建一个Toast 8.3.1 定制Toast 8.3.2 在工作(worker)线程中使用Toast 8.4 Notification简介 8.4.1 Notification Manager简介 8.4.2 创建Notification 8.4.3 触发Notification 8.4.4 向Earthquake Monitor中添加Notiflcation 8.4.5 高级Notification技术 8.4.6 持续的和连续的Notification 8.5 使用Alarm 8.6 使用Alarm更新Earthquake 8.7 小结 第9章 P2P通信 9.1 Android即时消息简介 9.1.1 使用GTalk服务 9.1.2 和GTalk服务绑定 9.1.3 建立GTalk连接,开始一个IM会话 9.1.4 在线状态和联系人列表简介 9.1.5 管理聊天会话 9.1.6 发送和接收数据信息 9.2 SMS简介 9.2.1 在应用程序中使用SMS 9.2.2 发送SMS信息 9.2.3 监听SMS消息 9.2.4 紧急响应的SMS示例 9.2.5 紧急响应自动化 9.3 小结 第10章 访问Android硬件 10.1 使用媒体API 10.1.1 播放媒体资源 10.1.2 录制多媒体 10.2 使用摄像头 10.2.1 控制摄像头设置 10.2.2 使用摄像头预览 10.2.3 照相 10.3 Sensor Manager简介 10.4 使用加速计和指南针 10.4.1 加速计介绍 10.4.2 检测加速度的改变 10.4.3 创建一个速度计 10.4.4 确定方向 10.4.5 创建指南针和地平仪 10.5 Android电话功能 10.5.1 打电话 10.5.2 监控电话状态和电话活动 10.5.3 监控数据连通性和活动 10.5.4 访问手机的属性和状态 10.5.5 控制电话 10.6 使用蓝牙 10.6.1 蓝牙服务介绍 10.6.2 控制本地蓝牙设备 10.6.3 发现并配对蓝牙设备 10.6.4 管理蓝牙连接 10.6.5 使用蓝牙进行通信 10.6.6 使用蓝牙耳机 10.7 管理网络和Wi-Fi连接 10.7.1 监控和管理Internet连接 10.7.2 管理活动的连接 10.7.3 管理你的Wi-Fi 10.8 控制设备震动 10.9 小结 第11章 Android高级开发 11.1 Android的安全性 11.1.1 Linux内核安全 11.1.2 权限简介 11.1.3 声明和实施权限 11.1.4 为广播Intent实施权限 11.2 使用AIDL来支持服务IPC 11.3 使用Internet服务 11.4 构建内容丰富的用户界面 11.4.1 使用动画 11.4.2 使用主题来为应用程序添加皮肤 11.4.3 高级画布绘图 11.4.4 SurfaceView简介 11.4.5 创建交互式控件 11.5 小结

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值