0

我创建了一个启动服务的插件,这工作正常。但是我希望能够从插件向正在运行的服务发送变量,并从服务中获取变量。我研究了广播/接收器和绑定,但无法获得任何使用我在下面使用的代码结构的示例。有没有人有任何提示?我是 android 开发的新手,对 Java 很陌生(但不是编程),所以我还没有完全实现概念上的飞跃。


插入

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);
    }
}


服务

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);
        }

    }
}
4

2 回答 2

1

我遇到了完全相同的问题:为 Salesforce Mobile SDK 编写插件(基于 Cordova 2.3.0)。

我的案例:插件和应用程序是不同的 android 项目。

解决方案是您必须发布主(应用程序)项目的内容ServiceAndroidManifest.xml请记住使用完整的限定路径对其进行签名并导出,如下所示:

<service
    android:name="full.qualified.path.to.Service"
    android:exported="true">
</service>
于 2013-06-05T09:35:34.647 回答
0

我设法创建了一个在这个项目中使用的工作插件: https ://github.com/opensystemsassociation/southendtransportresearch/tree/master/phonegap

代码并不整洁,因为它仍在开发中,但“hacky”方法工作得很好,所以希望它会对沿线的人有所帮助

于 2013-06-12T19:02:05.523 回答