【问题标题】:How to use PositionService in Gluon Mobile on iOS?如何在 iOS 上的 Gluon Mobile 中使用 PositionService?
【发布时间】:2016-08-27 11:41:13
【问题描述】:

我想在 iOS 上使用 Gluon Mobile 的 PositionService。我编写了一个在桌面上运行的小型示例应用程序,提供(如预期的)虚假位置更改,并在 Android 上运行。但是,在 iOS 模拟器上,不会调用侦听器。这是代码的相关部分:

public class BasicView extends View {

        private static final Logger LOGGER = Logger.getLogger(BasicView.class.getName());

        final Label label;

        public BasicView(String name) {
            super(name);
            label = new Label();
            VBox controls = new VBox(15.0, label);
            controls.setAlignment(Pos.CENTER);
            setCenter(controls);
            // get current position
            Platform p = PlatformFactory.getPlatform();
            PositionService ps = p.getPositionService();
            outputPos(ps.getPosition());
            ps.positionProperty().addListener((obs, oldPos, newPos) -> {
                LOGGER.log(Level.INFO, "\nobs: {0}\noldPos: {1}\nnewPos: {2}",
                        new Object[]{obs, oldPos, newPos});
                outputPos(newPos);
            });
        }

        private void outputPos(Position p) {
            if (p != null) {
                label.setText(String.format("We are currently here: %f %f",
                        p.getLatitude(),
                        p.getLongitude()));
            }
        }

        @Override
        protected void updateAppBar(AppBar appBar) {
            appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> System.out.println("Menu")));
            appBar.setTitleText("Basic View");
            appBar.getActionItems().add(MaterialDesignIcon.SEARCH.button(e -> System.out.println("Search")));
        }

    }

我已经添加了 libCharm.a,但据我所知,这里不需要它。

我还发现了有关更新 info.plist 的提示,如下所示,但无论有没有它都不会调用侦听器。

    <key>UIRequiredDeviceCapabilities</key>
    <array>
            <string>armv7</string>
            <string>location-services</string>
    </array>
    ...
    <key>NSLocationWhenInUseUsageDescription</key>
            <string>Location is required to find out where you are</string>
    <key>NSLocationAlwaysUsageDescription</key>
            <string>Location is required to find out where you are</string>

我看到的关于位置的唯一输出是:

Aug 27, 2016 1:37:31 PM com.gluonhq.charm.down.ios.IOSPositionService <init>
INFO: Location Manager configured with desiredAccuracy 10.00 and distanceFilter 100.00

Aug 27, 2016 1:37:31 PM com.gluonhq.charm.down.ios.IOSPositionService <init>
INFO: Telling LocationManager to start updating location.

我想我在这里遗漏了一些东西......一如既往,非常感谢任何帮助。

编辑:抱歉,忘记了 build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

mainClassName = 'com.thomaskuenneth.gluon.positionservicedemo.PositionServiceDemo'

dependencies {
    compile 'com.gluonhq:charm:3.0.0'

    androidRuntime 'com.gluonhq:charm-android:3.0.0'
    iosRuntime 'com.gluonhq:charm-ios:3.0.0'
    desktopRuntime 'com.gluonhq:charm-desktop:3.0.0'
}

jfxmobile {
    android {
        manifest = 'src/android/AndroidManifest.xml'
        compileSdkVersion = 23
    }
    ios {
        infoPList = file('src/ios/Default-Info.plist')
        forceLinkClasses = [
                'com.gluonhq.**.*',
                'io.datafx.**.*',
                'javax.annotations.**.*',
                'javax.inject.**.*',
                'javax.json.**.*',
                'org.glassfish.json.**.*'
        ]
    }
}

【问题讨论】:

  • 我们刚刚发布了 Charm Down 3.0.0,对库进行了全面检修。查看更新的 Gluon 示例,这些示例使用了新的 1.1.0 版本的 jfxmobile 插件。 IDE 插件也更新为使用这些版本。试一试,如果这能解决您的问题,请告诉我。
  • 何塞,抱歉花了一些时间回复。我升级到了Gluon Mobile 4,也就是插件和新的Charm Down。一旦我升级了我的代码(使用服务)并应用了我在回答中提到的更改,我开始在 iPad 模拟器中看到位置变化。所以,升级到 Gluon Mobile 4 解决了我的问题。 ;-)
  • 您是否导入com.gluonhq.charm.down.plugins.*(包含PositionServices)?我在我的build.gradle 文件中编译com.gluonhq:charm:4.3.7 - 但我不能/不知道如何访问最新的服务 - 只能看到数量有限的服务。它们无法通过import com.*.*...(等)实现。

标签: gluon-mobile


【解决方案1】:

