Android Tutorial [java] Creating libraries for B4A

hanyelmehy

Active Member
Licensed User
Longtime User
Hi,
i need to creating libraries for (startapp)
according to there document
this code must be written in main class activity:
B4X:
import com.apperhand.device.android.AndroidSDKProvider;
AndroidSDKProvider.initSDK(this);

i make this code but it did not wotk:BangHead:
B4X:
package any.b4a.hanylib;
import android.app.Activity;
import android.content.Context;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.DontInheritEvents;
import anywheresoftware.b4a.objects.ViewWrapper;
import com.apperhand.device.android.AndroidSDKProvider;

@ActivityObject
@Author("HanyElmehy")
@ShortName("HanyLib")
@Version(1)

public class HanyLib {
private BA ba;
  public void Initialize(BA ba)
 {
    AndroidSDKProvider.initSDK(this.ba);
 }
  
  }
any help well be appreciated
Thank you
 

mjas

Member
Licensed User
Longtime User
Documentation

Hello,

I'm starting to build a B4A library.

I use Eclipse and there I have all the Android stuff (Classes, Functions, properties, and so on) very well documented with the JavaDoc provided with the Android SDK's.
But for B4A Core and Shared Libraries I only have the basic guidelines (what is available and the syntax to use all the avaliable stuff) but none is documented.

How to get this libraries (Core and Shared) documented?

Thanks.
 

Firpas

Active Member
Licensed User
Longtime User
With SLC get the following result:

Starting step: Compiling Java code.
javac 1.6.0_45
D:\Eclipse Projects\src\html\to\pdf\HTMLToPdf.java:15: package com.itextpdf.text does not exist
import com.itextpdf.text.Document;
^
1 error

Error.

I send the Eclipse project if you want to take a look.
 

PhilN

Member
Licensed User
Longtime User
Hi All, I am trying to compile library files (.jar and .xml) for an Eclipse project. It is INS (Inertial Navigation System) source code that I found on the internet. I am very inexperienced at compiling libraries as I am a newbie. I have worked through the Flurry library tutorial which is quite simple. This is however much more complicated as there are 4 separate class modules as well as main.java. I thought that it would be really helpful to compile each class module as a separate library that I can include in B4A. I have attached the Eclipse project file with source code if anyone is willing to help. Any help here would be much appreciated. Thanks in advance...:confused:
 

Attachments

  • Demo6DoF-master.zip
    437.3 KB · Views: 890

Nickle

Member
Licensed User
Longtime User
I am trring to complile a library for Phone.SMS that I wrote in eclipes using SmsManager sm, but I want the library to take as input the following (destinationAddress, scAddress,text,null,null)
Any one has already done this can share. My java code in eclipse works for on my Android phone but want to incoperate this in a library for B4A as the current Phone.SMS library only accepts PhoneNo and Text. I am strugling to get the Javadoc to work and the compiler provided is also not working for me.
Please help include in a library or share your library.
B4X:
@ShortName("PhoneSms")
    @Permissions(values={"android.permission.SEND_SMS"})
    public static class PhoneSms {
        /**
        * Sends an Sms message. Note that this method actually sends the message (unlike most other methods that
        *create an intent object).
        */
        public static void Send(String PhoneNumber, String Text) {
            SmsManager sm = SmsManager.getDefault();
            sm.sendTextMessage(PhoneNumber, null, Text, null, null);
        }
    }
 

Avansys

Member
Licensed User
Longtime User
What should do if I want to create a library with a java code but it contains the structure of an android class?, I mean some like this:

B4X:
package avansys.libraries.payments;

import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA.Author;

@ShortName("ANSPayments")
@Version(1.0f)

public class ANSPayPal {
  
    // set to PaymentActivity.ENVIRONMENT_LIVE to move real money.
    // set to PaymentActivity.ENVIRONMENT_SANDBOX to use your test credentials from https://developer.paypal.com
    // set to PaymentActivity.ENVIRONMENT_NO_NETWORK to kick the tires without communicating to PayPal's servers.
    private static final String CONFIG_ENVIRONMENT = PaymentActivity.ENVIRONMENT_NO_NETWORK;
  
    // note that these credentials will differ between live & sandbox environments.
    private static final String CONFIG_CLIENT_ID = "credential from developer.paypal.com";
    // when testing in sandbox, this is likely the -facilitator email address.
    private static final String CONFIG_RECEIVER_EMAIL = "matching paypal email address";
    
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        Intent intent = new Intent(this, PayPalService.class);
      
        intent.putExtra(PaymentActivity.EXTRA_PAYPAL_ENVIRONMENT, CONFIG_ENVIRONMENT);
        intent.putExtra(PaymentActivity.EXTRA_CLIENT_ID, CONFIG_CLIENT_ID);
        intent.putExtra(PaymentActivity.EXTRA_RECEIVER_EMAIL, CONFIG_RECEIVER_EMAIL);
      
        startService(intent);
    }
    public void onBuyPressed(View pressed) {
        PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal("1.75"), "USD", "hipster jeans");
      
        Intent intent = new Intent(this, PaymentActivity.class);
      
        intent.putExtra(PaymentActivity.EXTRA_PAYPAL_ENVIRONMENT, CONFIG_ENVIRONMENT);
        intent.putExtra(PaymentActivity.EXTRA_CLIENT_ID, CONFIG_CLIENT_ID);
        intent.putExtra(PaymentActivity.EXTRA_RECEIVER_EMAIL, CONFIG_RECEIVER_EMAIL);
      
        // It's important to repeat the clientId here so that the SDK has it if Android restarts your
        // app midway through the payment UI flow.
        intent.putExtra(PaymentActivity.EXTRA_CLIENT_ID, "credential-from-developer.paypal.com");
        intent.putExtra(PaymentActivity.EXTRA_PAYER_ID, "your-customer-id-in-your-system");
        intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
      
        startActivityForResult(intent, 0);
    }
  
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {
                    Log.i("paymentExample", confirm.toJSONObject().toString(4));
                    // TODO: send 'confirm' to your server for verification.
                    // see https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
                    // for more details.
                } catch (JSONException e) {
                    Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                }
            }
        }
        else if (resultCode == Activity.RESULT_CANCELED) {
            Log.i("paymentExample", "The user canceled.");
        }
        else if (resultCode == PaymentActivity.RESULT_PAYMENT_INVALID) {
            Log.i("paymentExample", "An invalid payment was submitted. Please see the docs.");
        }
    }
  
    @Override
    public void onDestroy() {
        stopService(new Intent(this, PayPalService.class));
        super.onDestroy();
    }
}

I guess that I can add a library to the project and it's going to add itself to the library, or.. I'm wrong

Or, maybe... Should I pass this code to a java structure and then create the library?

Is there someone can help me?

Thanks
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Paypal, like Parse, do not produce a desktop SDK. You will need to rewrite the library to use either their REST or Classic APIs. On https://developer.paypal.com/ there is a link to their REST APIs page where, if you press the Java button, will show you a link at bottom right to a Java REST SDK on Github.
 

Avansys

Member
Licensed User
Longtime User
Your code subclasses Activity.

It is possible to add a complete activity in a library. However it will be more difficult to use it. A better option is to add the required methods and call them from a B4A activity.

See this tutorial:
Guide - Using onActivityResult
Let me see if I understand you, I'm creating a B4A library, and if I don't wrong, I should create a library that not use the activity's life cycle, there's not problem, but What should do I if I want to execute the INTENT method? Some examples?, it's my unique problem, I don't know how to do it, and I guess I'll have no problems with libraries that I add to B4A library like PayPal library, or Am I wrong?
 
Top