jeudi 21 mai 2015

Android - passing data from Service to main at unknown point in time

I've looked at some of the other questions here on SO and none seem to be answering my question. I have a main activity with a simple button. Right now it calls startService(new Intent(MainActivity.this, BluetoothScanner.class) which runs until it finds another bluetooth device. When this is done, I want the BluetoothScanner to report that it is complete and return something (a string perhaps) to the main activity, so I can create a popup or something that notifies the user that a bluetooth device has been seen in the vicinity. How do I best go about this?

The button looks like:

public void onClick(View v) {
    switch(on) {
        case 0:
            startService(new Intent(this, BluetoothScanner.class));
            on = 1;
            break;
        case 1:
            stopService(new Intent(...);
            on = 0;
            break;
       }
 } 
});

There seems to be some alternatives, like global variables (bad), resultReceivers,bundling stuff in intents. But I'm not sure how to do this. What would you suggest solves my problem?

Regards

Aucun commentaire:

Enregistrer un commentaire