2015-03-17 8 views
5

Ich habe eine Aktivität namens 'Signature' und ich rufe sie von CordovaPlugin;Rückruf von Aktivität auf Cordova

Plugin.java

public boolean execute(String action, JSONArray args, 
      CallbackContext callbackContext) throws JSONException 
    { 
    Intent i = new Intent(context, Signature.class); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    cordova.startActivityForResult(this,i,90); 
} 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     Log.d(TAG, "activity result in plugin: requestCode(" + requestCode + "), resultCode(" + resultCode + ")"); 
     if(requestCode == 90) { 
      if (resultCode == this.cordova.getActivity().RESULT_OK) { 
       Bundle res = intent.getExtras(); 
       String result = res.getString("results"); 
       Log.d("FIRST", "result:"+result); 
       this.callbackContext 
       .success(result.toString()); 
      } else { 
       this.callbackContext.error("Error"); 
      } 
    } 

Signature.java

private void finishWithResult(String result,int status) 
{ 
    Bundle conData = new Bundle(); 
    conData.putString("results", result); 
    Intent intent = new Intent(); 
    intent.putExtras(conData); 
    setResult(status, intent); 
    finish(); 
} 

jedoch, wenn i "cordova.startActivityForResult" Funktion aufrufen "onActivityResult" ruft sofort selbst ein. Ich kann von Aktivität über finishWithResult nicht zurückrufen. Irgendwelche Ratschläge. Dank

Antwort

6

Zunächst einmal gibt es einige Codes fehlen (return-Anweisung für -Methode ausführen), und Sie haben android/cordova-Plugin zu sagen, zu warten, bis es ein Ergebnis ist, zurück zu Ihrem Webview-App gesendet unter Verwendung von NO_RESULT und setKeepCallback von PluginResult sonst cordova/android erwartet ein Ergebnis, sobald ausführen -Methode beendet hat zu bekommen:

Plugin.java:

public boolean execute(String action, JSONArray args, 
      CallbackContext callbackContext) throws JSONException 
    { 

    PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); 
    r.setKeepCallback(true); 
    callbackContext.sendPluginResult(r); 

    Intent i = new Intent(context, Signature.class); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    cordova.startActivityForResult(this,i,90); 

    return true; 

} 

public void onActivityResult(int requestCode, int resultCode, Intent intent){ 
    // here is your former code 
    ... 
    ... 
    // at last call sendPluginResult 
    this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result.toString())); 

    // when there is no direct result form your execute-method use sendPluginResult because most plugins I saw and made recently (Reminder) prefer sendPluginResult to success/error 
    // this.callbackContext.success(result.toString()); 
} 

Haben Sie ein Beispiel here (für Ihre Plugin-Klasse) und here (für Ihre Signatur-Klasse).

Und eins von mir: here und here.