Jean
Jean

Reputation: 333

Android Phonegap 3.4: Notify javascript when an AsyncTask is finished

I have the same problem like Peacemoon on Android Phonegap: Notify javascript when an AsyncTask is finished

Well, this works for older PhoneGap versions, but... My problem: This doesn't work on newer PhoneGap Version like PhoneGap 3.4.

The plugin class inherit from CordovaPlugin now and the return type of execute method is boolean now and the parameter is CallbackContext instead int callbackId and so on. How can I realize an asynchron Task now?

My approach:

private CallbackContext myCallbackContext;

@Override
public boolean execute(String action, JSONArray args, 
    final CallbackContext callbackId)
{
    this.myCallbackContext = callbackId;
    if (action.equals("test"))
    {
        Intent intent = new Intent("com.companyname.name.TEST");
        this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);

         // create an empty result, because the asynchronous call can take long
        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        pluginResult.setKeepCallback(true);
        this.myCallbackContext.sendPluginResult(pluginResult);

        this.myCallbackContext.success;

        return true;
    }

public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    if (requestCode == 0)
    {
        if (resultCode == Activity.RESULT_OK)
        {
            Bundle korb = intent.getExtras();
            String osVersion = korb.getString("osVersion");


            JSONObject obj = new JSONObject();
             try
             {
                 obj.put("osVersion", osVersion);
             } catch(JSONException e)
             {
                 // Log
             }
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
             result.setKeepCallback(false);
             this.myCallbackContext.sendPluginResult(result);

             this.myCallbackContext.success(obj);
}

My return to JavaScript is undefined. But it works in my old version with an older PhoneGap version like 1 . How does it work on a newer PhoneGap version?

Upvotes: 3

Views: 1260

Answers (2)

Jean
Jean

Reputation: 333

My MainActivity implements CordovaInterface since newer versions of Cordova/PhoneGap. So it is necessary to implement the method "startActivityForResult". If I called "this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0)", the scope is set on the MainActivity, and the Call of the new intent is going over the method "startActivityForResult" in MainActivity. So, the result comes back to "onActivityResult" in the MainActivity. Here I have to call my method in the plugin:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent){
    Log.d("Debug", "-----> MainActivity: onActivityResult");
    if(this.activityResultCallback != null)
    {
        // this is the solution :)
        this.activityResultCallback.onActivityResult(requestCode, resultCode, intent);
    }
}

Additionally, the intent call work for both:

Intent intent = new Intent(this.cordova.getActivity(), com.companyname.name.TestActivity.class);
if (this.cordova != null) {
    this.cordova.startActivityForResult(this, intent, 0);
    return true;
}

or

Intent intent = new Intent("com.companyname.name.TEST");
if (this.cordova != null) {
    this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);
}

<!--you must add the String to the AndroidManifest.xml-->
<activity
    android:name=".TestActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="de.companyname.name.TEST" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>    

Otherwise (how QuickFix described) in the exute method I have to use 'return true' or 'return false', if an error occured. And in the onActivityResult method I have to use 'myCallbackContext.success(obj);'. Nothing else. I don't need something like 'this.myCallbackContext.sendPluginResult(pluginResult);'.

Upvotes: 0

QuickFix
QuickFix

Reputation: 11721

Here's what what my plugin class for phonegap 3.x looks like:

    public CallbackContext callbackContext;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        this.callbackContext = callbackContext;

        if (action.equals("anAction")) {
            Intent intent = new Intent(this.getActivity(),com.companyname.name.TEST.class);
            if (this.cordova != null) {
                this.cordova.startActivityForResult(this, intent, 0);
                return true;
            }
        }
        return false;
    }

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    switch (requestCode) {
    case 0:
        //retour de la version utilisant le sample de imense alpr
         if(resultCode == android.app.Activity.RESULT_OK){ //0:ok      
             String result=intent.getStringExtra("result"); 
             this.callbackContext.success(result);
         }
         else{
             String message=intent.getStringExtra("result");
             this.callbackContext.error(message);
         }

        break;
    default:
        break;
    }
}

Upvotes: 1

Related Questions