Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.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
Android Cordova后台地理定位服务器更新_Android_Cordova_Ionic Framework_Geolocation_Android Intentservice - Fatal编程技术网

Android Cordova后台地理定位服务器更新

Android Cordova后台地理定位服务器更新,android,cordova,ionic-framework,geolocation,android-intentservice,Android,Cordova,Ionic Framework,Geolocation,Android Intentservice,用例:在他登录后立即跟踪他所在的位置,但稍后关闭应用程序 使用插件。 在调试模式下,它显示值,但是,在回调函数中,它不进行服务器调用。 renderMaps函数调用navigator.geolocation.getCurrentPosition document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { // Now safe to use devi

用例:在他登录后立即跟踪他所在的位置,但稍后关闭应用程序

使用插件。 在调试模式下,它显示值,但是,在回调函数中,它不进行服务器调用。 renderMaps函数调用navigator.geolocation.getCurrentPosition

document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        // Now safe to use device APIs
        renderMaps();

        var callbackFn = function(location) {
            var data = 'longitude='+ location.longitude + '&latitude=' + location.latitude + '&id=' + vm.user_id + '&token=' + vm.accessToken;
            window.longitude_sel = location.latitude;
            window.latitude_sel = location.longitude;
            console.log("" + data);
             $.ajax({
                 type: "POST",
                 url: "https://example.com/partner/location",
                 data: data,
                 success: function(response){
                        console.log("RESPONSE" + response);
                      }
                  });
            backgroundGeolocation.finish();
        };

        var failureFn = function(error) {
            console.log('BackgroundGeolocation error');
        };

        // BackgroundGeolocation is highly configurable. See platform specific configuration options
        backgroundGeolocation.configure(callbackFn, failureFn, {
            desiredAccuracy: 5,
            stationaryRadius: 0,
            distanceFilter: 30,
            interval: 60000,
            stopOnTerminate: false,
            startOnBoot: false,
            startForeground: true,
            stopOnStillActivity: false,
            debug: true
        });

        backgroundGeolocation.start();
        console.log("TEST");
    }
尝试使用插件的“url”选项。 不要期望你的回调每次都能工作,因为你的应用程序活动可能会在后台被操作系统杀死,这也会杀死你的回调


除此之外,这项服务应该能够在杀戮中幸存下来,所以如果您使用插件的url选项,您仍然可以在服务器上获取更新,请尝试此解决方案。就我的情况而言,这是可行的:

    $$(document).on('deviceready', function() {



   cordova.plugins.backgroundMode.enable();
   alert('device working!');
   var userId=localStorage.getItem("userId");
   if(userId!=null)
   {
         BackgroundGeolocation.configure({
            locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
            desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
            stationaryRadius: 50,
            distanceFilter: 50,
            notificationTitle: 'Background tracking',
            notificationText: 'enabled',
            debug: true,
            interval: 10000,
            fastestInterval: 5000,
            activitiesInterval: 10000,
            url: 'http://example.com/index.php/locations/savebackgroundlocation',
            httpHeaders: {
              "Authorization": "Basic " + btoa('stack' + ":" + 'pwd@123#')
            },
            // customize post properties
            postTemplate: {
              lat: '@latitude',
              lon: '@longitude'

            }
      });

      BackgroundGeolocation.checkStatus(function(status) {
          BackgroundGeolocation.start(); //triggers start on start event
      });



   }


});

我必须给予访问、背景和位置的许可

在文件plugins/cordova plugin geolocation/plugin.xml中,您需要添加:

   <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

请参阅此处的更多信息: