Resetting Android setNetworkPreference
<p>I have an app with a service that tracks my location as a drive to or away from my house and triggers an socket connection when I come in or out of a certain range of my house.  The problem I am having is mostly when I am at my house, start the service, then start driving away from my house.  While at my house my phone is connected to my wifi.  My phone is still in the process of realizing the wifi is out of range and switching to mobile when I reach my proximity.  When the socket connection gets fired, my phone does not have data available and hence doesn't do what it's supposed to do.</p>

<p>A remedy I have come up with is to set the network preference to mobile when the service starts, here is my code.</p>

<pre><code>    public int onStartCommand(Intent intent, int flags, int reqId) {
            cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            cm.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
            //my gps tracking and socket connection code, stopSelf(); gets called in here
    }
</code></pre>

<p>and then .. </p>

<pre><code>    public void onDestroy() {
            super.onDestroy();
            cm.setNetworkPreference(ConnectivityManager.DEFAULT_NETWORK_PREFERENCE);
            //other code: gps cancel, wakelock release, etc
    }
</code></pre>

<p>This code forces my phone to mobile right away so that by the time my distance fires the socket connection it has a mobile connection.  The problem with this code is that in <code>onDestroy</code> the line <code>cm.setNetworkPreference(ConnectivityManager.DEFAULT_NETWORK_PREFERENCE);</code> sets my phone back to wifi, but no wifi is available anymore, so my phone gets stuck not connected to anything.</p>

<p>Is there a way I can tell my phone to go back to it's default state which is wifi if available and mobile otherwise?  Thanks.</p>
