Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 357 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
357
Dung lượng
6,35 MB
Nội dung
Books for professionals by professionals® Companion eBook Available ro JavaMEApps gives you, the developer, the know-how required for writing sophisticated JavaME applications and the tools for taking advantage of this huge potential market JavaME is the largest mobile software platform in the world, supported by over 80% of all phones ProJavaMEApps explains what JavaME is and how it compares to other mobile software platforms, how to properly design and structure JavaME applications, and how to think like an experienced JavaME developer Unlike other JavaME books out there, which only teach the basics of JavaME by way of a few simple examples, ProJavaMEApps presents a broader, eagle-eye picture of a complete JavaME application It guides you through the implementation of all the essential aspects of JavaME development, from the user interface to client-server communication, and reveals the decisions and reasoning behind the code You’ll learn: What makes mobile software different from desktop software How to design and implement your JavaME application How to solve the most common problems affecting JavaME development Proper JavaME programming techniques How to optimize your code How to develop commercial-quality Apps • • • • • • This book is for intermediate to advanced JavaME developers looking to learn proper JavaME development ProJavaMEApps P Building Commercial Quality JavaMEAppsProJavaMEApps Iliescu Companion eBook SOURCE CODE ONLINE www.apress.com Shelve in Mobile Computing / Programming Java User level: Intermediate–Advanced Ovidiu Iliescu For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them Contents at a Glance Contents v About the Author xi About the Technical Reviewer xii Acknowledgments xiii Introduction xiv ■Chapter 1: Getting Started 1 ■Chapter 2: A JavaME Framework 25 ■Chapter 3: Defining Our Data 51 ■Chapter 4: The Networking Module 63 ■Chapter 5: The Persistence Module 93 ■Chapter 6: The UI Module 115 ■Chapter 7: The Localization Module 155 ■Chapter 8: Putting It All Together 169 ■Chapter 9: Device Fragmentation 193 ■Chapter 10: Optimizing Your Code 223 ■Chapter 11: Adding Fine Touches and User Interaction Improvements 251 ■Chapter 12: JavaME Application Testing 271 ■Chapter 13: Advanced JavaME Graphics 283 ■Chapter 14: The Proper JavaME Mindset 305 ■Chapter 15: JavaME and the Future 325 ■Chapter 16: Final Words 337 Index 341 iv Introduction I find mobile phones to be amazing pieces of technology They allow you to be almost anywhere in the world and still be able to reach your friends, loved ones, colleagues or business partners with a press of a button If that wasn’t enough, they have grown so powerful that they are now nothing short of pocket computers Only a few decades ago the only place you could see a similar devices was a Star Trek movie – now they’re reality Modern mobile phones run on a variety of software platforms, but by far the most popular one in terms of reach and market share is JavaME This once feature-limited platform has grown and matured over the years along with the hardware supporting it; is now capable of delivering truly amazing applications that have the potential to reach hundreds of millions (if not billions) of people It is a bit disappointing, then, that the techniques developers use to write JavaME applications have not kept up with the times Many developers, especially less experienced ones, tend to approach JavaME development in the same extremely conservative way that it was approached almost a decade ago, when the platform was in its infancy Or, even worse, they treat JavaME like a desktop platform, with complete disregard to its nuances and to the fact that it is, in fact, a mobile platform designed to run on mobile devices As a consequence, many JavaME applications fall short of their potential The book you are reading right now aims to correct this by presenting how modern JavaME application development should be approached – complete with all the important tips and techniques applicable to the platform today What is covered in this book This book is somewhat different from other JavaME books, in that it is built around writing a complete and fully-functional JavaME application from scratch, using modern day development techniques To this end, everything from writing an application framework to writing a UI module (complete with widgets and touch support) will be discussed – no important application component will be left uncovered Furthermore, you will get to see how all of these fit into the context of a real-world application and how individual application components interact and work together Decisions made along the way will be explained, potential pitfalls will be highlighted and important advice stemming from hands-on experience will be given - all in an effort to create a comprehensive picture of what modern JavaME application development is all about Once the application is written, we will cover other aspects essential to JavaME development Code optimization, application testing, advanced graphics, improving the user’s experience and the proper JavaME developer’s mindset will all be discussed Finally, we will take a look at Java ME’s future in order to get a better understanding of where the platform is heading and a list of further research topics relevant to JavaME development will be given xiv ■ INTRODUCTIN What you will need for this book First of all, you should be familiar with JavaME development on at least an intermediate level This is not a beginner’s book – in fact, it is assumed that you have worked on at least one medium-sized JavaME application before Second, the target device for the source code presented in this book is the WTK emulator For best results, it is recommended that you have both versions 2.5.2 and 3.0 installed The WTK emulator has been chosen because of its ubiquitous nature and because it is pretty much the “reference implementation” for JavaME However, the code is designed to be as portable as possible; running it on other emulators and on real devices should work just fine (but cannot be guaranteed) It is also recommended that you run all the code presented and experiment with it JavaME is all about subtleties, many of which have to be seen in action in order to be grasped For obvious reasons, this is not possible with static code listings Let the fun begin JavaME development, for me, is always a fun endeavor While not always easy, it is almost never dull either; there’s always some new challenge to overcome and something new to learn from overcoming it With this in mind, I hope that you will enjoy reading this book and that the information contained within will help make your life as a JavaME developer easier, more fun and more interesting xv ■ INTRODUCTION xvi Chapter Getting Started Ten years ago, if you wanted to write software for mobile phones, you pretty much had only one option: JavaME (or, as it was known back then, J2ME) Things have changed in recent years, and a multitude of mobile platforms have appeared, each with its own strengths and weaknesses Despite these newcomers, JavaME is still a strong platform for mobile development The catch is that the quality bar is set pretty high these days, and in order to compete in the real world, your applications must be very competent from a technical standpoint This book aims to teach you how to achieve just that First, however, you must understand where JavaME stands today Java ME, Smartphones, and Feature Phones Some people think that JavaME has no place in a world where all the news is about iPhone and Android smartphones This is simply not true JavaME is still a great platform for mobile development Let’s look at why that is Java ME’s Advantages First, contrary to popular belief, JavaME is a viable option for smartphone software development For one thing, Symbian is the most widespread smartphone operating system in use today and it has full support for JavaME Blackberry devices also support JavaME And there are even ways to get JavaMEapps on Android and on the iPhone, using cross-platform conversion tools and emulators Thus, JavaME is supported in one form or another by most smartphones in use today—and it can make use of their superior capabilities Moreover, while smartphones tend to get all the hype these days because of their cool new capabilities, it’s worth noting that modern feature phones are starting to play in the same ballpark Features like touchscreens, 3D accelerators, sensors (accelerometers for example), geo-location support, multitasking, large amounts of RAM, fast CPUs, and high-resolution screens are gradually starting to become common in the feature phone world too, in which JavaME is the uncontested king CHAPTER 1: Getting Started Furthermore, feature phone sales are still considerably higher than smartphone sales For example, according to Wikipedia, feature phones have an 83% market share in the US as of 2010 This means that 83% of all US phones can’t run Android or iPhone apps, but they can run JavaME apps, which makes JavaME the best platform if you want to achieve high market penetration Finally, JavaME is designed to be simple and modular Everything but core features is defined and regulated by individual Java Specification Requests (JSRs): 3D support in JSR 184, PIM and file system support in JSR 75, sensor support in JSR 256, and so forth This means two things First, JavaME as a whole can evolve and improve while still maintaining backward compatibility You will always be able to leverage your existing know-how and software base, since there will be no groundbreaking and compatibility-breaking changes along the way, only optional and incremental improvements that don’t affect existing functionality Second, you can tailor your applications to provide the exact feature set needed for any given target device, based on the functionality that particular device supports In the end, you get a platform that is supported in one form or another on the vast majority of phones in use today and that, unlike rival platforms, can run on low-end feature phones too, not just on state-of-the-art smartphones Furthermore, the platform is flexible enough to allow you to use fancy features where available (such as 3D support and geo-location), so you’re not limited to just the core JavaME features So you see, JavaME is still an excellent candidate for developing your mobile applications Java ME’s Disadvantages Like all software platforms, JavaME has weaknesses as well as strengths In order to provide a fair assessment, let’s look at Java ME’s most notable disadvantages—and how these can be mitigated The main problem with JavaME is that its core API is somewhat outdated and low-level by today’s standards Fortunately, plenty of third-party libraries out there, from UI libraries to utility libraries, can help alleviate this This can even be considered an advantage, as you are free to choose whatever libraries and coding paradigms best suit your particular project, thus keeping your code base clean and to the point JavaME also suffers from device fragmentation (which is somewhat unavoidable considering the staggering number of devices that support Java ME) This was a huge problem in the early days; however, device support has become more standardized and tools have appeared to combat fragmentation Today, fragmentation is still a problem, but to a much lesser degree, and there are plenty of resources you can use to resolve it We will discuss fragmentation later in the book JavaME is also somewhat less tolerant than competing platforms Having to work, in general, with fewer resources than other modern-day platforms means that problems like memory leaks and inefficient algorithms are much more likely to cause stability and performance issues The best solution for this is to code defensively and to optimize your algorithms and your code—we’ll discuss both topics later CHAPTER 1: Getting Started In addition, JavaME simply lacks support for some features For example, system-wide notifications are unavailable, as is support for writing widgets and for low-level interactions with the hardware and with the operating system The upcoming MIDP 3.0 standard will alleviate some of these problems, but until then JavaME applications have to be, for the most part, stand-alone and self-contained, with only limited access to certain operating system and hardware features NOTE: There are ways to mix regular JavaME API calls with platform-specific API calls (this is frequently done in Blackberry applications), but the end result is not, strictly speaking, a JavaME application, so for bookkeeping purposes it doesn’t count Finally, JavaME generally lacks the integration of an application store within the underlying platform, something both Android and iPhone have going for them However, in recent years, JavaME application stores such as Nokia’s Ovi Store and GetJar have become mainstream and well-known, so getting your software to your customers isn’t a real problem anymore The Bottom Line First, the bad: JavaME is not a universal solution from a technical point of view I would not use it, for example, for writing an extremely sophisticated, state-of-the-art 3D game I probably wouldn’t use it if my application needed to target only the latest and greatest smartphones, as native applications are better suited for this Finally, even if I wanted to, I couldn’t use it for applications that require intimate interactions with the operating system or for writing widgets That being said, JavaME is well-suited for pretty much everything else, from business applications and regular games (even 3D ones) to applications that by their very nature require high market penetration, such as social networking clients and supporting applications for web-based services (for file transfer, web mail, etc.) When you add in business factors, JavaME becomes a really strong contender: it’s hard to argue against the world’s most popular mobile platform, with literally billions of devices out there supporting it Overall, JavaME is still quite a robust platform Though it has undergone changes and repositioning throughout the years, it remains a useful vehicle for building mobile applications Creating a JavaME Application The best way to learn how to create a high-quality JavaME application is to actually create one, so the rest of this book will be dedicated to this endeavor We’ll start with the basics, such as the project’s idea and goals, and progress toward a fully functional JavaME application CHAPTER 1: Getting Started As this is a technical book, we will focus primarily on technical aspects—everything else will be discussed only marginally NOTE: Modern JavaMEapps typically have thousands or tens of thousands of lines of code, but we will include in the book only the minimum amount of code necessary to illustrate the subject at hand The full source code is available on the Apress web site The Idea for the Application Every project starts with an idea Since this book is a learning tool, our project should be something simple, fun, rewarding, and easy to understand, but at the same time it should be substantive enough to have the potential of becoming a ProJavaME application Beginner programming books typically start with a “Hello, world” example, but we need something a little more complex So for our project, we’ll write a JavaME Twitter client This makes sense because Its complexity is just right: A Twitter client is sufficiently complex to present a challenge and provide enough learning material for our purposes, yet simple enough to be presented and discussed within a reasonable number of pages It is straightforward: Twitter provides a clean and simple API for accessing its services Documentation and code examples are also readily available on the Internet It is fun: The interactive nature of Twitter makes this a fun project As we write the code, you will be able to easily see it in action at every step along the way, and show it to your friends and family for fame and feedback It teaches a lot of stuff: To write a Twitter client, we will deal with everything from creating a UI to writing a networking layer and using persistent storage to save application settings It provides a good starting point for other applications: Once the Twitter client is ready, you can modify it to support other Twitter-like services or even instant messaging protocols Goals, Features, Revenue Sources, and Target Devices After settling on a particular idea, the tech and business teams should decide together what the project’s goals and desired feature set are The keyword here is “together”; compared to other more powerful mobile platforms, it is much more important for JavaME projects that the tech team have a say ... Pro Java ME Apps Are Built Around a Custom Framework Pro Java ME applications are invariably built around a custom framework This framework is usually developed in-house to fit the specific project... reasons Pro Java ME Apps Reuse Code Code reuse has always been a touchy subject for Java ME, because of the widely different environments and circumstances Java ME applications have to run on This means... developers use to write Java ME applications have not kept up with the times Many developers, especially less experienced ones, tend to approach Java ME development in the same extremely conservative