暂停应用程序(android)后,如何在nativescript中获取用户位置更新?

时间:2019-06-20 03:17:33

标签: javascript android geolocation nativescript

我正在使用插件nativescript-geolocation,并且在尝试暂停应用程序的同时尝试记录用户位置。我正在制作一个导航样式的应用程序,该应用程序需要能够在未打开的情况下监视用户的位置。例如,我开始导航,然后在设备上按“主页”并打开另一个应用程序;我想在后台记录他们的地理位置。

我已尝试关注this,但是该位置仅在应用打开时运行。如果应用程序被挂起,则在移动设备时控制台日志似乎不会发生。我还尝试将位置保存到数组中,然后在恢复应用程序时控制台记录它们,这只会打印出第一个位置段。

background-service.js

const geolocation = require("nativescript-geolocation");
const Accuracy = require("tns-core-modules/ui/enums").Accuracy;
const application = require("tns-core-modules/application");
const device = require("tns-core-modules/platform");

var watchID;

function clearWatch() {
    if (watchID) {
        geolocation.clearWatch(watchID);
        watchID = null;
    }
}

function startWatch() {
    console.log("starting watch??");
    clearWatch();
    watchID = geolocation.watchLocation(
        function (loc) {
            console.log("repeat?");
            if (loc) {
                console.log("Background location: " + loc.latitude + ", " + loc.longitude);
            }
        },
        function (err) {
            console.log(err);
        }, {
            desiredAccuracy: Accuracy.high,
            updateDistance: 5,
            updateTime: 1000
        }
    );
}

application.on(application.exitEvent, clearWatch);

if (application.android) {
    android.app.job.JobService.extend("com.oa.location.BackgroundService26", {
        onStartJob() {
            console.log("service onStartJob");
            startWatch();
            return true;
        },
        onStopJob(jobParams) {
            console.log("service onStopJob");
            this.jobFinished(jobParams, false);
            clearWatch();
            return false;
        },
    });

}

在home-page.js中

 application.on(application.suspendEvent, args => {

      // background recording segment
      if (application.android) {
        var context = utils.ad.getApplicationContext();
        var component = new android.content.ComponentName(context, com.oa.location.BackgroundService26.class);
        var builder = new android.app.job.JobInfo.Builder(1, component);
        builder.setRequiredNetworkType(android.app.job.JobInfo.NETWORK_TYPE_ANY);
        //builder.setPeriodic(30);
        const jobScheduler = context.getSystemService(android.content.Context.JOB_SCHEDULER_SERVICE);
        service = jobScheduler.schedule(builder.build());
        console.log(`Job Scheduled: ${jobScheduler.schedule(builder.build())}`);
        // var intent = new android.content.Intent(context, com.oa.location.BackgroundService26.class);
        // context.startService(intent);
      }

        console.log("suspended");
    });


application.on(application.resumeEvent, args => {
    if (args.android) {
      //geolocation.clearWatch(watchID);
      console.log("resumed");
      // remove background recording
      var context = utils.ad.getApplicationContext();
      const jobScheduler = context.getSystemService(android.content.Context.JOB_SCHEDULER_SERVICE);
      jobScheduler.cancel(service);
      console.log("Canceled " + service);
      service = null;
    }
  });

在AndroidManifest.xml中

<application android:usesCleartextTraffic="true" android:name="com.tns.NativeScriptApplication" android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme">
        <meta-data android:name="firebase_crashlytics_collection_enabled" android:value="true" />
        <service android:name="com.oa.location.BackgroundService26" android:permission="android.permission.BIND_JOB_SERVICE" android:enabled="true" android:exported="false">
        </service>
        <activity android:name="com.tns.NativeScriptActivity" android:label="@string/title_activity_kimera" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode" android:theme="@style/LaunchScreenTheme" android:screenOrientation="portrait">
            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity" />

    </application>

我希望发生的是,当用户按下设备的主页按钮将应用置于暂停模式时,后台服务将运行并收集地理位置更新。

1 个答案:

答案 0 :(得分:0)

由于您只关注concurrent.futures,所以我假设您正在使用Android 8或更高版本进行测试,在这种情况下,我认为您错过了setOverrideDeadline