[英]How to get user location updates in nativescript when the app is suspended (android)?
我正在使用插件nativescript-geolocation,並且試圖在應用程序暫停期間記錄用戶位置。 我正在制作一個導航樣式的應用程序,該應用程序需要能夠在未打開的情況下監視用戶的位置。 例如,我開始導航,然后在設備上按“主頁”並打開另一個應用程序; 我想在后台記錄他們的地理位置。
我嘗試按照此方法操作 ,但是該位置僅在應用程序打開時運行。 如果應用程序被掛起,則在移動設備時控制台日志似乎不會發生。 我還嘗試將位置保存到數組中,然后在恢復應用程序時控制台記錄它們,這只會打印出第一個位置段。
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>
我希望發生的事情是,當用戶按下設備的主頁按鈕將應用置於暫停模式時,后台服務將運行並收集地理位置更新。
由於您僅關注BackgroundService26
我假設您正在使用Android 8或更高版本進行測試,在這種情況下,我認為您錯過了setOverrideDeadline
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.