authors are vetted experts in their fields and write on topics in which they have demonstrated experience. All of our content is peer reviewed and validated by Toptal experts in the same field.
Ivan Dimoski
Verified Expert in Engineering

Ivan is an accomplished Android developer and consultant with six years of experience developing user-friendly applications.

Expertise

PREVIOUSLY AT

Truecaller
Share

Android. 这个平台有什么不喜欢的? 它是免费的,它是可定制的 rapidly growing and it’s available not just on your phone or tablet, but on your smartwatch, TV and car too.

With the latest Lollipop update, Android programming 持续改善. Android编程软件、工具和资源也是如此. 从一开始,这个平台已经成熟了很多 AOSP 发布,并设置用户期望的门槛相当高. 看这新东西多好 Material 设计模式外观!

There are thousands 不同的设备, 不同的屏幕尺寸, chip architectures, 硬件配置, 软件版本. Unfortunately, 细分是开放的代价, 你的应用在不同设备上失败的方式有上千种, even as an 高级Android程序员.

不管如此巨大的分割, 大多数错误实际上是由于逻辑错误而引入的. 这些漏洞是很容易预防的,只要我们掌握了基本的知识!

Here’s an Android programming tutorial to address the 10 most common Android development mistakes.

学习Android编程在更高级的水平与本教程.

常见错误1:面向iOS开发

令我非常高兴的是, this Android mistake is far less common nowadays (partially because clients are beginning to realize that the days when Apple was setting all the design standards are long gone). 但是,我们仍然时不时地看到一款iOS克隆应用.

不要误解我的意思,我并不是Android开发的传道者! 我尊重每一个推动移动世界向前发展的平台. But, 现在是2014年,用户使用Android已经有一段时间了, 他们已经习惯了这个平台. 将iOS设计标准强加给他们是一种糟糕的策略!

除非有特别好的理由打破 guidelines, don’t do it. (谷歌一直在这么做,但从来没有通过复制粘贴.)

以下是Android最常见的错误:

  1. You should not be making static tabs, and they don’t belong on the bottom (I’m pointing at you Instagram).
  2. 系统通知图标不应该有颜色.
  3. App icons should not be placed inside a rounded rectangle (unless that’s your actual logo ex. facebook).
  4. 启动画面在初始设置/介绍之外是多余的. 不要在其他场景中使用它们.
  5. 列表不应该有插入符号.

这些只是其中的一些 many other 可能会破坏用户体验的小事情.

常见错误2:针对Android设备开发游戏

除非你是在为一台平板电脑创建一个信息亭/促销应用, 你的Android应用可能不会在所有设备上都好看. Here are a few Android programming tips to remember:

有上千种可能的情况, 但过了一段时间,你就会有一种用a来覆盖它们的感觉 handful of cases.

你不会拥有成千上万的设备? Not a problem. Android Emulator在复制物理设备方面非常出色. 更好的是,试一试 Genymotion, it’s lightning fast and comes with a lot of different popular preset devices.

还有,你试过旋转你的设备吗? 所有的地狱都可以挣脱……

常见错误#3:不使用意图

Intents 是Android的关键组件之一吗. 它是在应用程序的不同部分之间传递数据的一种方式, even better, 系统上不同的应用程序.

Let’s say you have a gallery app that can share a download link to some images via SMS. 这两种选择哪一种更符合逻辑?

Option 1:

  • 请求SEND_SMS权限.

      
    
  • 编写自己的代码发送短信使用 SmsManager.
  • Explain to your users why your gallery app needs access to services that can cost money, 以及为什么他们必须授予使用你的应用的权限.

Option 2:

  • 启动一个SMS Intent,让一个专为SMS设计的应用程序来完成这项工作

      sendIntent = new Intent.ACTION_VIEW);
      sendIntent.setData(Uri.parse("sms:" + telephoneNumber));
      sendIntent.putExtra(“sms_body”,x);
      startActivity (sendIntent);
    

如果你有任何疑问,最好的解决方案是选项2!

这种方法几乎可以应用于任何事情. Sharing content, taking pictures, recording video, picking contacts, adding events, 打开原生应用的链接, etc.

