From ba944ccda3d61ccbe6061277fcc50c96fe2643f1 Mon Sep 17 00:00:00 2001
From: Dirk Dougherty It's possible to write code that wins every performance test in the world, but still sends users in a fiery rage when they try to use the application. These are the applications that aren't responsive enough — the ones that feel
-sluggish, hang or freeze for significant periods, or take too long to process
-input. In Android, the system guards against applications that are insufficiently responsive for a period of time by displaying a dialog to the user, called the Application Not Responding (ANR) dialog. The user can choose to let the application continue, but the user won't appreciate having to act on this dialog every time he or she uses your application. So it's important to design responsiveness into your application, so that the system never has cause to display an ANR to the user. Figure 1. An ANR dialog displayed to the user. Generally, the system displays an ANR if an application cannot respond to user input. For example, if an application blocks on some I/O operation (frequently a network access), then the main application thread won't be able to process incoming user input events. After a time, the system concludes that the application has hung, and displays the ANR to give the user the option to kill it.
+ It's possible to write code that wins every performance test in the world,
+but still sends users in a fiery rage when they try to use the application.
+These are the applications that aren't responsive enough — the
+ones that feel sluggish, hang or freeze for significant periods, or take too
+long to process input. In Android, the system guards against applications that are insufficiently
+responsive for a period of time by displaying a dialog to the user, called the
+Application Not Responding (ANR) dialog, shown at right in Figure 1. The user
+can choose to let the application continue, but the user won't appreciate having
+to act on this dialog every time he or she uses your application. It's critical
+to design responsiveness into your application, so that the system never has
+cause to display an ANR dialog to the user. Generally, the system displays an ANR if an application cannot respond to
+user input. For example, if an application blocks on some I/O operation
+(frequently a network access), then the main application thread won't be able to
+process incoming user input events. After a time, the system concludes that the
+application is frozen, and displays the ANR to give the user the option to kill
+it. Similarly, if your application spends too much time building an elaborate in-memory
structure, or perhaps computing the next move in a game, the system will
@@ -15,31 +33,17 @@ conclude that your application has hung. It's always important to make
sure these computations are efficient using the techniques above, but even the
most efficient code still takes time to run. In both of these cases, the fix is usually to create a child thread, and do
+ In both of these cases, the recommended approach is to create a child thread and do
most of your work there. This keeps the main thread (which drives the user
-interface event loop) running, and prevents the system from concluding your code
+interface event loop) running and prevents the system from concluding that your code
has frozen. Since such threading usually is accomplished at the class
level, you can think of responsiveness as a class problem. (Compare
this with basic performance, which was described above as a method-level
concern.) An ANR dialog displayed to the user. This document discusses how the Android system determines whether an application is
-not responding and provides guidelines for
-ensuring that your application is responsive. This document covers these topics: This document describes how the Android system determines whether an
+application is not responding and provides guidelines for ensuring that your
+application stays responsive. What Triggers ANR?
@@ -48,8 +52,10 @@ and Window Manager system services. Android will display the ANR dialog
for a particular application when it detects one of the following
conditions:
Android 1.5 and earlier versions of the platform were designed to support a -single screen configuration — HVGA (320x480) resolution on a 3.2" screen. -Because the platform targeted just one screen, application developers could -write their applications specifically for that screen, without needing to worry -about how their applications would be displayed on other screens.
- -Starting from Android 1.6, the platform adds support for multiple screen +
Starting from Android 1.6, the platform provides support for multiple screen sizes and resolutions, reflecting the many new types and sizes of devices on -which the platform will run. This means that developers must design their -applications for proper display on a range of devices and screens.
+which the platform runs. If you are developing an application that will run +on Android 1.6 or later, you can use the compatibility features of the Android +platform to ensure that your application UI renders properly across the range of +supported screen sizes and resolutions. -To simplify the way application developers design their user interfaces for -multiple devices, and to allow more devices to participate without impacting +
To simplify the way that developers design their user interfaces for +multiple devices and to allow more devices to participate without affecting applications, the platform divides the range of actual supported screen sizes and resolutions into:
Applications can provide custom resources (primarily layouts) for any of the -three generalized sizes, if needed, and they can also provide resources -(primarily drawables such as images) for any of the three generalized densities. -Applications do not need to work with the actual physical size or density of the -device screen. At run time, the platform handles the loading of the correct size -or density resources, based on the generalized size or density of the current -device screen, and adapts them to the actual pixel map of the screen.
- -The table below lists some of the more common screens supported -by Android and illustrates how the platform maps them to generalized screen -configurations. Some devices use screens that are not specifically listed -in the table — the platform maps those to the same set generalized -screen configurations.
- - - -+ |
|
@@ -196,10 +218,7 @@ device screens supported by Android.
Small screen | -
-
| QVGA (240x320) | @@ -208,74 +227,29 @@ device screens supported by Android. | Normal screen | -
-
|
-
-
|
-
-
|
+ WQVGA400 (240x400) WQVGA432 (240x432) |
+ HVGA (320x480) | +WVGA800 (480x800) WVGA854 (480x854) |
||
Large screen | - |
-
|
+ WVGA800* (480x800) WVGA854* (480x854) |
As shown above, the various screen configurations are arranged around a -baseline screen that is assigned a size of "normal" and a density of -"medium". The HVGA screen is used as the baseline because all applications -written against Android 1.5 or earlier are (by definition) written for the HVGA -screen used on the T-Mobile G1 and similar devices.
- - - -Although the platform currently supports the nine possible size-density -configurations listed in the table, you do not necessarily need to create custom -resources for each one of them. The platform provides robust compatibility -features, described in the sections below, that can handle most of the work of -rendering your application on the current device screen, provided that the UI is -properly implemented. For more information, see Best Practices for Screen Independence.
- - +low (ldpi) density screens in Android-powered devices available now, see the Screen Sizes and Densities dashboard. +large
, normal
, and
small
, and those for density-specific resources are
hdpi
(high), mdpi
(medium), and ldpi
-(low). The qualifiers correspond to the generalized densities given in
-Table 1, above.
+(low). The qualifiers correspond to the generalized densities described in
+Range of screens supported, above.
<supports-screens>
@@ -457,7 +431,7 @@ different.
smallScreens
, normalScreens
, and
largeScreens
attributes correspond to the generalized screen sizes
-shown in Table 1, earlier in this document.
+described in Range of screens supported, earlier in this
+document.
Description | +
+ Default value, |
+
+ Default value, |
|
---|---|---|---|
@@ -497,10 +478,10 @@ shown in Table 1, earlier in this document. |
Whether or not the application UI is designed for use on
small screens — "true " if it is, and
-"false " if not. See Default values for
-attributes for information about the assumed value of this attribute, if not
-declared.
+"false " if not.
|
+"false " |
+"true " |
@@ -509,8 +490,10 @@ declared. |
Whether or not the application UI is designed for use on
normal screens — "true " if it is, and
-"false " if not. The default value is "true ".
+"false " if not. The default value is always "true ".
|
+"true " |
+"true " |
@@ -519,10 +502,10 @@ declared. |
Whether or not the application UI is designed for use on
large screens — "true " if it is, and
-"false " if not. See Default values for
-attributes for information about the assumed value of this attribute, if not
-declared.
+"false " if not.
|
+"false " |
+"true " |
@@ -535,9 +518,13 @@ in different density environments — "true " if so, and
px ) and math — and
+relies on the application to use density-independent pixel units
+(dp ) and/or math to manage the adaptation of pixel values according
+to density of the current screen. That is, as long as your application uses
+density-independent units (dp) for screen layout sizes, then it will perform
+properly on different densities when this attribute is set to
+"true ".
See Default values for attributes for -information about the assumed value of this attribute, if not declared. +Note that the setting of this attribute affects density-compatibility only. +It does not affect size-compatibility features such as display on a virtual +baseline screen. |
+"false " |
+"true " |
In general, when you declare a screen-size attribute
(smallScreens
, normalScreens
, or
-largeScreens
) as "true", you are signaling to the platform that
-your application wants to manage its UI by itself, for all screen sizes, without
-the platform applying any size-compatibility behaviors (such as a virtual HVGA
-display area). If you declare a screen-size attribute as "false", you are
-signaling that your application is not designed for that screen size. The
-effects are conditioned by the screen size that your application does not
-support:
largeScreens
) as "true
", you are signaling to the
+platform that your application is designed to render properly on that screen
+size. As a result, the platform does not apply any size-compatibility features
+(such as a virtual HVGA display area). If you declare a screen-size attribute as
+"false
", you are signaling that your application is not
+designed for that screen size. In this case, the platform does apply
+size-compatibility features, rendering the application in an HVGA baseline
+display area. If the current screen is larger than normal size, the
+platform renders the application in a virtual HVGA screen on the larger screen.
+See Screen-Compatibility Examples for an
+illustration of what an application looks like when displayed in a virtual HVGA
+screen.
+
+In other words, setting a <supports-screens>
attribute to
+"false
" tells the platform to enable it's compatibility features
+when displaying the application on a screen of that size or any larger
+size, if also disallowed. Otherwise, the platform gives the application a
+normal display area that can use the full device screen area, if
+appropriate.
Android Market also makes use of the <supports-screens>
+attributes. It uses them to filter the application from devices whose screens
+are not supported by the application. Specifically, Android Market considers an
+application compatible with a device if the application supports a screen that
+is the same or smaller than the current device screen. Android Market filters
+the application if it disallows the device's screen size and does not support a
+smaller size. In general, Android does not provide downward size-compatibility
+features for applications.
Here are some examples:
+largeScreens="false"
, your application can
-still be installed by users of devices with large screens. When run on a device
-with a large screen, this attribute value causes the platform to run the
-application in compatibility mode, rendering it in a baseline screen area
-(normal size, medium density) reserved on the larger screen. See
-Screen-Compatibility Examples for an
-illustration of what an application looks like when displayed in compatibility
-mode.smallScreens="false"
, your application can
-still be installed by users of devices with small screens. However, this
-attribute value causes Android Market to filter your application from the list
-of applications available to such users. In effect, this prevents users from
-installing the application on small-screen devices. smallScreens="false" normalScreens="true"
+largeScreens="false"
in your application's manifest. Although the +application is not designed for display on large screens, the platform can still +run it successfully in size-compatibility +mode. Android Market does not filter the application from devices +normal and large size screens, but does filter it from +small size screens, since the application provides no screen support at +small size (and there is no smaller size).
smallScreens="false" normalScreens="false"
+largeScreens="true"
in your application's manifest. Android Market +filters the application from users of devices with small and +normal size screens. In effect, this prevents such users from +installing the application.
If you declare the android:anyDensity
attribute as "true", you
-are signaling to the platform that your application wants to manage its UI by
-itself, for all screen densities, using the actual screen dimensions and pixels.
-In this case, the application must ensure that it declares its UI dimensions
-using density-independent pixels and scales any actual pixel values or math by
-the scaling factor available from
-{@link android.util.DisplayMetrics#density android.util.DisplayMetrics.density}.
If you declare the android:anyDensity
attribute as
+"true
", you are signaling to the platform that your application is
+designed to display properly on any screen density. In this case, the
+application must ensure that it declares its UI dimensions using
+density-independent pixels (dp
) and scales any absolute pixel
+values (px
) or math by the scaling factor available from {@link
+android.util.DisplayMetrics#density android.util.DisplayMetrics.density}. See Converting from dips to pixels for an example.
Note that the setting of the android:anyDensity
attribute does
not affect the platform's pre-scaling of drawable resources, such as bitmaps and
@@ -594,22 +609,22 @@ nine-patch images, which always takes place by default.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> - + ... <supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" - android:resizable="true" android:anyDensity="true" /> - </manifest> + ... +</manifest>- +
The default values for the <supports-screens>
attributes
-differs, depending on the the value of the
+differ, depending on the the value of the
android:minSdkVersion
attribute in the application's manifest, as well as on
the value of android:targetSdkVersion
, if declared:
android:targetSdkVersion
, if declared:
android:minSdkVersion
or
-android:targetSdkVersion
is "3" (Android 1.5) or lower, the default
-value for everything except android:normalScreens is false
. If you
-are primarily targeting pre-Android 1.6 platforms but also want to support other
-densities/screen sizes, you need to set the appropriate attributes to
-true
.
+android:targetSdkVersion
is "4" (Android 1.6) or higher, the
+default value for everything is "true
". If your application uses
+APIs introduced in Android 1.6 or higher, but does not support specific screen
+densities and/or screen sizes, you need to explicitly set the appropriate
+attributes to "false
".
android:minSdkVersion
or
-android:targetSdkVersion
is "4" (Android 1.6) or higher, the
-default value for everything is true
. If your application
-requires Android 1.6 features,
-but does not support these densities and/or screen sizes, you need to set the
-appropriate attributes to false
.
+ If android:minSdkVersion
is declared with a value of "3"
+(Android 1.5) or lower and a android:targetSdkVersion
+attribute is not declared with a value of "4" or higher, the default
+value for all attributes except android:normalScreens
is
+"false
". If you are primarily targeting pre-Android 1.6 platforms
+but also want to support other densities/screen sizes, you need to explicitly
+set the appropriate attributes to "true
".
android:normalScreens
always defaults to
@@ -646,8 +662,8 @@ appropriate attributes to false
.
of resources based on the characteristics of the screen on which your application
is running. You can use these qualifiers to provide size- and density-specific
resources in your application. For more information about the generalized sizes
-and densities that correspond to the qualifiers, see Table
-1, earlier in this document.
+and densities that correspond to the qualifiers, see Range
+of Screens Supported, earlier in this document.