Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>larsVogel solves this problem (and many others like it) <a href="http://www.vogella.com/articles/AndroidServices/article.html#scheduleservice_startauto" rel="nofollow">in this excellent post</a>. </p> <p>this is how i adapted his code to create a connectivity receiver that monitors when the user connects to a WIFI network so as to batch upload usage data:</p> <p>in the Manifest file, place a receiver and declare a service right before the end tag for your &lt; / application >:</p> <pre><code> &lt;receiver android:name=".ConnMonitor" android:enabled="true"&gt; &lt;intent-filter&gt; &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;service android:name=".BatchUploadGpsData" &gt;&lt;/service&gt; &lt;/application&gt; </code></pre> <p>create a broadcast receiver class in a separate file called ConnMonitor.java (please uncomment the Log calls to be able to properly monitor the flow)</p> <pre><code>import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; public class ConnMonitor extends BroadcastReceiver { private String TAG = "TGtracker"; @Override public void onReceive(Context context, Intent intent) { //String typeName = ""; String state = ""; int type = -1; ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); NetworkInfo test = (NetworkInfo) connectivityManager.getActiveNetworkInfo(); //Log.v(TAG,"there has been a CONNECTION CHANGE -&gt; "+intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO)); try { //typeName = test.getTypeName().toString(); type = test.getType(); state = test.getState().toString(); //Log.i(TAG,"type -&gt; '"+typeName +"' state -&gt; '"+state+"'" ); } catch (Exception e) { //typeName = "null"; type = -1; state = "DISCONNECTED"; //Log.i(TAG,"type -&gt; error1 "+e.getMessage()+ " cause = "+e.getCause() ); } if ( (type == 1) &amp;&amp; (state == "CONNECTED") ) { //Log.i(TAG, "I am soooo friggin uploadin on this beautiful WIFI connection "); Intent batchUploadDataService = new Intent(context, BatchUploadGpsData.class); context.startService(batchUploadDataService); } else { //Log.e(TAG,"NO FOUND MATCH type -&gt; '"+typeName +"' state -&gt; '"+state+"'" ); } } } </code></pre> <p>and, finally, create a service BatchUploadGpsData.java like this:</p> <pre><code>import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class BatchUploadGpsData extends Service { final String TAG = "TGtracker"; @Override public void onCreate() { Log.e(TAG, "here i am, rockin like a hurricane. onCreate service"); // this service tries to upload and terminates itself whether it is successful or not // but it only effectively DOES anything while it is created // (therefore, you can call 1 million times if uploading isnt done, nothing happens) // if you comment this next line, you will be able to see that it executes onCreate only the first it is called // the reason i do this is that the broadcast receiver is called at least twice every time you have a new change of connectivity state with successful connection to wifi this.stopSelf(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { //Log.i(TAG, "Received start id " + startId + ": " + intent); Log.e(TAG, "call me redundant BABY! onStartCommand service"); // this service is NOT supposed to execute anything when it is called // because it may be called inumerous times in repetition // all of its action is in the onCreate - so as to force it to happen ONLY once return 1; } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } </code></pre> <p>this is not pseudocode, this is actual code, tested and running on android 2.2 and up.</p> <p>the way to test this service is to shut down and restart your WIFI services on your android (powering off the wifi router will also do the trick). BUT this code does not verify if you are effectively connected to the net. for that, i recomend that you make an httpclient request and check out the result of the call. beyond the scope of this discussion.</p> <p>NOTE: since services run on the same thread as the UI, i highly recommend that you implement the uploading proper on a separate thread or asynctask, depending your specific needs. you can also run the whole service on a separate thread, but that is once again not the scope of this discussion, despite being standard practice in these cases.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload