Pages

Friday, 15 March 2013

Android Cloud To Device Messaging

[This post is by Wei Huang, who helped implement this feature. — Tim Bray]

In the just-launched Android 2.2, we’ve added a new service to help developers send data from servers to their applications on Android phones. Android Cloud to Device Messaging (C2DM) makes it easier for mobile applications to sync data with servers.

Most of the useful applications on your mobile phone use the Internet to keep users connected. Traditionally, many apps use polling to fetch data periodically. POP mail clients, for example, connect to the server every 15 minutes or so. Polling is fairly easy to implement, and works well in many situations. It’s tricky, though, to select the frequency: Poll too often, you may not see any new data, and create unnecessary stress on the server and network. Poll too rarely and the data on the device may become stale. Polling is especially problematic on mobile devices, because it consumes precious network bandwidth and battery life.

Having the server push messages to the client asynchronously may be a superior choice for getting the latest data to your applications, resulting in fresher data and more efficient use of the network and your battery. However, it’s also tricky to implement a good push solution, and it isn’t free as there is some overhead in maintaining the required connection. On a mobile device like an Android phone, implementing applications to receive these messages is tricky; you have to worry about patchy network coverage and zombie connections created when the wireless carrier’s routers time out connections that are idle for too long.

Many of the Google applications on Android already use push to keep their data fresh, for example Gmail, Contacts, and Calendar. Starting with Android 2.2, C2DM allows third-party developers to use the same service the Google apps do.

Here are a few basic things to know about C2DM:

  • It requires Android 2.2; C2DM uses Google services which are present on any device running the Android Market.

  • It uses existing connections for Google services. This requires the users to sign into their Google account on Android.

  • It allows 3rd party servers to send lightweight data messages to their apps. The C2DM service is not designed for pushing a lot of user content; rather it should be used like a “tickle”, to tell the app that there is new data on the server, so the app can fetch it.

  • An application doesn’t need to be running to receive data messages. The system will wake up the app via an Intent broadcast when the the data message arrives, so long as the app is set up with the proper Intent Receiver and permissions.

  • No user interface is required for receiving the data messages. The app can post a notification (or display other UI) if it desires.

It’s easy to use the C2DM API. Here is how it works:

  • To enable C2DM, an application on the device registers with Google and get a registration ID, and sends the ID to its server.

  • When the server needs to push a message to the app on the device, it posts the message via HTTP to Google’s C2DM servers.

  • The C2DM servers route the message to the device, and an Intent broadcast is sent to the app.

  • The app is woken up to process the message in its Intent Receiver.

  • The app can unregister with C2DM when the user no longer wants messages to be pushed to it.

That’s about it! All you need is a server that knows to talk HTTP, and an Android app that knows how to use the Intent APIs. Below are some code samples:

// Use the Intent API to get a registration ID// Registration ID is compartmentalized per app/deviceIntent regIntent = new Intent( "com.google.android.c2dm.intent.REGISTER");// Identify your appregIntent.putExtra("app", PendingIntent.getBroadcast(this /* your activity */,  0, new Intent(), 0);// Identify role account server will use to sendregIntent.putExtra("sender", emailOfSender);// Start the registration processstartService(regIntent);

The registration ID will be delivered back to your app via an intent broadcast with the Intent Action com.google.android.c2dm.intent.REGISTRATION. Here is a code sample to receive the registration ID.

// Registration ID received via an Intentpublic void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (“com.google.android.c2dm.intent.REGISTRATION”.equals(action)) { handleRegistration(context, intent); }}public void handleRegistration(Context context, Intent intent) { String id = intent.getExtra(“registration_id”); if ((intent.getExtra(“error”) != null) { // Registration failed. Try again later, with backoff. } else if (id != null) { // Send the registration ID to the app’s server. // Be sure to do this in a separate thread. }}

On the server side, your server needs to get a ClientLogin Auth token in order to talk to the C2DM servers. When it wants to push a message to the device, it can send an authenticated http POST with:

  • Authorization: GoogleLogin auth=

  • URL encoded parameters including the registration id, the data key/value pairs, a “collapse key” used for overriding old messages with the same key on the Google C2DM servers, and a few other optional params.

When you use the C2DM service, you no longer need to worry about dealing with flaky mobile data connections, or when the user isn’t connected to the internet (i.e. Airplane mode). C2DM keeps the messages in the server store, and delivers them when the device comes back online. Basically, you can leave all the hard work of designing a robust push service to Google. Your application can take advantage of push infrastructure we’ve already built and tested, and stay more connected to the internet. Best of all, you won’t ruin the users’ battery life.

Information about how to build C2DM enabled applications on Android is online at the code lab, and more will be coming as we approach general release.

Android Design V2: Now with stencils

[This post is by Android designer Alex Faaborg, on behalf of the entire User Experience team. —Tim Bray]

When we initially released Android Design, by far the number one request we received was for us to release stencils as well. The fine folks on the Android User Experience team are pleased today to release some official Android Design stencils for your mockup-creating pleasure.

With these stencils you can now drag and drop your way to beautifully designed Ice Cream Sandwich (Android 4.0) applications, with grace and ease. The stencils feature the rich typography, colors, interactive controls, and icons found throughout Ice Cream Sandwich, along with some phone and tablet outlines to frame your meticulously crafted creations.

Currently we have stencils available for those venerable interactive design powerhouses Adobe® Fireworks®, and Omni® OmniGraffle® and we may expand to other applications® in the future. The source files for the various icons and controls are also available, created in Adobe® Photoshop®, and Adobe® Illustrator®. Here are the downloads.

We’ll be updating these stencils over time so, as always, please send in your feedback!

Happy mockup making,
— Your friendly Android Design Droids

Android Developer Challenge, Sub-Saharan Africa!

[This post is by Bridgette Sexton, an innovation advocate for the African tech community. — Tim Bray]

En Français.

In the past year alone, we have met with over 10,000 developers and techies across Sub Saharan Africa. We are continually impressed by the ingenuity and enthusiasm of this community in solving real problems with technology. From applications that crowd-source traffic info to mobile registration of local businesses, handheld devices have taken center stage for consumers and developers in Africa. With a number of countries in the region hovering around 80-90% mobile penetration, mobile is the screen size for the web and the communication experience.

Correspondingly, at every Google event in Africa, Android is the hottest topic; we know why. Every day over 300,000 Android devices are activated globally! A growing number of these mobile devices are powering on for the first time in emerging markets like those in Africa. As Android users multiply, so does the appeal to for developers of building apps on this free open-source platform.

An increasing number of users are searching for 'Android' on Google in Sub-Saharan Africa

For all these reasons and more, we are proud to be launching the Android Developer Challenge for Sub-Saharan Africa!

The Android Developer Challenge is designed to encourage the creation of cool and innovative Android mobile apps built by developers in Sub-Saharan Africa. Invent apps that delight users and you stand a chance to win an Android phone and $25,000 USD. To get started, choose from one of three defined eligible categories (see below), build an Android app in a team or by yourself, and submit it via the competition website by July 1st. The winning app will be announced on September 12th at G-Kenya. Get more details as well as Terms and Conditions on our site.

Categories for Entry:

  • Entertainment / Media / Games

  • Social Networking / Communication

  • Productivity / Tools / Lifestyle

(See Terms & Conditions for more details!)

To launch this competition, we have teamed up with Google Technology User Groups (GTUGs) across Africa to host Android Developer Challenge events. Check out our website for Android gatherings near you, and get coding!

Android at the 2010 Game Developers Conference

2009 has been a great year for Android Market. With the support of developers like you, Android Market now has more than 20,000 applications. Of these applications, games have proven to be particularly popular with Android users. Seven of the ten most popular paid applications on Android Market are games, so there's a significant opportunity for game developers as the number of Android devices continues to grow at a rapid pace.

To better support this trend, we are increasing our support of Android game development. As a first step, we will be presenting a number of Android sessions at the Game Developers Conference on March 9th and 10th in San Francisco. The sessions will be technical and will include everything you need to know about Android game development in Java or C++. Engineers from the Android team will also be available to answer your questions. Whether you are new to the platform or already have games in development, we would love to meet you face-to-face, answer your questions, and see what you're working on. Be sure to keep checking the GDC site because we'll be adding more sessions.

But that's not all. Google and GDC will also be providing complimentary Android phones to attendees who register for All Access or Tutorials and Summits passes by the Early Bird deadline of February 4, 2010. Qualified attendees will receive either a Nexus One or a Verizon Droid by Motorola, so they can quickly apply what they learn from the various Android sessions. You can find more details about the Android phone offer on the GDC site.

Our goal is to make it as easy as possible to develop awesome games for Android, and this is a first step. Hope to see you at GDC!