background preloader

Multithreading For Performance

Multithreading For Performance
[This post is by Gilles Debunne, an engineer in the Android group who loves to get multitasked. — Tim Bray] A good practice in creating responsive applications is to make sure your main UI thread does the minimum amount of work. Any potentially long task that may hang your application should be handled in a different thread. Typical examples of such tasks are network operations, which involve unpredictable delays. Users will tolerate some pauses, especially if you provide feedback that something is in progress, but a frozen application gives them no clue. In this article, we will create a simple image downloader that illustrates this pattern. An Image downloader Downloading an image from the web is fairly simple, using the HTTP-related classes provided by the framework. static Bitmap downloadBitmap(String url) { final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); final HttpGet getRequest = new HttpGet(url); A client and an HTTP request are created. Demo Future work Related:  performance

Customizing Android ListView Items with Custom ArrayAdapter Tutorial in Category Android at EzzyLearning.com how to add onItemClick event based on Custom array adapter above Posted by kadek iwan on 27 Aug 2011 You can use listView1.setOnItemClickListener event. See my following tutorial for an example. Posted by Waqas Anwar on 27 Aug 2011 Marvelous work... keep it up.. Posted by ajay on 31 Aug 2011 Your solution assumes 5 entries cloudy, showers, stormy etc... Posted by Mark Manickaraj on 20 Sep 2011 You can fill weather data variable from the data coming from any of your method or service method.Weather weather_data[] = youservice.GetWeatherData(); Posted by Waqas Anwar on 21 Sep 2011 When you add enough items to scroll the scroll is laggy. Posted by Mark Manickaraj on 22 Sep 2011 Hey thanks. Posted by ruchi on 20 Nov 2011 hie , there seem a problem with ur code ..works slow and list view takes time to load while scrolling ..can u help ?? Posted by gaurav on 21 Nov 2011 great!!! Thanks!!!!

Slides et vidéo "Android rendu et performance" par Romain Guy le 17 avril 2012 – juin 21, 2012Classé dans : Compte-rendu De passage à Paris pour Devoxx France, Romain Guy, ingénieur Android chez Google spécialisé sur l’application Home et l’UI Toolkit, est intervenu le 17 avril 2012 au Google Master Class organisé par Telecom Paritech Entrepreneurs. Il a parlé de Rendu et de Performance sur Android. Nous vous proposons quelques notes, les slides et une vidéo de sa présentation: Pas de traitement dans UI threadView.post() RunnableStrict mode: pour afficher logs, messsages erreur, afficher flash à l’écran. A partir de Android Gingerbread.AsyncTask: manière simple avec pool de thread de communiquer les résultats dans l’UI threadAsyntask sont serialisés à partir de Android 3.0 et ne sont plus parallélisés. (A noter qu’il a fait la même présentation en interne.) Vidéo: Pour aller plus loin

Android chart.png - Wikipedia, the free encyclopedia java: how can i set a class's field by a field name that stored in a variable StrictMode StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them. StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users. Note that even though an Android device's disk is often on flash memory, many devices run a filesystem on top of that memory with very limited concurrency. If you find violations that you feel are problematic, there are a variety of tools to help solve them: threads, Handler, AsyncTask, IntentService, etc. StrictMode is not a security mechanism and is not guaranteed to find all disk or network accesses.

File Class Overview An "abstract" representation of a file system entity identified by a pathname. The pathname may be absolute (relative to the root directory of the file system) or relative to the current directory in which the program is running. The actual file referenced by a File may or may not exist. This class provides limited functionality for getting/setting file permissions, file type, and last modified time. On Android strings are converted to UTF-8 byte sequences when sending filenames to the operating system, and byte sequences returned by the operating system (from the various list methods) are converted to strings by decoding them as UTF-8 byte sequences. Summary Fields public static final String pathSeparator The system-dependent string used to separate components in search paths (":"). public static final char pathSeparatorChar The system-dependent character used to separate components in search paths (':'). This field is initialized from the system property "path.separator".

Rich-Style Formatting of an Android TextView - ChrisUmbel.com Even a developer-friendly mobile platform like Android can have a developer feeling a little lost when trying to perform simple tasks when you're unfamiliar with the platform. One of these simple, however poorly documented, tasks is rich-style text formatting within a TextView. SpannableString In the end this isn't limited to formatting. Example Essentially this code will set a TextView's text to the familiar, "Lorem ipsum dolor sit amet" and perform the following formatting: Make "Lorem" red Make "ipsum" a 1.5 times bigger than what the TextView's setting Make "dolor" display a toast message when touched Strike through "sit" Make "amet" twice as big as the TextView's setting, green and a link to this site The results of which will look something like: Note that we set the TextView's movement method to a LinkMovementMethod instance. Next Steps This covers the fundamental concepts, but there are many extensions of CharacterStyle I haven't covered here. Sat Aug 28 2010 18:25:02 GMT+0000 (UTC)

