Receiving Intents

pocmo edited this page Feb 5, 2013 · 2 revisions
Clone this wiki locally

Intents are received using the Discovery class. Once started by calling enable() the Discovery class will spawn a background thread that will wait for incoming Intent objects. A DiscoveryListener instance will be notified about every incoming Intent.

Sample code

// (1) Implement a listener

DiscoveryListener listener = new DiscoveryListener() {
    public void onDiscoveryStarted() {
        // The discovery has been started in the background and is now waiting
        // for incoming Intents.
    }

    public void onDiscoveryStopped() {
        // The discovery has been stopped. The listener won't be notified for
        // any incoming Intents anymore.
    }

    public void onDiscoveryError(Exception exception) {
        // A (network) error has occured that prevents the discovery from working
        // probably. The actual Exception that has been thrown in the background
        // thread is passed to this method. A call of this method is almost always
        // followed by a call to onDiscoveryStopped()
    }

    public void onIntentDiscovered(InetAddress address, Intent intent) {
        // An Intent has been successfully received from the given address.
    }
}

// (2) Create and start a discovery

Discovery discovery = new Discovery();
discovery.setDiscoveryListener(listener);

discovery.enable() // Start discovery

// later..
discovery.disable() // Stop discovery