首先,為了熟悉Android的后臺任務和前臺服務,我已經實作了一個原生的android應用程式。在某些時候,我有一個可以作業的前臺服務通知應用程式 - 很好。
// native Android:
Intent runningIntentService = new Intent(getApplicationContext(), ForegroundService.class);
stopService(runningIntentService);
startService(runningIntentService); // the onCommandStart is being executed.
然后我將所有內容編譯為cordova插件,但最終沒有呼叫服務的onCommandStart方法。在cordova中做什么才能啟動服務?
// Cordova:
try {
Intent runningIntentService = new Intent(cordova.getActivity().getApplicationContext(), ForegroundService.class);
Log.d("test", "#1"); // gets logged
cordova.getActivity().stopService(runningIntentService);
Log.d("test", "#2"); // gets logged
cordova.getActivity().startService(runningIntentService); // the onCommand is not being executed.
Log.d("test", "#3"); // gets logged
} catch (Exception e) {
Log.e("test","Error: " e.toString()); // nothing is catched.
}
uj5u.com熱心網友回復:
我的錯誤是plugin.xml在包的 android 服務名稱中設定了錯誤的路徑:
<service android:name=".ForegroundService"/> <!-- wrong -->
<service android:name="com.package.example.ForegroundService"/> <!-- right -->
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/315410.html
上一篇:為什么沒有生成apk的密鑰
