gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:14:30 24 4
gpt4 key购买 nike

我有一个名为 GPS_Service 的服务。 从另一个 Activity 我想启动此服务并从 onLocationChanged() 方法获取 location.getLatitude() 和 location.getLongitude() 的值。如果 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);
}
}
}

最佳答案

在您的服务中尝试一下:尝试使用Bundle:

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);

关于java - 在 onLocationChanged 方法中从服务获取纬度/经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46287070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com