I am trying to request the network, so I build the NetworkRequest and I build the PendingIntent,
ConnectivityManager conn_manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest net_req;
net_req = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) //Data Network?
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) //Wifi
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) //This might be what need internet permission
.addCapability(NetworkCapabilities.NET_CAPABILITY_MMS)
.build();
Intent intent_sent = new Intent(activity, Service_MmsNetworkAvailable.class);
PendingIntent pending_intent_net_req_received = PendingIntent.getService(activity, 0, intent_sent,
PendingIntent.FLAG_ONE_SHOT);
conn_manager.requestNetwork(net_req, pending_intent_net_req_received);
Then once the Service runs from the PendingIntent calling it, I am doing some Network stuff inside the Service and then when I'm done I believe I need to call releaseNetworkRequest(PendingIntent).
But here's the whole problem of mine, how do I reference the PendingIntent that started the Service from inside the Service? There is no guarantee that the Activity that created the NetworkRequest and PendingIntent will still be alive so I cannot rely on returning to that Activity to be able to call releaseNetworkRequest(PendingIntent) where I have reference to the PendingIntent.
Android specifically states that you must pass in the same PendingIntent into releaseNetworkRequest(PendingIntent) that you passed into conn_manager.requestNetwork(net_req, pending_intent_net_req_received);
So how is a developer supposed to releaseNetworkRequest(PendingIntent)?
Can I somehow reference the PendingIntent from the Service? I understand that PendingIntents stay alive even after the Activity that called it parishes into the abyss.
I must be missing something here, PendingIntents call Activities, Receivers, and Services, none of which I see can reference back to the original PendingIntent. Furthermore, NULL cannot be passed into releaseNetworkRequest(PendingIntent) according to Android documentation?
Also just to note, I am only doing this because startUsingNetworkFeature() is deprecated and developers are encouraged to use NetworkRequest(), arg!!!
I am literally stuck here now with no way to go, this is rare to be so backed into a corner here if anyone can shed light on this predicament of mine I would be grateful.
You can recreate the PendingIntent
in your Service
by using the same code you used when you created it in the first place:
Intent intent_sent = new Intent(activity, Service_MmsNetworkAvailable.class);
PendingIntent pending_intent_net_req_received = PendingIntent.getService(activity, 0, intent_sent,
PendingIntent.FLAG_ONE_SHOT);
Instead of activity
in new Intent(...)
, you can use the Service
instance, as you just need a Context
from your application.
NOTES:
It probably isn't necessary to use PendingIntent.FLAG_ONE_SHOT
when creating (or recreating) this PendingIntent
. I've had a number of issues with the behaviour of FLAG_ONE_SHOT
, so I would just remove it if I were you.
The documentation indicates that the PendingIntent
should trigger a Broadcast
, but you are using a Service
. This may or may not work as expected. Good luck!