【问题标题】:Appcelerator Titanium Ti.Geolocation.addEventListener('location',{}); not triggering when turn off the gpsAppcelerator Titanium Ti.Geolocation.addEventListener('location',{});关闭gps时不触发
【发布时间】:2017-02-27 13:50:10
【问题描述】:

每当我打开或关闭移动设备中的 gps 时,我都需要更改图标颜色。目前我正在使用Ti.Geolocation.addEventListener('location',{}); 在我打开或关闭 GPS 时触发。当我打开gps时它会触发,但是当我关闭gps时它不会触发。我在 androidiOS 中都需要此功能,但在两者中都不起作用。

为什么当我们关闭 GPS 时这个 api 没有触发?任何其他方式来解决这个问题。

【问题讨论】:

    标签: gps titanium appcelerator


    【解决方案1】:

    我认为他们没有这样的事件监听器可以在 GPS 关闭时触发,相反,您可以尝试下面的代码,看看它是否能解决您的目的:

    exports.testGPS = function(_callback) {
        Ti.Geolocation.purpose = "Recieve User Location";
        Ti.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
         if (_callback && typeof(_callback)==='function') {
            Ti.Geolocation.getCurrentPosition(function(e) {
                    _callback ((e.error) ? false: true);
            });    
        }
    }
    

    您可以根据需要更新此功能。它会 获取时通过 false 并在检索当前时出错 设备的位置,因此我们可以得出结论,GPS 设置已关闭。

    祝你好运, 干杯

    编辑

    您可以使用广播接收器来做到这一点,见下文:

    更改 tiapp.xml 中的清单

    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    

    创建广播接收器并在 index.js 中注册

    var bc = Ti.Android.createBroadcastReceiver({
        onReceived: function() {
            Ti.API.info('Handling broadcast.');
        }
    });
    
    Ti.Android.registerBroadcastReceiver(bc, [Ti.Android.ACTION_PROVIDER_CHANGED]);
    

    【讨论】:

    • 为了使用这个函数我需要调用这个函数对吗?但我的问题是当我关闭 GPS 时,我需要一个触发器。每次打开或关闭gps时如何触发该触发器。在该函数内部,我可以调用上述函数。你对此有什么想法吗?
    • @Dev786:嗨,我已经编辑了问题,我想这将满足您的要求。看看,让我知道它是否工作正常。
    • 不幸的是它在android中不起作用。我们需要在manifest中添加任何用户权限吗。你能确认一下吗。我也需要在ios中工作。
    • 请提供在tiapp.xml中提及的用户权限
    • 位置提供商的广播接收器不工作。请您确认一下
    【解决方案2】:

    如果您收听位置转换器 Ti.Geolocation.addEventListener('location',function(response){}); 当位置改变和 GPS 开启或关闭时,你有一个事件

    当你关闭你的 GPS 时,你的反应是

    在安卓上:

     {
      "type": "location",
      "source": {
        "bubbleParent": true,
        "hasCompass": true,
        "__propertiesDefined__": true,
        "preferredProvider": "gps",
        "Android": {
          "bubbleParent": true,
          "manualMode": false,
          "apiName": "Ti.Geolocation.Android"
        },
        "lastGeolocation": "{\"latitude\":36.4880079,\"longitude\":2.8170365,\"altitude\":0,\"accuracy\":42.5,\"heading\":0,\"speed\":0,\"timestamp\":1488278209059}",
        "_events": {
          "location": {}
        },
        "locationServicesEnabled": false,
        "accuracy": 1,
        "apiName": "Ti.Geolocation"
      },
      "bubbles": false,
      "success": true,
      "code": 0,
      "error": "network is disabled",
      "cancelBubble": false
    }
    

    在 iOS 上

    {
      "bubbles": true,
      "type": "location",
      "source": {
        "preferredProvider": null
      },
      "success": false,
      "code": 1,
      "error": "location",
      "cancelBubble": false
    }
    

    iOS 上的事件在应用程序恢复时触发,在我的情况下,我在应用程序处于后台时跟踪 GPS,将其添加到 tiapp.xml

    <key>UIBackgroundModes</key>
    <array>
         <string>location</string>
    </array>
    

    你可以找到gps状态 对于response.source.locationServicesEnabled中的android 如果response.error,则在iOS上检查状态或权限

    if(response.error){
       if(!Ti.Geolocation.locationServicesEnabled){
          //GPS desactivate
       }else if(Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS) || Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE)){
          //no authorised
      }
    }
    

    如果您想强制更改图标颜色,即使您没有位置事件,请在包含图标的窗口的 onFocus 事件中检查 GPS 状态和自动功能

    【讨论】:

    • Ti.Geolocation.addEventListener('location',function(response){});当我们关闭 GPS 时,事件本身不会触发。请您检查一下我们关闭 gps 时是否触发了此事件
    • 我重新检查并完成我的回复。事件被触发
    • 您可能想在 android 上使用手动模式(例如 github.com/m1ga/titanium-libraries/blob/master/geo.js#L34)并设置 minAge。如果我没记错的话,当您在打开 GPS 时有缓存值时,它不会调用 location 事件,直到您的位置发生变化。
    • 当我在 gps 上时,此事件完美调用,但在我关闭 gps 时无法正常工作。
    • 你在测试什么平台,你用的是什么 TiSDK,我有这个响应 TiSDK 5.5.1
    猜你喜欢
    • 2015-06-12
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2016-04-04
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多