Question

I am new on phonegap and android.

I created a plugin in phonegap and android to call native function using javascrip.

My coode is as below.

plugin/BannerLink.js

var BannerLink = {

    callNativeFunction: function (success, fail, resultType) {
        //alert(resultType);
         return    Cordova.exec( success, fail,
                    "org.apache.cordova.example.BannerLink", 
                    "nativeFunction", 
                    [resultType]);

                    alert(resultType);
    }
};

My html view file

function bannerPressed(link){
    alert(link.rel);
    //window.location.href=link.rel;
    //window.open(link.rel);
    BannerLink.callNativeFunction( nativePluginResultHandler,nativePluginErrorHandler,link.rel );
}
function nativePluginResultHandler (result) {
    alert("SUCCESS: \r\n"+result );
}

function nativePluginErrorHandler (error) {
    alert("ERROR: \r\n"+error );
}

My BannerLink.java file

package org.apache.cordova.example;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

import android.app.AlertDialog;
import android.util.Log;

@SuppressWarnings("deprecation")
public class BannerLink extends Plugin {

    @Override
    public PluginResult execute(String action, JSONArray args, String callbackId)  {
        // TODO Auto-generated method stub

        AlertDialog alertDialog=new AlertDialog.Builder(null).create();
        alertDialog.setTitle("Reset...");
        alertDialog.show();
        Log.d("HelloPlugin", "Hello, this is a native function called from PhoneGap/Cordova!"); 
        //only perform the action if it is the one that should be invoked 
        return new PluginResult(PluginResult.Status.ERROR);
    }

}

My config.xml file

<plugin name="BannerLink" value="org.apache.cordova.example.BannerLink"/>

I am using phonegap 2.0

Please correct me where I have made mistake.

Was it helpful?

Solution

There are a few errors is you JS. "cordova" is lower case not upper case and you just need to provide the plugin name not the full path in the exec method:

var BannerLink = {
    callNativeFunction: function (success, fail, resultType) {
        return    cordova.exec(success, fail,
                    "BannerLink", 
                    "nativeFunction", 
                    [resultType]);
    }
};

The AlertDialog you are trying to show in the Java code needs to be wrapped in a runnable for instance:

    Runnable runnable = new Runnable() {
        public void run() {

            AlertDialog alertDialog=new AlertDialog.Builder(null).create();
            alertDialog.setTitle("Reset...");
            alertDialog.show();
        };
    };
    this.cordova.getActivity().runOnUiThread(runnable);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top