我不是一個JS /科爾多瓦開發人員,我是Android開發人員。一旦我編寫了一個Cordova插件,遇到了一些問題並就此進行了一些調查。
keepRunning標誌的一般目的是表明,如果當應用程序被暫停JS定時器必須停止(去背景)。回答你的問題:不,它不會創建任何新的服務。現有的設計在這方面很簡單。
的keepRunning標誌在CordovaActivity.java定義如下:
// Keep app running when pause is received. (default = true)
// If true, then the JavaScript and native code continue to run in the background
// when another application (activity) is started.
protected boolean keepRunning = true;
它的主要目的是禁用JS定時器當科爾多瓦程序是暫停,在CordovaWebView.java:
public void handlePause(boolean keepRunning)
{
LOG.d(TAG, "Handle the pause");
// Send pause event to JavaScript
this.loadUrl("javascript:try{cordova.fireDocumentEvent('pause');}catch(e){console.log('exception firing pause event from native');};");
// Forward to plugins
if (this.pluginManager != null) {
this.pluginManager.onPause(keepRunning);
}
// If app doesn't want to run in background
if (!keepRunning) {
// Pause JavaScript timers (including setInterval)
this.pauseTimers();
}
paused = true;
}
請注意,插件也通過PluginManager,因此理論上它們可以處理應用程序中止的事件,以停止(或不)在後臺活動,具體取決於keepRunning標誌。
在我來說,我有一個問題/錯誤時keepRunning是真正,但JS計時器被停止反正。它的發生是因爲存在與該標誌的附加功能,在CordovaActivity.java:
/**
* Launch an activity for which you would like a result when it finished. When this activity exits,
* your onActivityResult() method will be called.
*
* @param command The command object
* @param intent The intent to start
* @param requestCode The request code that is passed to callback to identify the activity
*/
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
this.activityResultCallback = command;
this.activityResultKeepRunning = this.keepRunning;
// If multitasking turned on, then disable it for activities that return results
if (command != null) {
this.keepRunning = false;
}
// Start activity
super.startActivityForResult(intent, requestCode);
}
當科爾多瓦應用程序啓動另一個Android活動,主要活動科爾多瓦(屏幕的WebView)被切換到後臺,因此被暫停。在我的情況下,它是通過Google地圖插件製作的,它通過Cordova應用程序啓動了GM屏幕。
上面的代碼關閉keepRunning標誌,這意味着當被調用的活動出現時(無論在CordovaActivity.onPause方法中),無論keepRunning是true還是false,JS定時器都會停止!
它看起來像一種伎倆實施一些不清楚(而沒有記錄)的目的,我不知道它的上下文。在我的情況下,它造成了一個錯誤,我剛剛刪除keeprunning處理在startActivityForResult,重新編譯科爾多瓦,它的工作正常。
添加:關於使用GPS服務 - 你說得很對,我同意。作爲一名具有相關(GPS)經驗的Android開發人員,我可以說正確的方法(也可能是唯一可以接受的)就是爲此使用服務。據我所知科爾多瓦不提供任何功能,所以我認爲它應該通過一個插件。我的意思是你可以編寫用於GPS功能的原生Android代碼(作爲服務實現)並從JS代碼訪問它。我相信這是科爾多瓦常見的解決方案。
好點,因爲你不需要修改科爾多瓦的代碼。其實我也在其他一些項目中使用了相同的方法,但忘記了這一點。我記得''cordova.startActivityForResult'沒有爲我工作,可能是因爲'keepRunning'的問題,但我沒有調查那個時間,只是切換到直接調用'cordova.getActivity()。startActivityForResult '。 – Mixaz