Java Question Errors while trying to use my first Library

PhillipMorris

Member
Licensed User
Longtime User
Hello,
Here I am trying to build my first library.
I am trying to read TotalRxBytes and TotalTxBytes from TrafficStats.
Following Erel video tutorial, I managed to build both .xml and .jar files.
Copied them to \Libraries folder and tried to use it as follows :
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim f1 As FirstLib
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Log(f1.TotalRxBytes)
End Sub

The class in Eclipse :
B4X:
package mikesoftware.b4a.sample;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@Version(1)
@ShortName("FirstLib")

public class FirstLib {
    public final static int UNSUPPORTED = -1;
   public static native long getMobileTxPackets();
    public static native long getMobileRxPackets();
    
    public static native long getMobileTxBytes();
    public static native long getMobileRxBytes();
    
    public static native long getTotalTxPackets();
    public static native long getTotalRxPackets();
    
    public static native long getTotalTxBytes();
    public static native long getTotalRxBytes();
    
    
    
   public int multiply(int x) {
      return x * 2;
   }
}

and this is error message when I am trying to run the project :
B4X:
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 30)
Log(f1.TotalRxBytes)
java.lang.UnsatisfiedLinkError: Native method not found: mikesoftware.b4a.sample.FirstLib.getTotalRxBytes:()J
   at mikesoftware.b4a.sample.FirstLib.getTotalRxBytes(Native Method)
   at b4a.example.main._activity_create(main.java:243)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at b4a.example.main.afterFirstLayout(main.java:89)
   at b4a.example.main.access$100(main.java:16)
   at b4a.example.main$WaitForLayout.run(main.java:74)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4898)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
   at dalvik.system.NativeStart.main(Native Method)

What am I doing wrong ?
Please help.
 

PhillipMorris

Member
Licensed User
Longtime User
Thanks Erel.
I finally managed to do it.
The correct java code is like this :
B4X:
    public long getMobileTXBytes() {
       return TrafficStats.getMobileTxBytes();
    }

Now I can read Total and Mobile Traffic.
I am assuming that Total - Mobile = WIFI Traffic.
 
Top