除非有很好的理由进行自定义实现(例如.(一个应用滤镜的相机),对于这些场景总是使用intent. 它将为您节省大量的编程时间,并剥离 AndroidManifest.xml 不必要的权限.

常见错误4:不使用片段

不久前在Honeycomb中,Android引入了 fragments. Think of them as separate building blocks with their own (rather complex) life cycles that exist inside an Activity. 它们对各种屏幕的优化有很大帮助, 它们很容易被父活动管理, can be reused, 任意组合和定位.

Launching a separate activity for each app screen is terribly inefficient, 因为系统会尽可能长时间地将它们保存在内存中. 杀死一个不会释放其他动物使用的资源.

This Android programming tutorial recommends the proper use of fragments to make your app more efficient.

除非你想深入挖掘Android核心并阅读 this article, advocating against fragment usage, you should use fragments whenever possible. 它基本上是说碎片和 cursor loaders 有良好的预期目的,但执行不力.

常见错误#5:阻塞主线程

The main thread has a single purpose: keeping the user interface responsive.

Although the science behind measuring the frame rate our eyes/brain can perceive is complex and influenced by a lot of factors, a general rule is that anything below 24 fps with delay greater than 100 ms won’t be perceived as smooth.

这意味着用户的行动将会有延迟的反馈, 你编写的安卓应用程序就会停止响应. 剥夺用户对应用程序的控制权会导致挫败感, 受挫的用户往往会给出非常负面的反馈.

Even worse, 如果主线程被阻塞了一段时间(活动5秒), 10(广播接收机), ANR will happen.

As you learn Android programming, you will come to know and fear this message.  遵循这些Android编程技巧来尽量减少这种情况.

这在Android 2中很常见.X,在较新的版本上系统不允许您这样做 network calls in the main thread.

To avoid blocking the main thread, always use worker/background threads for: 1. network calls 2. bitmap loading 3. image processing 4. database querying 5. SD读/写

常见错误#6:重新发明轮子

"好的,我不使用主线程. I’ll write my own code that communicates with my server in a background thread.”

No! 请不要这样做! Network calls, image loading, database access, JSON parsing, 社交登录是应用中最常见的功能. 不只是你的,所有的应用都是. 有更好的办法. 记住Android是如何成熟和成长为一个平台的? 下面是一些简短的例子:

  1. Use gradle as a build system.
  2. Use Retrofit / Volley for network calls.
  3. Use Picasso for image loading.
  4. Use Gson / Jackson for JSON parsing.
  5. Use 共同实现 for social login.

If you need something implemented, chances are it’s already written, tested and used widely. 做一些基础研究,读一些 Android编程教程 在编写自己的代码之前!

常见错误7:不假设成功

Great. We have learned that there is a better way for handling long running tasks, 为此,我们正在使用文档完备的库. 但是用户仍然需要等待. It’s inevitable. 包裹不是立即发送、处理和接收的. There is a round trip delay, there are network failures, packages get lost, and dreams get destroyed.

但这一切都是可以衡量的. 成功的网络呼叫是 far more likely 而不是失败的人. 那么为什么要在处理成功的请求之前等待服务器响应呢? 假设成功,处理失败,这要好得多. So, 当用户点赞一篇文章时,点赞数会立即增加, 在不太可能的情况下,电话失败了, the user is notified.

在现代社会,人们期望得到即时反馈. 人们不喜欢等待. Kids don’t want to sit in a classroom obtaining knowledge that has uncertain future payoff. 应用程序必须适应用户的心理.

常见错误8:不理解位图

Users love content! 特别是当内容格式良好且看起来不错时. Images, for instance, 都是非常好的内容, 主要是由于他们的特性,传达千言万语的每一个图像. 它们还会消耗大量内存. A lot of memory!

Before an image is displayed on the screen, it has to be loaded into the memory. 因为位图是最常用的方法, we’re going to provide an Android programming guide for the whole process:

Let’s say you want to display an image on your screen that you just took with your camera. The total memory needed for this is calculated with the following formula: memory_needded_in_bytes = 4 * image_width * image_height;

Why 4? 那么,最常见/推荐的位图配置是 ARGB_8888. 这意味着对于我们绘制的每个像素, 我们需要为alpha保留8位(1字节), the red, 记忆中的贪婪和蓝色通道, 以便正确地显示它. 还有其他选择,比如 RGB_565 只需要一半内存的配置 ARGB_8888, but loses the transparency and the color precision (while maybe adding a green tint).

Let’s assume you have a brand new device with full HD screen and 12 MP camera. The picture you just took is 4000x3000 pixels large and the total memory needed to display it is: 4字节* 4000 * 3000 = 48mb

48mb的内存只能存储一张图像!? That’s a lot!

现在让我们考虑一下屏幕分辨率. You are trying to show a 4000x3000 image on a screen that has 1920x1080 pixels, in worst case scenario (displaying the image full screen) you shouldn’t allocate more than 4 * 1920 * 1080 = 8.3 MB of memory.

始终遵循Android编程提示 有效显示位图:

  1. 测量显示图像的视图.
  2. 相应地缩放/裁剪大图.
  3. 只显示可以显示的内容.

常见错误#9:使用深度视图层次结构

在Android中,布局有一个XML表示. 以便绘制内容, 需要解析XML, 屏幕需要测量, 所有的元素都需要相应地放置. 这是一个需要优化的资源和耗时的过程.

This is how the ListView (以及最近的 RecyclerView) works.

如果一个布局被膨胀过一次,系统就会重用它. 但是,在某些时候,膨胀布局是必须发生的.

假设你想用图像制作一个3x3的网格. 一种方法是用垂线 LinearLayout containing 3 LinearLayoutS的权值相等,每个都包含3 ImageViews with equal weight.

Some Android programming beginners don’t always make the best use of LinearLayouts.

用这种方法我们能得到什么? “嵌套权重对性能不利”的警告.

在Android编程界有一种说法,是我刚刚编出来的: “不费什么力气,所有的等级制度都可以被夷平。”.

In this case RelativeLayout or GridLayout 将有效地替换嵌套 LinearLayouts.

常见错误#10:未将minSdkVersion设置为14

好吧,这不是错误,但这是不好的做法.

Android 2.x was a huge milestone in developing this platform, but some things should be left behind. Supporting older devices adds more complexity for code maintenance and limits the development process.

The numbers are clear, the users have moved on, the developers shouldn’t stay behind.

I’m aware that this doesn’t apply for some big markets with old devices (ex. 印度),并设置 minSdkVersion to 14, on the Facebook App, means leaving couple of million users without their favorite social network. But, if you are starting fresh and trying to create a beautiful experience for your users, 是否考虑过消除过去. 没有资源的用户, 或者觉得需要升级他们的设备/操作系统, won’t have the incentive to try out a superior version of your Android app and ultimately spend money on it.

Wrap Up

Android是一个发展迅速的强大平台. 期望用户跟上步伐可能是不合理的, 但这对Android开发者来说至关重要. Getting Android basics right and avoiding Android errors is of utmost importance before taking on more challenging projects.

Knowing that Android is not just on our phones or tablets is even more important. It’s on our wrists, in our living rooms, in our kitchens, and in our automobiles. A good grasp of the ecosystem and more advanced Android tutorials will help you create apps for a variety of hardware platforms and use cases, 从顶级智能手机到基本的智能家居设备.

关于总博客的进一步阅读:

聘请Toptal这方面的专家.
Hire Now
Ivan Dimoski

Ivan Dimoski

Verified Expert in Engineering

Stockholm, Sweden

2013年12月11日成为会员

About the author

Ivan is an accomplished Android developer and consultant with six years of experience developing user-friendly applications.

authors are vetted experts in their fields and write on topics in which they have demonstrated experience. All of our content is peer reviewed and validated by Toptal experts in the same field.

Expertise

PREVIOUSLY AT

Truecaller

世界级的文章,每周发一次.

输入您的电子邮件,即表示您同意我们的 privacy policy.

世界级的文章,每周发一次.

输入您的电子邮件,即表示您同意我们的 privacy policy.

Toptal Developers

Join the Toptal® community.