Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在onLocationChanged方法中从服务获取纬度/经度_Java_Android_Gps - Fatal编程技术网

Java 在onLocationChanged方法中从服务获取纬度/经度

Java 在onLocationChanged方法中从服务获取纬度/经度,java,android,gps,Java,Android,Gps,我有一个叫做GPS_服务的服务。从另一个活动中,我想启动此服务,并从onLocationChanged()方法中获取location.getLatitude()和location.getLatitude()的值。如果onLocationChanged()不是静态的,我该怎么做 提前谢谢 public class GPS_Service extends Service { private LocationListener listener; private LocationManager loc

我有一个叫做GPS_服务的服务。从另一个活动中,我想启动此服务,并从
onLocationChanged()
方法中获取location.getLatitude()和location.getLatitude()的值。如果
onLocationChanged()
不是静态的,我该怎么做

提前谢谢

public class GPS_Service extends Service {

private LocationListener listener;
private LocationManager locManager;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    liveLocs = new ArrayList<>();
    if (PreferenceManager.getDefaultSharedPreferences(GPS_Service
            .this).getString("len_live_locations", "-") != "-") {

    }

    listener = new LocationListener() {
    @Override
        public void onLocationChanged(Location location) {
            Intent i = new Intent("location_update");
            i.putExtra("coordinates", location.getLongitude() + "       " + location
                    .getLatitude());
            sendBroadcast(i);
        }
    }
}
公共类GPS\u服务扩展服务{
私密地点监听;
私人场所经理;
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
public void onCreate(){
liveLocs=newarraylist();
if(首选项管理器。GetDefaultSharedReferences(GPS\U服务
.this).getString(“len_live_locations”、“-”!=“-”){
}
listener=新位置listener(){
@凌驾
已更改位置上的公共无效(位置){
意向i=新意向(“位置更新”);
i、 putExtra(“坐标”,location.getLongitude()+“”+位置
.getLatitude());
发送广播(一);
}
}
}

在您的服务中尝试以下方法:尝试使用
捆绑包

Intent intent = new Intent("YourAction");
Bundle bundle = new Bundle();
bundle.put... // put extras you want to pass with broadcast. This is optional
bundle.putString("valueName", "The value you want in the activity");
bundle.putDouble("doubleName", someDouble);
intent.putExtras(bundle)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

这可能会变得非常复杂。你必须决定你将使用哪种类型的位置提供商,获得权限,检查他们是否安装了Google Play服务以及所需的正确版本,当然还有位置管理器与融合位置管理器等的备用选项

我花了很多时间在监控位置附近编写库,这会非常耗时,维护有时会有点痛苦

下面是我的建议。你提到了“服务”,我不知道这是否是你真正需要的,但你可以做三件事中的一件

  • 使用单例包装器管理位置
  • < LI>创建一个智能服务,以获得您满意的精度的位置,然后考虑使用自定义广播接收器广播接收您的更改。
  • 创建一个常规的Android服务,该服务可以继续更新位置,并根据需要从后台反馈给您,您需要绑定到该服务,启动和停止该服务由您决定。但是,请记住,在后台收集位置有限制,可能需要在现在
  • 我建议你从一个简单回调的单例包装器中的一个好库开始,然后从那里进入一个服务包装器。我个人喜欢这个库用于位置更新


    很好。祝你好运。

    中有很多例子,我建议你去那里。但是你能告诉我如何调用这个方法(onLocationChanged)吗从另一个活动中?我在这里找不到:@Marci它的优点是,你根本不调用onLocationChanged。相反,只要android设备的位置发生变化,LocationManager就会调用它。为了实现这一点,你需要通过调用reques在LocationManager实例上设置你的LocationListener实例tLocationUpdates。这里有一个例子:“intent.putExtras(intent)”我不理解这一点,为什么你把intent放在它自己?我认为这只是一个很小的错误。它应该是intent.putExtras(intent)而不是intent.putExtras(intent)我明白了,谢谢。但是好的,这个代码片段在我的onLocationChanged方法中。但是我如何从另一个活动中获取捆绑包呢?@Marci您需要在该活动中设置一个BroadcastReceiver。例如,请参阅,让我推荐使用LocalBroadcastManager(正如Gowthaman建议的那样)而不是“sendBroadcast”如在原始代码片段中。如果您的活动和服务在同一个应用程序中,LoopAlxBaseMead是您所需要的,并且它更安全/私密。@ MARCI最后,我不知道将LoistListInter放入单独的服务中的原因,但是……也许您应该考虑将LoistListListar直接放入活动中?