Blog

Wed

13

Jul

2011

Push notifications for Android

This video from the Google I/O 2010 shows how to get started with Android Cloud to Device Messaging (C2DM) - a service that helps developers to push data from servers to their applications on Android devices. The service provides a simple and efficient mechanism for servers to notify Android applications that there is new data ready to be fetched from the server. What's more, C2DM manages all aspects of queueing of messages and transmission to the target application.

 

 




0 Comments

Mon

04

Jul

2011

Consuming RESTful web services on Android using Spring Android

In order to simplify the development of native Android applications, the Spring community produced Spring Android, an extension of the Spring Framework specifically targeted to Android. This extension provides a Representational State Transfer (REST) client for Android as well as authentication support for accessing secure APIs. In this blog post, we are going to see how to use Spring Android to write client code for an Android application that consumes RESTful web services.

Let's suppose that we have a web service that handles HTTP GET requests to get all the currently available products, each described by an id, a name and a price. The implementation of the Product class is found in the listing below:

 

 public class Product {
private long id;
private String name;
private double price;

// the usual getters and setters...
}

 

And, assuming that the web service serves JavaScript Object Notation (JSON), the response entity-body for a GET request for all available products might look as follows:

 

[{"name":"Banana","id":1,"price":23.0},{"name":"Peanut butter","id":2,"price":50.5}]

 

Now that we have looked at the REST service, let's see the steps involved to consume it in an Android application using Spring Android's RestTemplate.

  1. Start a new Android project named HelloSpringAndroid.
  2. Now add the required Spring Android's JARs (i.e., spring-android-core-1.0.0.M3.jar, spring-android-rest-template-1.0.0.M3.jar) and their dependent JARs (i.e., jackson-core-asl-1.8.2.jar, jackson-mapper-asl-1.8.2.jar) to the libs directory of your project and include them in the project's classpath. You can download the JAR files from http://www.springsource.com/download/community?project=Spring%20Android and http://wiki.fasterxml.com/JacksonDownload.
  3. You also need access to the Internet in order to connect to the web service, so you must also request the INTERNET permission. In the Android manifest file AndroidManifest.xml, add the following as a child of the <manifest> element:

     

    <uses-permission android:name="android.permission.INTERNET" />
    

     

  4. Open the HelloSpringAndroid.java file and add the onCreate() callback method to the class:

     

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    

     

    In order to connect to the RESTful web service described above and fetch the results, you need to add the following lines of code at the end of the onCreate() method:

     

    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate();

    // The URL for making the GET request final String url = "http://10.0.2.2:8080/rest/products"; // Instantiate the HTTP GET request, expecting an array of // Product objects in response Product[] products = restTemplate.getForObject(url, Product[].class);

     

  5. The response of the web service are JSON objects, so in order to convert the response into Java objects you need to create the Product.java file on the client as well (see the beginning of the blog post for its definition).
  6. That's all. (This post has only described how to perform a HTTP GET request to a RESTful web service; however, the other RESTful methods (i.e., POST, PUT and DELETE) can be implemented in a similar manner.)

 

 

0 Comments

Mon

06

Jun

2011

How to add search functionality to your Android application


Figure 1: Screenshot of an application's search dialog

With data-intensive applications, the ability for users to search for information within an application is an essential feature. Fortunately, Android includes a search framework that helps developers to implement search in their applications and to create a consistent search experience for users. This blog post describes how to implement search functionality using this framework.

 

Enabling application search using the Android search framework involves the following three steps:

  1. Creating a searchable configuration
  2. Creating a searchable activity
  3. Implementing a search interface

 

 

Step 1: Creating a searchable configuration

The first thing that is needed is an XML file called the searchable configuration in the res/xml folder. This file configures various UI aspects of the search interface and defines the behavior of certain search features (e.g., voice search, search suggestions). A typical searchable configuration file might look as follows:

 

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name" android:hint="@string/search_hint">
</searchable>

 

The android:label attribute is the only required attribute and references a string resource denoting the name of the application, whereas the android:hint is a recommended attribute and specifies a hint text to display in the search box when no text has been entered. Fore more information about the searchable configuration file, see the Android Developers documentation for Searchable Configuration.     

 

 

Step 2: Creating a searchable activity

Next, an activity is needed to perform searches and to display the search results. Usually, this will be a ListView-based activity, but it is possible to use any other user interface. As shown below, inside the manifest's <activity> element, it's necessary to include an intent filter to accept the ACTION_SEARCH action as well as a <meta-data> element that includes an android:name attribute with the value android.app.searchable and an android:resource attribute with a reference to the searchable configuration file.

 

<application ... >
    <activity android:name=".SearchableActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />

        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>
    ...

</application>

 

When a user performs a search from the search interface, the Android system starts the searchable activity, passing it an ACTION_SEARCH intent that includes the search query in the QUERY string extra. Then, when the searchable activity starts, the search query can be retrieved and processed as follows:

 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);

// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}

 

The local doMySearch() method performs the search operation (e.g.,  by querying a local SQLite database or by submitting a request to a search web service) and finally presents all the search results to the user (e.g., by applying the results to a ListView).

 

Step 3: Implementing a search interface

The search interface can be implemented using either:

  • The search dialog

The search dialog (see Figure 1), which by default is always hidden, appears at the top of an activity when the user presses the device's SEARCH button (if available) or when the onSearchRequested() method is called. However, except for the searchable activity itself, either of these work only if one enables the search dialog for the activity. This is achieved by placing a <meta-data> element with the android:name attribute with a value of android.app.default_searchable and the android:value attribute with a reference to the searchable activity inside the respective activity's <activity> element. The example below shows the declaration for both a searchable activity (i.e., SearchableActivity) and another activity (i.e., OtherActivity) that uses SearchableActivity to handle search queries from its search dialog.

 

<application ... >
<!-- this is the searchable activity; it performs searches -->
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>

<!-- this activity enables the search dialog to initiate searches
in the SearchableActivity -->
<activity android:name=".OtherActivity" ... >
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
</activity>
...
</application>

 

In order to enable the search dialog for every activity in an Android application, one can add the above <meta-data> element inside the <application> element, instead of each <activity>.

 

  • Or, the search widget

The search widget, which is available in Android 3.0 and higher, is an instance of SearchView that can be placed anywhere in an activity layout. To configure the search widget, it suffices to call setSearchableInfo() and pass it the SearchableInfo object that represents the searchable configuration:

 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) findItemById(R.id.search_view);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
}

 

 

5 Comments

 

Blog Archive (all posts)

 




StatCounter
About | Privacy Policy | Sitemap
© Matthias Braunhofer. All rights reserved