Clean up local services with direct wifi channel created in IntentService

Apologies for the long post:

I am implementing an Android SDK that needs to transfer data wirelessly without pairing in a background job to adjacent "hubs" (which I control). I use a Wi-Fi Direct P2P service to locate and store my data as a record in a DNS record that the center collects and processes. The value I need to fix is ​​stored in the shared preference instance because it is being modified by two services that run on schedule.

I currently have a process that advertises a local service to IntentService

. He gets the value he has to advertise from his intentions:

protected void onHandleIntent(Intent intent) {
    val = intent.getStringExtra(AppConstants.EXTRA_VALUE);
    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    mRecord = new HashMap<String, String>();
    mRecord.put(AppConstants.RECORD_KEY, val);
    mService = WifiP2pDnsSdServiceInfo.newInstance(
        AppConstants.SERVICE_INSTANCE, AppConstants.SERVICE_REG_TYPE, mRecord);

    mManager.addLocalService(mChannel, mService, addServiceListener);
    mManager.setDnsSdResponseListeners(mChannel, dnsSdServiceResponseListener,
        dnsSdTxtRecordListener);
    mManager.addServiceRequest(mChannel, mRequest, addServiceRequestListener);

    mManager.discoverServices(mChannel, serviceDiscoveryListener);
}

      

And this service is added to the framework (I know this works because I can get the entry on my hub).

The problem occurs when the value changes. I can't just start another instance of this service because the original channel is still open on the first one and therefore two entries will be found by my hub (each device must advertise one instance of my service with one entry). I can't clear anymore mChannel

because the first one IntentService

has already come out / died. Channels persist after the service exists (p2p infrastructure still sees ads).

Several things I've tried / considered:

  • Add BroadcastReceiver

    to each instance IntentService

    that gets notified when the value is updated to either properly kill itself (call mChannel.clearLocalServices()

    ) or start a new service, or just update the value in the original instance by cleaning up and reversing the service on the same channel
    • Doesn't work because the service dies after the discoverServices()

      receiver is not called
  • Foreground Service - Must run in the background, independent of the user interface, and without the user's knowledge. Don't want to hang up the main thread.

In short, I need an efficient way to extend the life of a service in the background indefinitely and still be able to respond to broadcasts (so not sure if this Thread.sleep

will work), save mChannel

somewhere that can be raised the next time the value changes , or completely use a different design (except for services).

+3


source to share





All Articles