I would like to be able to start a service, or have a class run on a background service, but i would like to access the class my cordova plugin.
At the moment, i have something similar to below, which isnt great, but it works. But if the user pushes the app to the back, or closes the app (not the service), then it stops running.
I need the MyHttpServer
to continue running when a user closes the UI, or exits the application.
public class MyCordovaPlugin extends CordovaPlugin {
private static final String TAG = "MyCordovaPlugin";
MyHttpServer httpServer;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (httpServer == null) {
httpServer = new MyHttpServer();
}
if (action.equals("get-service-stats")) {
callbackContext.success(httpServer.getStats());
}
}
}
I know that to run a service in the background, i can use the following code, which i do now for something else and it works, but i cannot access the instance from cordova.
// stop just encase its already started
context.stopService(new Intent(context, HttpServerService.class));
// start service
context.startService(new Intent(context, HttpServerService.class));
Is there a particular way of implementing communicating between a cordova plugin and an adnroid background service?? Lets say for this example that MyHttpServer
as a method on it called getStats
, how can i call that in my cordova plugin if MyHttpServer is running in its own Service
.
So something like this, here is the plugin
public class MyCordovaPlugin extends CordovaPlugin {
private static final String TAG = "MyCordovaPlugin";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("get-service-stats")) {
// CALL HttpServerService.GETSTATS METHOD HERE
}
}
}
Here is the background server
public class HttpServerService extends Service {
private static final String TAG = "HttpServerService";
private MyHttpServer httpServer;
private Context context;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = this.getApplicationContext();
httpServer = new MyHttpServer();
httpServer.start();
return Service.START_STICKY;
}
public string getStats() {
return httpServer.getStats();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
if (httpServer != null)
httpServer.stop();
super.onDestroy();
}
}
Is there a particular way of implementing communicating between a cordova plugin and an adnroid background service?? Lets say for this example that MyHttpServer as a method on it called getStats, how can i call that in my cordova plugin if MyHttpServer is running in its own Service
You can use this Developers guide to implement binding between your activity and service. By doing this you'll be able to call your service's methods from activity.
At the moment, i have something similar to below, which isnt great, but it works. But if the user pushes the app to the back, or closes the app (not the service), then it stops running.
Your service has to be foreground service if you want it runs even you activity will be stopped. You should see this example of implementation of foreground service. You have to know that Oreo has some limitations of background execution.
Sorry for my English
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.