我已经搜索了很多,我并不完全无知。我已经实施了一个临时解决方案,但是想知道是否有更好的方法。将位置信息提供给服务的最佳方式
我有一个应用程序,每60秒后将一个人的位置发送到服务器。在我的仪表盘(主屏幕应用程序启动后,将去的onPause),我已经注册了的LocationManager用下面的代码:
service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled)
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
else
{
Criteria criteria = new Criteria();
provider = service.getBestProvider(criteria, false);
service.requestLocationUpdates(provider, 10000, 50, this);
Location location = service.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null)
{
onLocationChanged(location);
}
else
{
Log.d("Location: ", "No update received");
}
}
然而,正如我所说,本次活动将被最小化用户(通过按主页按钮)。 AlarmManager每60秒会调用一次服务。该服务从Dashboard Activity(lat,lon)访问静态变量并将其发送到服务器。
我的问题:
如果活动去的onPause,将requestLocationUpdates功能停止?或者它会继续工作?
如果它继续工作,它将不断更新两个经纬度和静态字符串对象,并且服务将不断获取更新值。如果他们停下来,服务会一直获得相同的旧值。
此外,有没有更好的方法来解决这个问题?使用GPS提供商和网络提供商的混合? (我需要相当准确的值)。
编辑
这里是我的报警。此代码是内部登录活动
Intent i = new Intent(con, LocationPoller.class);
i.putExtra(LocationPoller.EXTRA_INTENT, new Intent(con,
Login.class));
i.putExtra(LocationPoller.EXTRA_PROVIDER,
LocationManager.GPS_PROVIDER);
gps = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(con, 0, i, 0);
gps.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
10 * 1000, pi);
Log.d("Service: ",
"GPS Service started and scheduled with AlarmManager");
这里是我的接收器(也内登录活动)
private class ReceiveMessages extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Location loc = (Location) intent.getExtras().get(
LocationPoller.EXTRA_LOCATION);
String msg;
if (loc == null)
{
msg = intent.getStringExtra(LocationPoller.EXTRA_ERROR);
}
else
{
msg = loc.toString();
}
if (msg == null)
{
msg = "Invalid broadcast received!";
}
Log.d("GPS Broadcast: ", msg);
}
}
没有发生的事情:不是过得好logcat的,这意味着没有被接收的广播东西。
这个例子是这样复杂的微不足道,因为这一个任务。 – Asim
@Asim - 我假设你想持续获取位置并更新到服务器。如果是这样的话,那么你需要这种复杂性。例如,如果手机处于睡眠模式并且您想上传位置信息。为此,您需要获取唤醒锁定,获取并上传位置,然后释放唤醒锁定。如果您的需要只是在活动处于前台时获取并更新位置,则可以通过从您的活动获取位置并将其更新到服务器的服务启动服务 – Amit
确定我已导入库。我现在有点困惑(全新的BroadcastReceiver)。我正在登录屏幕上启动闹钟。该屏幕在用户登录时(用户登陆仪表板)进入onResume(或其他)。 RECEIVER应该进行哪项活动?此外,我的仪表板活动已在使用接收器进行其他任务。如何区分多个广播?我知道intents ..但是这个广播的初衷是Intent(Login,Dashboard),但是我怎样才能在我的Dashboard活动中使用它? 对不起,如果这是所有非常混淆:) – Asim