我听从了 José Pereda 的建议并升级到了 Gluon Mobile 4。在执行以下步骤后,我在 iPad 模拟器中看到了位置变化。所以,我建议任何遇到我在问题中提到的同样困难的人,尝试升级。这解决了我的问题。

更新 build.gradle 如下:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.1.1'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

mainClassName = 'com.thomaskuenneth.gluon.positionservicedemo.PositionServiceDemo'

dependencies {
    compile 'com.gluonhq:charm:4.0.1'
}

jfxmobile {
    downConfig {
        version '3.0.0'
        plugins 'display', 'lifecycle', 'statusbar', 'storage', 'position'
    }
    android {
        manifest = 'src/android/AndroidManifest.xml'
        androidSdk = "/Users/thomas/Library/Android/sdk"
        compileSdkVersion = 24
    }
    ios {
        infoPList = file('src/ios/Default-Info.plist')
        forceLinkClasses = [
                'com.gluonhq.**.*',
                'io.datafx.**.*',
                'javax.annotations.**.*',
                'javax.inject.**.*',
                'javax.json.**.*',
                'org.glassfish.json.**.*'
        ]
    }
}

由于 Charm Down API 稍有改动,将访问仓位服务的代码修改如下:

package com.thomaskuenneth.gluon.positionservicedemo;

import com.gluonhq.charm.down.Services;
import com.gluonhq.charm.down.plugins.Position;
import com.gluonhq.charm.down.plugins.PositionService;
import com.gluonhq.charm.glisten.control.AppBar;
import com.gluonhq.charm.glisten.mvc.View;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

public class BasicView extends View {

    private static final Logger LOGGER = Logger.getLogger(BasicView.class.getName());

    final Label label;

    public BasicView(String name) {
        super(name);
        label = new Label();
        VBox controls = new VBox(15.0, label);
        controls.setAlignment(Pos.CENTER);
        setCenter(controls);
        Services.get(PositionService.class).ifPresent(serivce -> {
            serivce.positionProperty().addListener((obs, oldPos, newPos) -> {
                LOGGER.log(Level.INFO, "\nobs: {0}\noldPos: {1}\nnewPos: {2}",
                        new Object[]{obs, oldPos, newPos});
                outputPos(newPos);
            });
        });
    }

    private void outputPos(Position p) {
        if (p != null) {
            label.setText(String.format("We are currently here: %f %f",
                    p.getLatitude(),
                    p.getLongitude()));
        }
    }

    @Override
    protected void updateAppBar(AppBar appBar) {
        appBar.setNavIcon(MaterialDesignIcon.MENU.button(e -> System.out.println("Menu")));
        appBar.setTitleText("Basic View");
        appBar.getActionItems().add(MaterialDesignIcon.SEARCH.button(e -> System.out.println("Search")));
    }

}

在早期版本中,似乎有必要在 src/ios/jniLibs 中包含文件 ibCharm.a。使用 Gluon 4,我在链接时一直看到错误,直到我删除了文件。我假设由于 Charm Down 的重构,该目录中不再需要 lib。

另外,请注意,您需要编辑 Default-Info.plist,正如现在关于 PositionService 的 Charm Down 文档中提到的那样。我已经在我的问题中描述了这一点。截至今天,文档没有提到考虑添加

<string>location-services</string>

<key>UIRequiredDeviceCapabilities</key>
<array>
    ...

但我认为如果您的应用需要定位服务并且不会运行,这可能是一个好主意。或许 iOS 专家可以对此发表评论。

【讨论】:

  • 看起来不错。关于更新到 Gluon Charm 4,Gluon IDE 插件已经做到了,用最新版本和新的 Down 插件更新你的 build.gradle。您会注意到默认添加了其他必需的插件,例如 lifecyclestorage。 ConfigDown 会为你添加原生库,无需保留libCharm.a,也无需使用任务extractNativeLibrary
  • 关于UIRequiredDeviceCapabilities,我不确定这是否是强制性的。在我的测试中,每个插件(加速度计、相机、位置......)都可以正常工作,而无需将它们添加到该列表中。似乎更多的是the App Store uses the contents of this key to prevent users from downloading your app onto a device that cannot possibly run it.
  • 何塞,我认为您对 UIRequiredDeviceCapabilities 的假设是正确的,不需要使代码正常工作。但是,如果设备上没有位置服务,应用程序根本无法运行,则可能应该设置它。 :-) 关于其他必需的插件......实际上我删除了它们。您会建议我更新我的答案,即重新包含它们吗?
  • 是的,您应该包含它们,因为 Gluon Charm 库依赖于它们,但它不包含它们。在您的情况下,lifecycle(如果存在)用于避免在应用程序进入后台时运行服务。
  • 感谢您花时间测试并发布它,它肯定会对其他人有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多