简体   繁体   中英

Phonegap - communicating between plugin and service

I have created a plugin that starts a service and this is working fine. However I wish to be able to send variables to the running service from the plugin, and to get variables out of the service. I have researched broadcast/receivers and binding but haven't be able to to get any examples working with the code structure I am using below. Does anyone have any tips? I'm new to android development and pretty new to Java (but not programming) so there is a conceptual leap that I haven't quite got yet.


Plugin

public class IOIOconnect extends CordovaPlugin {
    private Context thiscontext;
    private Intent ioioService;

    // Handle calls from Javascript
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        // Call from javascript to startup the IOIO service
        if (action.equals("ioioStartup")) {
            this.ioioStartup(callbackContext); 
            return true;
        }
    }

    // Initialise IOIO service (Called from Javascript)
    private void ioioStartup(CallbackContext callbackContext) {
        // Initialise the service variables and start it it up
        thiscontext = this.cordova.getActivity().getApplicationContext();
        ioioService = new Intent(thiscontext, HelloIOIOService.class);
        ioioService.putExtra("loadinterval", 800); // Set LED flash interval
        thiscontext.startService(ioioService);
    }
}


Service

public class HelloIOIOService extends IOIOService {
        private int interval = 100; 

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    // USUAL IOIO SERVICE STUFF
    @Override
    public void onStart(Intent intent, int startId) {  
        // Service has been started
        super.onStart(intent, startId);


        // IOIO When service is started load external vars (if set)
        int loadinterval = intent.getIntExtra("loadinterval", -1);
        if(loadinterval>=0){ interval = loadinterval; }

        // Native IOIO stuff
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (intent != null && intent.getAction() != null && intent.getAction().equals("stop")) {
            // User clicked the notification. Need to stop the service.
            nm.cancel(0);
            stopSelf();
        } else {
            // Service starting. Create a notification.
            Notification notification = new Notification(
                    R.drawable.ic_launcher, "IOIO service running",
                    System.currentTimeMillis());
            notification
                    .setLatestEventInfo(this, "IOIO Service", "Click to stop",
                            PendingIntent.getService(this, 0, new Intent(
                                    "stop", null, this, this.getClass()), 0));
            notification.flags |= Notification.FLAG_ONGOING_EVENT;
            nm.notify(0, notification);
        }

    }
}

I had exact same problem: writing plugin for Salesforce Mobile SDK (based on Cordova 2.3.0).

My case: plugin and app is different android projects.

The solutions, is that you have to publish the Service in AndroidManifest.xml of the main (app) project. Remember to sign it with full qualified path and as exported, like this:

<service
    android:name="full.qualified.path.to.Service"
    android:exported="true">
</service>

I managed to create a working plugin which is used in this project: https://github.com/opensystemsassociation/southendtransportresearch/tree/master/phonegap

The code's not tidy as its still in development, but the 'hacky' approach works fine so hopefully it will help someone along the line a bit

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM