Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting network location in background in ANDROID
    primarykey
    data
    text
    <p>I am researched from many threads here for getting location information in Android OS, and tried few designs. However, I was getting lots of battery drain everytime, just couldn't get rid of it. Here is a part of my code.</p> <p>LocationReceiver.Java</p> <pre><code>public class LocationReceiver extends BroadcastReceiver { public static double latestUpdate = 0; public static int status = 0; public static int count_upload = 0; public static int count_ignored = 0; public static Geocoder gc; public void onReceive(Context context, Intent intent) { // Do this when the system sends the intent Bundle b = intent.getExtras(); Location loc = (Location) b .get(android.location.LocationManager.KEY_LOCATION_CHANGED); if (loc == null) return; if (gc == null) gc = new Geocoder(context); update(loc, context); } public void update(Location loc, Context context) { // Here I am checking if 10 minutes passed from last update to now. // and if so, I am uploading my location to my server. if (latestUpdate != 0 &amp;&amp; latestUpdate + ((LaunchReceiver.interval - 2) * 1000) &gt; SystemClock .elapsedRealtime()) { // duplicate (ignore) count_ignored++; } else { // Upload to server on an async task. // ... LocationReceiver.latestUpdate = SystemClock.elapsedRealtime(); count_upload++; } } } </code></pre> <p>LaunchReceiver.Java</p> <pre><code>public class LaunchReceiver extends BroadcastReceiver { public static boolean registered = false; public static LocationManager lm; public static int interval = 600000; public static PendingIntent pendingIntent; SharedPreferences sharedPrefs = null; @Override public void onReceive(Context context, Intent intent) { if (registered) return; sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); interval = Integer.parseInt(sharedPrefs.getString("updates_interval", "600000")); Intent in = new Intent("bdd.sanalmusavir.LOCATION_READY"); pendingIntent = PendingIntent.getBroadcast(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT); lm = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); // Register for broadcast intents lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval, 0, pendingIntent); registered = true; } } </code></pre> <p>AndroidManifest.Xml</p> <pre><code>&lt;application &lt;receiver android:name=".LaunchReceiver" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;receiver android:name=".LocationReceiver" &gt; &lt;intent-filter&gt; &lt;action android:name="bdd.sanalmusavir.LOCATION_READY" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;/application&gt; </code></pre> <p>Of course, this is just part of my code. I tried before same thing with a service by adding alarms periodically but that was draining battery as well. </p> <p>In this practice, let's say I set the interval to 10 minutes, and when I register location updates (requestLocationUpdates), I am getting location updates random intervals (every 30 seconds or so). How can I get rid of it? What is wrong with checking if 10 minutes passed and uploading to server if so?</p> <p>When I check count_ignored and count_uploaded after 1 day, ignored count was 2100~ and uploaded count was 50~ (upload count was true). And my application was used 90 minutes of CPU, which was unacceptable. (Upload part of application is just calling an URL on the web, with HttpRequest).</p> <p>How can I implement better design? Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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