Android Question Interact with Java code

LuigiTasca

Member
Licensed User
Longtime User
Hello,
I'm integrating with a third part android service. The interaction is easy. I call the service with an intent passing a bitmap.

The problem is that in the second part of the integration I have to intercept the successful of the call.

They asked me to add this class to my source that I implemented in this way:

B4X:
#if java
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Parcel;
    import android.os.ResultReceiver;
    import android.media.session.MediaSession.*;
    import android.content.Intent;
    import anywheresoftware.b4a.objects.IntentWrapper;
    public class PrinterResultReceiver extends ResultReceiver{
        private Receiver receiver;
        public PrinterResultReceiver(Receiver r) {
            super(new Handler() );
            this.receiver = r;      
        }
        //public interface Receiver {
        public interface Receiver {
            void onReceivePrinterResult(int resultCode);          
        }
        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            if(null!=this.receiver){
                this.receiver.onReceivePrinterResult(resultCode);
            }
        }
        public ResultReceiver asResultReceiver() {
            Parcel parcel = Parcel.obtain();
            this.writeToParcel(parcel,0);
            parcel.setDataPosition(0);      
            ResultReceiver = receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
            parcel.recycle();
            return receiverForSending;
        }
    }
#End If

But I get this error if I try to run it:

B4A line: 12
End Sub
javac 1.8.0_144
src\com\GenialFood\POS\paxprintservice.java:91: error: Illegal static declaration in inner class paxprintservice.PrinterResultReceiver
public interface Receiver {
^
modifier 'static' is only allowed in constant variable declarations


Then I have to implement this method (That I'll use to know if the service successifully printed my bitmap)

B4X:
public void onReceivePrinterResult(int result){
'my conditions
}

How can I interact with the java code? Can somebody help me, please?
 

LuigiTasca

Member
Licensed User
Longtime User
@Erel thank you for the reply, it works. How you wrote the code has others errors but in this moment I have problems with the first part of the integration too.

I have to pass a bitmap to a service with put extra. I have the android studio code but I cannot understand how turn it in b4a code. I don't know what methods I have to use instead of the initializion and the "setPackage".

This is the Android Studio code:

B4X:
private void DoPrint(){
Bitmap icon = this.getBitmap();
byte[] bytes = this.convertBitmapToByteArray(icon, Bitmap.CompressFormat.PNG);
//Create intent
Intent intent = new Intent("it.getyourbill.gybpaxmiddleware.PrintService");
intent.setPackage("it.getyourbill.gybpaxmiddleware");
//send image
intent.putExtra("IMAGE", bytes);
//start the print
startService(intent);

for the "putExtra" I did this in b4a:

B4X:
OUT.InitializeToBytesArray(100)       
Bmp.WriteToStream(OUT,100,"PNG")   
intnt.PutExtra("IMAGE",OUT.ToBytesArray)



Can you please show me?
 
Upvote 0

LuigiTasca

Member
Licensed User
Longtime User
Convert bitmap to bytes: https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#post-445167

B4X:
Dim jo As JavaObject = intent
jo.RunMethod("setPackage", Array("it..."))

B4X:
Dim intnt As Intent
intnt.Initialize(intnt.ACTION_SEND, "it.getyourbill.gybpaxmiddleware.PrintService")
Dim jo As JavaObject = intnt
jo.RunMethod("setPackage", Array("it.getyourbill.gybpaxmiddleware"))
           
intnt.PutExtra("IMAGE",ImageToBytes(Bmp))
StartService(intnt)

In this way? I'm sorry, but I am going crazy
 
Upvote 0

LuigiTasca

Member
Licensed User
Longtime User
Because with logcat I got this:
Unable to start service Intent { act=android.intent.action.SEND dat=it.getyourbill.gybpaxmiddleware.PrintService pkg=it.getyourbill.gybpaxmiddleware (has extras) } U=0: not found
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
B4X:
Dim jo As JavaObject
    jo.InitializeContext
    Dim in As Intent = jo.RunMethod("getIntent",Null)
    Dim jo As JavaObject = in
    jo.RunMethod("setPackage", Array("it.getyourbill.gybpaxmiddleware"))
    in.PutExtra("IMAGE",ImageToBytes(Bmp))
    StartService(in)

#If Java
import android.content.Intent;
public Intent getIntent() {
Intent intent = new Intent("it.getyourbill.gybpaxmiddleware.PrintService");
return intent;
}
#End If
 
Upvote 0

LuigiTasca

Member
Licensed User
Longtime User
B4X:
Dim jo As JavaObject
    jo.InitializeContext
    Dim in As Intent = jo.RunMethod("getIntent",Null)
    Dim jo As JavaObject = in
    jo.RunMethod("setPackage", Array("it.getyourbill.gybpaxmiddleware"))
    in.PutExtra("IMAGE",ImageToBytes(Bmp))
    StartService(in)

#If Java
import android.content.Intent;
public Intent getIntent() {
Intent intent = new Intent("it.getyourbill.gybpaxmiddleware.PrintService");
return intent;
}
#End If

Nothing to do...

W/ActivityManager( 655): Unable to start service Intent { act=android.intent.action.VIEW dat=it.getyourbill.gybpaxmiddleware.PrintService pkg=it.getyourbill.gybpaxmiddleware (has extras) } U=0: not found
 
Upvote 0
Top