Search code examples
androidbroadcastreceiver

Android - Possible to add broadcast to separate application?


This might be a stretch, but I was wondering if it is possible to add functionality to an application that is already created for the android device. Specifically, I would like to send a broadcast whenever the user tries to make a new search in the internet browser. There might be another way to act only when the user searches the browser, but I thought this would be the easiest. If this isn't possible (or is completely the wrong way of going about this), please let me know. Any help is appreciated.


Solution

  • You can probably query the Browser provider and get search data from it. Here is some basic code on how to do it:

    Cursor cursor = this.context.getContentResolver().query(Browser.SEARCHES_URI, null, null, null, null);
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                final int indexDate = cursor.getColumnIndex(Browser.SearchColumns.DATE);
                final int indexTerm = cursor.getColumnIndex(Browser.SearchColumns.SEARCH);
                String date = cursor.getString(indexDate);
                String term = cursor.getString(indexTerm);
                cursor.moveToNext();
            }
        }
        cursor.close();
    

    Keep in mind that it would be better if you run queries on a separate Thread using the Loader framework.

    I do not think there is a broadcast sent when a new search is performed.