2013-04-01 20 views
0

我創建了一個啓動服務的插件,並且工作正常。不過,我希望能夠通過插件將變量發送到正在運行的服務,並使變量脫離服務。我研究過廣播/接收器和綁定,但無法獲得任何使用下面使用的代碼結構的示例。有沒有人有任何提示?我對Android開發很陌生,對Java很新穎(但不是編程),所以我還沒有完成一個概念上的飛躍。Phonegap - 插件和服務之間的通信


插件

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

    } 
} 

回答

1

我有完全相同的問題:Salesforce的移動SDK編寫插件(基於科爾多瓦2.3.0 )。

我的情況:插件和應用程序是不同的Android項目。

解決方案是,您必須在主(app)項目的AndroidManifest.xml中發佈Service。請記住,具有完全合格的路徑和出口標誌,就像這樣:

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