如何在 cordova 中 运行 android 前台服务?
how to run an android foreground service in cordova?
首先,为了熟悉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.
}
我的错误是在 plugin.xml
中为包的 android 服务名称设置了错误的路径:
<service android:name=".ForegroundService"/> <!-- wrong -->
<service android:name="com.package.example.ForegroundService"/> <!-- right -->
首先,为了熟悉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.
}
我的错误是在 plugin.xml
中为包的 android 服务名称设置了错误的路径:
<service android:name=".ForegroundService"/> <!-- wrong -->
<service android:name="com.package.example.ForegroundService"/> <!-- right -->