在ANDROID中获取后台网络位置
我在这里从许多线程中研究了如何在Android操作系统中获取位置信息,并尝试了一些设计。然而,我每次都会有大量的电池耗电,只是无法摆脱它。这是我代码的一部分 Java在ANDROID中获取后台网络位置,android,location,broadcastreceiver,Android,Location,Broadcastreceiver,我在这里从许多线程中研究了如何在Android操作系统中获取位置信息,并尝试了一些设计。然而,我每次都会有大量的电池耗电,只是无法摆脱它。这是我代码的一部分 Java public class LocationReceiver extends BroadcastReceiver { public static double latestUpdate = 0; public static int status = 0; public static int count_upload = 0; pub
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
&& latestUpdate + ((LaunchReceiver.interval - 2) * 1000) > SystemClock
.elapsedRealtime()) {
// duplicate (ignore)
count_ignored++;
} else {
// Upload to server on an async task.
// ...
LocationReceiver.latestUpdate = SystemClock.elapsedRealtime();
count_upload++;
}
}
}
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;
}
}
Java
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
&& latestUpdate + ((LaunchReceiver.interval - 2) * 1000) > SystemClock
.elapsedRealtime()) {
// duplicate (ignore)
count_ignored++;
} else {
// Upload to server on an async task.
// ...
LocationReceiver.latestUpdate = SystemClock.elapsedRealtime();
count_upload++;
}
}
}
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;
}
}
AndroidManifest.Xml
<application
<receiver
android:name=".LaunchReceiver"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<receiver android:name=".LocationReceiver" >
<intent-filter>
<action android:name="bdd.sanalmusavir.LOCATION_READY" />
</intent-filter>
</receiver>
</application>
LocationManager.requestLocationUpdates上的字段minInterval需要毫秒时间。你超过600,这将导致经理每600毫秒或基本上尽可能快地开火。我想你只是少了一个*1000。这只是为了测试。对不起,我忘了在我的帖子上提到它。我使用的是我的首选项600*1000。修正了我的帖子。