Configure and start a search

This task demonstrates how to start a search that is limited to the searchable entities that your application creates. The UnifiedSearchServices.search() method blocks thread execution, so doSearch() below invokes it in a separate thread.

  1. Import the required classes and interfaces.
    import net.rim.device.api.unifiedsearch.*;
    import net.rim.device.api.unifiedsearch.searchables.*;
    import java.util.*;
    
  2. Create an instance variable to store a reference to the UnifiedSearchServices object.
    public class MySearchScreen() {
        UnifiedSearchServices _uss = UnifiedSearchServices.getInstance();
  3. Retrieve a reference to your EntityBasedSearchable object and assign it to an instance variable. When you retrieve a list of searchable applications on the device, change the SearchableContentTypeConstants value to one that is appropriate for your search. Make sure you take some kind of action if your EntityBasedSearchable is not found in the deviceSearchables vector.
    MySearchable _searchable = null;
    Vector deviceSearchables = _uss.getDeviceSearchables(SearchableContentTypeConstants.CONTENT_TYPE_MEMO);
    for(int i = deviceSearchables.size() -1; i >=0; --i) {
        if(deviceSearchables.elementAt(i).getName().equals("name of your searchable")) {
            _searchable = (MySearchable) deviceSearchables.elementAt(i);
        }
    }
    // Do something if _searchable is still null
  4. Create a method to start the search. Accept the search keyword as a parameter, where your application specifies the search keywords that the BlackBerry® device user provides. This method invokes a new thread, so declare the String parameter as final.
    private void doSearch(final String keyword) {
  5. Create the Thread object that will run your search.
        Thread searchThread = new Thread(new Runnable() {
            public void run() {
                try {
  6. Start the search and retrieve the results, if any. If there is a result, send it to another method to be parsed.
                    SearchResponse results = null;
                    results = _uss.search(keyword, _searchable);
                    if(results != null) {
                        parseResponse(results);
                    }
  7. Catch any errors that are thrown.
                } catch (Exception e) {
                    // Do something about the error
                }
  8. Complete the Thread definition and run it.
            }, "Search Thread");
        searchThread.start();
After you finish: Implement parseResponse. For more information see "Process search results."
Previous topic: Searching

Was this information helpful? Send us your comments.