Android 3.0 Hardware Acceleration [This post is by Romain Guy, who likes things on your screen to move fast. —Tim Bray] One of the biggest changes we made to Android for Honeycomb is the addition of a new rendering pipeline so that applications can benefit from hardware accelerated 2D graphics. In this article, I will show you how to enable the hardware accelerated 2D graphics pipeline in your application and give you a few tips on how to use it properly. Go faster To enable the hardware accelerated 2D graphics, open your AndroidManifest.xml file and add the following attribute to the <application /> tag: android:hardwareAccelerated="true" If your application uses only standard widgets and drawables, this should be all you need to do. If you have custom drawing code you might need to do a bit more, which is in part why hardware acceleration is not enabled by default. Controlling hardware acceleration Because of the characteristics of the new rendering pipeline, you might run into issues with your application. Dos and don'ts

FileOutputStream Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources. Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue. Unlike constructors, finalizers are not automatically chained. Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread.

Android – Simple XML DOM Parser Example for Reading Response From InputStream | Vikas Patel's Blog Hi, I am trying to use simple XML DOM Parser for my HTTP response. Basically I am calling .Net webservice and I need to parse response. Let me also cover calling the .Net webservice. Webservice can be called/consumed by may way. For Example: Use SOAP call, HTTP Get method, HTTP Post method and many more.. I have one Web service method with Post call structure: POST /api.asmx/validateCredentials HTTP/1.1 Host: api.mywebsite.com Content-Type: application/x-www-form-urlencoded Content-Length: length username=string&password=string Now I want to call it from Android HTTP Post method. I’ll do it like: Now First of all I wanted my response as String. So I use Buffer reader to read the data from response object. Now I can see the response in LogCat. <? Here you can see some tags and actually we need those values!! So I can now use this “String” object to parse the XML. To parse the XML we have several methods like SAX parser, DOM parser XML pool parser etc. Like this: Like Loading...

Smoothing performance on Fragment transitions Suppose you're doing a pretty standard Fragment replacement with a custom animation: getSupportFragmentManager() .beginTransaction() .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out) .replace(android.R.id.content, new MyFragment()) .commit(); You may notice that the performance can be a bit rough, not as smooth as you'd like. public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { Animation animation = super.onCreateAnimation(transit, enter, nextAnim); // HW layer support only exists on API 11+ if (Build.VERSION.SDK_INT >= 11) { if (animation == null && nextAnim ! Now the animation should be a lot more smooth! * If you're not using the support library, then you'll be overriding Fragment.onCreateAnimator() and using animator-based classes.

BitmapDrawable Class Overview A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object. It can be defined in an XML file with the <bitmap> element. For more information, see the guide to Drawable Resources. Also see the Bitmap class, which handles the management and transformation of raw bitmap graphics, and should be used when drawing to a Canvas. Summary XML Attributes android:antialias Enables or disables antialiasing. Must be a boolean value, either "true" or "false". This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "? This corresponds to the global attribute resource symbol antialias. android:dither Enables or disables dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance: a ARGB 8888 bitmap with an RGB 565 screen). android:filter android:gravity android:mipMap

Android Button background image pressed/highlighted and disabled states without using multiple images | Shiki In Android, if you provide custom background images for buttons, you will lose the pressed and disabled image effects. The common way to fix that is to provide additional images for those states. I’m lazy and I find this inconvenient especially during the prototyping phase of app development. To use this, just replace your original button declarations like this: To this: Here’s a sample output using this custom button: The code is also available on GitHub. TimingLogger Class Overview A utility class to help log timings splits throughout a method call. Typical usage is: TimingLogger timings = new TimingLogger(TAG, "methodA"); // ... do some work A ... timings.addSplit("work A"); // ... do some work B ... timings.addSplit("work B"); // ... do some work C ... timings.addSplit("work C"); timings.dumpToLog(); The dumpToLog call would add the following to the log: D/TAG ( 3459): methodA: begin D/TAG ( 3459): methodA: 9 ms, work A D/TAG ( 3459): methodA: 1 ms, work B D/TAG ( 3459): methodA: 6 ms, work C D/TAG ( 3459): methodA: end, 16 ms Summary Public Constructors public TimingLogger (String tag, String label) Create and initialize a TimingLogger object that will log using the specific tag. Parameters Public Methods public void addSplit (String splitLabel) Add a split for the current time, labeled with splitLabel. public void dumpToLog () Dumps the timings to the log using Log.d(). public void reset (String tag, String label) public void reset ()

Related: