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

标签 java android gps

我有一个名为 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/

相关文章:

unix - 使用gpsd或cgps返回纬度和经度,然后退出

java - 如何使用 maven-swagger-codegen-plugin 生成文档?

java - 如何在java中的引号之间获取数据?

android - 手机重启后小部件的按钮不起作用

android - 多次运行 JUnit 测试会给出不同的结果

android - 为什么手机gps在不同的位置提供相同的坐标

java - 尝试理解最终变量

java - 正则表达式模式在没有空格结束时不起作用

android - 如何使用 BETWEEN 和 DOUBLE 值在 SQLite 中查询?

android - 在应用程序退出时关闭 bt/gps/wifi 是否明智?