Skip to Content

In this blog we will write native plugin for Phonegap on Android hwc container.

Useful link

Building Phonegap native plugin for HWC container – IOS

The javascript class

The javacript file will be the same as previos blog

var SupuserinfoAccess = {

     devicename: function(types, success, fail) {

          return Cordova.exec(success, fail, “Supuserinfo”, “getUserinfo”, types);

     }

};

The Native Class


Get the source code of Android HWC container application on Eclipse for Android and create a new class name it as Supuserinfo.

Screen Shot 2013-06-04 at 10.07.05 PM.png

Open Supuserinfo class you just created and add the following code. As you can see it extends the Plugin superclass.


package com.sybase.hwc;

import org.apache.cordova.api.Plugin;

import org.apache.cordova.api.PluginResult;

import org.json.JSONArray;

import com.sybase.messaging.common.ClientConfig;

import com.sybase.messaging.common.persist.Property;

public class Supuserinfo  extends Plugin{

    public static final String FILEPATH = “getUserinfo”;

    @Override

    public PluginResult execute(String action, JSONArray data, String callbackId) {

        // TODO Auto-generated method stub

        if (FILEPATH.equals(action)) {

            String supUserName = “”;

            Property oProp = CustomizationHelper.getInstance().getDefaultConnectionAutoRegistration();

            oProp = CustomizationHelper.getInstance().getDefaultConnectionUserName();

            supUserName = oProp.Value.toString();

            return new PluginResult(PluginResult.Status.OK, supUserName);

        }

        return null;

    }

}

Add your plugin to the Plugin.xml file

On hwc container project go to “res->xml” and open the plugin.xml file

pluginxml.PNG

Add following  xml code to the bottom of the <plugins> tag. Make sure the package name is same as your project.

<plugin name=”Supuserinfo” value=”com.sybase.hwc.Supuserinfo”/>

addxml.PNG

Clean project and build again.

Go to the your Hybrid app project and in custom.js file call our plugin to test.


function customAfterWorkflowLoad() {

    document.addEventListener(“deviceready”, onDeviceReady, true);

}

function onDeviceReady() {

    SupuserinfoAccess.devicename([“x”],onSuccess,onFailure);

}

function onSuccess(name){

    alert(name);

}

function onFailure(error){

}

Deploy your project to the modified HWC container application and you will get registered username as below.

final.PNG

To report this post you need to login first.

12 Comments

You must be Logged on to comment or reply to a post.

  1. Suraj Singh

    Hi Tahir,

    I am very new to Android world. I followed your blog religiously, but I am sure that I have done mistakes and missed some pre-requisites.

    Therefore, please be kind enough to help me.

    (a) I don’t see plugins.xml in the xml folder.

    (b) Supuserinfo Javascript

    I hope I should create this Javascript in SUP HWC in html -> Js . Please provide a snapshot from your project ( if possible)

    Thanks for your help.

    – Suraj

    (0) 
    1. Tahir Öz Post author

      Hi Suraj,

      a- In SUP 2.2. plugin.xml moved into the config.xml.

      1a.PNG

      b- yes, you will add into the HWC project which in html->Js

      1b.PNG

      Regards

      (0) 
      1. Suraj Singh

        Dear Tahir,

        Thank you very much. I encountered a small problem here.

        I am unable to build the application as I got an error for oprop.value. Did you encounter this error as well ?

        – Suraj

        (0) 
          1. Suraj Singh

            Tahir,

            I appreciate your prompt response. Unfortunately, I am unable to succeed yet. Here is what I have done. I will be glad if you can help me further.

            1>New class created and config.xml updated

            2> Javascript and workflow screen

            3> custom.js

            But, when I open the application in HWC, I don’t see anything

            Any thoughts where I might have done wrong.

            Thank you.

            – Suraj

            (0) 
            1. Tahir Öz Post author

              Well it seems okay, have you cleaned and build your android project ?. Have you added the onFailure method in custom.js as well?

              You can also try hwc android  project in debug mode see if it invokes the plugin.

              Tahir

              (0) 
  2. Jitendra Kansal

    Hi Tahir,

    i am trying to implement above scenario.

    just want to understand

    first i create one mbo, deploy to server, create hybrid designer, generate js, html etc class and then add Supuserinfo  js class with the mentioned code.

    after that you mentioned

    The Native Class

    Get the source code of Android HWC container application on Eclipse for Android and create a new class name it as Supuserinfo.

    i didnt understand it? how to get the source code of android hwc container app?

    plz help.

    Rgrds,

    Jitendra

    (0) 
    1. Tahir Öz Post author

      Hi jitendra,

      You can get Android HWC application source code from the path of installation Sybase SDK as,

      C:\<sybasepath>\UnwiredPlatform\MobileSDK213\HybridWeb\Android

      (0) 

Leave a Reply