我正在开发具有某些位置跟踪功能的 Cordova Android/iOS 应用程序。 为了不断更新位置,我使用了后台地理定位插件 ( https://github.com/mauron85/cordova-plugin-background-geolocation )。
这个插件创建一个监听系统 LocationManager 的服务。 当系统获得位置更新时,插件会触发事件链/一些计算,直到它到达我在我的 cordova 应用程序中使用的最终 JavaScript 回调。
它在 iOS 上运行良好,永远不会被杀死,但我在 Android 上遇到了问题,当你让手机进入休眠状态(锁定屏幕)而没有在前台运行该应用程序(假设你切换到另一个应用程序锁定前)。 如果您移动(并因此获得位置更新),该过程将继续运行。但是如果你静止不动超过 5 分钟,后台定位服务就会停止,并且不会有更多的位置传递给我的 JS 回调。 此后,如果您再次移动,该服务将保持停止状态,直到您解锁屏幕并将该应用程序调到前台。
这很烦人,因为我的应用程序完全基于这种走走停停模式,需要在锁定屏幕(手机放在口袋里)下运行,并且我的服务器会检查位置的时间戳以过滤哪些人被认为在线和活跃有一个新的位置,谁不是。 所以我基本上需要两件事:能够在用户再次移动时重新启动位置跟踪,并在用户空闲时继续向我的服务器发送一些位置。
根据我的研究,我最终创建了一个 hibernate 60 秒并循环返回的线程,每分钟调用我的后台服务的句柄位置函数(将位置发送到前端 JS)。 不幸的是,我是 Cordova 插件的新手,没能成功。我在尝试访问应用程序上下文的 systemService 时遇到空指针异常:
E/AndroidRuntime( 4627): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
E/AndroidRuntime( 4627): at android.content.ContextWrapper.getSystemService(ContextWrapper.java:583)
E/AndroidRuntime( 4627): at com.tenforwardconsulting.cordova.bgloc.ForcedUpdateLocation.handleLocationFromOutside(ForcedUpdateLocation.java:12)
E/AndroidRuntime( 4627): at com.tenforwardconsulting.cordova.bgloc.BackgroundGeolocationPlugin$3.run(BackgroundGeolocationPlugin.java:266)
E/AndroidRuntime( 4627): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime( 4627): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime( 4627): at java.lang.Thread.run(Thread.java:818)
插件的启动函数:
public ComponentName startBackgroundService () {
if (isEnabled) { return null; }
Class serviceProviderClass = null;
try {
serviceProviderClass = ServiceProvider.getClass(config.getServiceProvider());
} catch (ClassNotFoundException e) {
callbackContext.error("Configuration error: provider not found");
return null;
}
Activity activity = this.cordova.getActivity();
Log.d(TAG, "Starting bg service");
registerActionReceiver();
locationServiceIntent = new Intent(activity, serviceProviderClass);
locationServiceIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
// locationServiceIntent.putExtra("config", config.toParcel().marshall());
locationServiceIntent.putExtra("config", config);
isEnabled = true;
//Start a thread to send a position every 60s
final ForcedUpdateLocation ful = new ForcedUpdateLocation();
cordova.getThreadPool().execute(new Runnable(){
public void run(){
try {
while(isEnabled){
Thread.sleep(3000);
Log.d(TAG, "Force location sent");
ful.handleLocationFromOutside();
}
} catch (InterruptedException e){
Log.d(TAG, "EXCEPTION ! Force location sent", e);
}
}
});
return activity.startService(locationServiceIntent);
}
我的 stub “ForcedUpdateLocation”服务扩展了插件的 LocationService(调用 handleLocation):
package com.tenforwardconsulting.cordova.bgloc;
import android.location.Location;
import android.content.Context;
import android.location.LocationManager;
public class ForcedUpdateLocation extends com.tenforwardconsulting.cordova.bgloc.AbstractLocationService {
public void handleLocationFromOutside (){
LocationManager locationMangerWakeThread = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationMangerWakeThread.getLastKnownLocation(locationMangerWakeThread.GPS_PROVIDER);
handleLocation(location);
}
@Override
protected void cleanUp(){
//Stub
};
@Override
protected void startRecording(){
//Duh
};
@Override
protected void stopRecording(){
//Wouf ?
};
}
除了我坚持这个事实之外,我发现这个解决方案真的很难看......
那么保持此服务运行的最佳方法是什么?是否有在后台保持长时间运行的空闲进程的最佳做法?
非常感谢
最佳答案
尝试使用此插件的“stopOnTerminate”和“startOnBoot”选项。 这将帮助您在后台持续获取更新,即使应用程序已被操作系统终止。
另请记住,您的 callbk 函数不会始终执行,因为如果您的应用程序被系统终止,这将终止您的 Activity 和您的 callbk。 因此,请尝试使用插件的 url 选项,即使在后台也能继续将您的位置发布到服务器。
关于java - Cordova android 后台插件在 5 分钟后被杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38925148/