PowerPoint 3

Download Report

Transcript PowerPoint 3

Advanced Java Application
Development for the
BlackBerry Smartphone
Trainer name
Date
© 2009 Research In Motion Limited
v0.01
Agenda
In this course, you will cover the following topics:
– Introduction to advanced mobile application
development for the BlackBerry® smartphone
– Application control for mobile devices
– Optimization for mobile application development
– Advanced BlackBerry user interface
– Introduction to multimedia application development
for the BlackBerry smartphone
– Understanding push technology
UI = user interface
<Course code>
© 2009 Research In Motion Limited
Agenda
In this course, you will cover the following topics:
– Understanding client/server push
– Introduction to developing Bluetooth® applications
for mobile devices
– Developing secure applications
<Course code>
© 2009 Research In Motion Limited
Advanced Java Application Development for the BlackBerry
Smartphone
Objectives:
– Describe optimization techniques for BlackBerry
applications
– Create BlackBerry applications that demonstrate
the use of optimization techniques
– Demonstrate how to optimize BlackBerry
applications using the BlackBerry IDE profiler tool
– Identify a memory leak using the Object Viewer
Optimization for mobile application
development
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
– Inefficient and ineffective code can result in slow
and costly applications.
– Use efficient coding techniques to create efficient
code.
– Reuse or reduce code where possible
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Using classes effectively
– Use or extend a class from the Java® platform
– Mark classes as final when possible
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Using methods
– Synchronize methods rather than code blocks
– Write high traffic methods as final
– Override methods with simplified, optimized
versions
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Using local variables
– Local members are more efficient
Writing efficient loops
– Always factor loop invariant code out of loops
Optimizing subexpressions
– Use a local variable instead of using the same
expression twice
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Optimizing division operations
– Division operations are slow because the processor
does not have a hardware divide instruction
Using shorthand for evaluating Boolean conditions
– Use efficient shorthand for evaluating boolean
conditions
Using int instead of long
– Operations run two to four times faster using int
instead of long
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Managing garbage collection
– Avoid calling system.gc to perform garbage
collection
– Garbage collection happens automatically
Using static variables for Strings
– Define static fields of type String to increase
program speed by using static variables (not final)
instead of constants (final)
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Avoiding the String(String) constructor
– Create a String without using the
java.lang.String(String) constructor.
Avoid java.util.enumeration
– Avoid using java.util.enumeration unless
trying to hide data
– Iterate over elements to avoid unnecessary
garbage
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Using try/catch exception handling
– Try/catch exception handling does not introduce
expensive overhead like other Java applications
Using the instanceof operator
– Use instanceof instead of catching a
ClassCastException to more efficiently evaluate
whether a cast succeeds
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Evaluating conditions using instanceof
– Evaluate a condition using instanceof (but do
not evaluate explicitly whether the variable is null)
Using longs for unique identifiers
– Ensures they remain unique across all third-party
applications
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Increasing speed
– Inline code in critical sections
– Avoid recursive calls
– Avoid multiple transactions
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Using threads
– Create new threads for lengthy operations
– Use background threads
Reusing objects
– Reuse objects wherever possible
– Avoid using immutable strings
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Using images effectively
– Use the most compressed form possible
Exiting applications correctly
– Before you invoke System.exit(int status),
remove unused objects from the runtime store
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Printing the stack trace
– Catch a Throwable instance to view the stack trace
when you debug your application
Using class and instance variable defaults
– Use the default values of instance variables within a
class when possible
Using loop induction variables
– Try to do your own induction variable optimizations
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Using objects judiciously
– Consider the following questions:
• Are all of the objects necessary?
• Can your application store any objects that represent
primitives as primitives instead of as objects?
• Are all of the persisted objects necessary?
• Do any instances of Vector and Hashtable exist? Are
these instances necessary?
• How many objects does your application create and then
throw away?
<Course code>
© 2009 Research In Motion Limited
Optimal BlackBerry coding techniques
Reducing the size of compiled code
– Set appropriate access
– Avoid creating interfaces
– Use static strings
– Import individual classes
<Course code>
© 2009 Research In Motion Limited
Using the BlackBerry IDE profiler tool
<Course code>
© 2009 Research In Motion Limited
Using the BlackBerry IDE profiler tool
– The BlackBerry® Java® Plug-in for Eclipse
comprises a series of development tools:
®
• Java ME compiler
• Editor
• Debugger
• BlackBerry IDE profiler
• BlackBerry MDS simulator
<Course code>
© 2009 Research In Motion Limited
Using the BlackBerry IDE profiler tool
– The BlackBerry IDE profiler tool shows the
percentage of time spent in each code area
– Use it to isolate and address bottlenecks
– Three views:
• Summary
• Methods
• Source
<Course code>
© 2009 Research In Motion Limited
Using the BlackBerry IDE profiler tool
Optimizing code using the BlackBerry IDE profiler tool
– To optimize code using the BlackBerry IDE profiler
tool do the following:
• Generate the profile data
• Set the profile options
• View the profile data
• Save the contents to a file
<Course code>
© 2009 Research In Motion Limited
Identifying memory leaks
<Course code>
© 2009 Research In Motion Limited
Identifying memory leaks
– Garbage collection locates and deletes objects no
longer needed by the application
– All nodes no longer referenced are deleted, freeing
the associated memory
– A memory leak can occur if a high-persistence
class fails to clear references to a limited
persistence class
<Course code>
© 2009 Research In Motion Limited
Identifying memory leaks
– Common places to find memory leaks:
• Vectors
• List and Hashtables
• A class registered as an event listener and never
unregistered
<Course code>
© 2009 Research In Motion Limited
Identifying memory leaks
– Memory Statistics tool and Object tool can display
statistics on:
• Number of objects and bytes used for object
handles
• RAM and flash memory
RAM = random access memory
<Course code>
© 2009 Research In Motion Limited
Identifying memory leaks
Finding object leaks
– Memory leaks are most common in the following:
• Data structure
• Local variables
• Runtime store
• Listeners
<Course code>
© 2009 Research In Motion Limited
Identifying memory leaks
Finding object leaks
– Symptoms of memory leaks:
• Hourglass appears often
• Email messages are automatically deleted
• A low Free File Number
<Course code>
© 2009 Research In Motion Limited
Identifying memory leaks
Viewing statistics to locate object leaks
– The Memory Statistics tool displays information on
memory usage and the number of objects in
memory
– The Object tool displays information on each object
<Course code>
© 2009 Research In Motion Limited
Summary
– To ensure that your BlackBerry smartphone applications
run effectively for the user and efficiently for the device,
use optimal coding techniques.
– Use tools such as the BlackBerry IDE profiler tool to
help you optimize your code.
– Watch for memory and object leaks. Use the Memory
Statistics and Object tools to locate and identify the
sources of memory leaks.
<Course code>
© 2009 Research In Motion Limited
<Course code>
© 2009 Research In Motion Limited
Legal Disclaimer
© 2009 Research In Motion Limited. All rights reserved. BlackBerry®, RIM®, Research
In Motion®, SurePress™ SureType® and related trademarks, names and logos are the
property of Research In Motion Limited and are registered and/or used in the U.S. and
countries around the world. All other trademarks are the property of their respective
owners. This documentation is provided "AS IS" and without condition, endorsement,
guarantee, representation or warranty, or liability of any kind by Research In Motion
Limited and its affiliated companies, all of which are expressly disclaimed to the
maximum extent permitted by applicable law in your jurisdiction.
<Course code>
© 2009 Research In Motion Limited