Android Example Various Utilities created with Inline Java Code

Discussion in 'Tutorials & Examples' started by Johan Schoeman, Apr 26, 2015.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed User

    The attached project (VariousUtilities.zip) has a large number of Bitmap and String Utilities. There is also a utility that lists the sensors of your device in the Log. Three B4A classes are defined:
    1. StringUtilities
    2. BitmapUtilities
    3. SensorUtilities

    All the methods in the classes are based on inline java code with B4A code inside the classes serving as the interface to the java code.

    Results of all methods in classes StringUtilities and SensorUtilities are reported in the Log of the project. For BitmapUtilities - click on the left imageview to cycle through the various Bitmap methods. You can pass different parameters to most of the methods in class BitmapUtilities to change the appearance of an image that is generated with a specific method.

    You can add to the project (complimenting/adding to the existing classes or adding new classes with their own methods supporting the new class - eg DateUtilities, TimeUtilities, etc) and compile it to a library. The project as it is at present was compiled to a library (VariousUtils.zip) and the library files (jar and xml) are included in the /files folder of the project.

    Please note that when you open the B4A project and go to the Bitmap class you might find an error on line 1893. For some reason the "return" statement keeps on reverting to "Return" when the project is opened. Can't figure out why it does so. Just change the "Return" to "return" and the project will compile.

    All of the Java code were found somewhere on the Web - mostly from StackOverflow
     

    Attached Files:

    Mashiane, canalrun, wonder and 7 others like this.
  2. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Have added 20 odd Date/Time Utilities to the project making use of inline java code - found mostly on StackOverflow. It prints the results of the methods in the Date/Time Utilities to the Log. Have also compiled it to a library. Library files (jar and xml) are in the /files folder of the project (although you don't need them as the code is in the classes). Change the naming of the methods so that it perhaps makes better sense to you (in Java Code, Java B4A interface, and in main activity).
     

    Attached Files:

    Jaames and eurojam like this.
  3. Johan Schoeman

    Johan Schoeman Expert Licensed User

    It adds class VibrateUtilities. It will vibrate once if you click on the left imageview (you can set/change the vibration time in Sub Globals ---> Dim duration As Long = 200). Added 4 buttons:
    1. Vibrate a pattern once
    2. Vibrate once for a length of time specified by "duration"
    3. Vibrate a pattern repeatedly
    4. Cancel the vibration activated by (3) above
     

    Attached Files:

    Jaames likes this.
  4. Johan Schoeman

    Johan Schoeman Expert Licensed User

    It adds class BatteryUtilities. It prints to the Log and reports about various states/conditions of the battery. The value reported by EXTRA_TEMPERATURE must be divided by 10 and that of EXTRA_VOLTAGE by 1000. They are indexes 6 and 7 of the returned integer array.
     

    Attached Files:

    Jaames likes this.
  5. Johan Schoeman

    Johan Schoeman Expert Licensed User

    It adds class TorchUtilities. It switches the camera flash into torch mode on/off. Not sure if this will work on all types of devices. Use the last two buttons from the bottom to switch the torch on/off
     

    Attached Files:

    Jaames likes this.
  6. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Added class ContactUtilities that extracts 17 fields of info from each contact on your device and prints them to the Log. Seem to extract the contacts on your Google account as well.....? Probably not the fastest way to do it but it does return the requested info.

    Please feel free to add to this project any other utilities created from inline java code that might be useful - and then please post it in this thread.
     

    Attached Files:

    Last edited: May 3, 2015
    Jaames and cimperia like this.
  7. Jaames

    Jaames Active Member Licensed User

    Awesome. Thanks for sharing!
     
  8. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Added class InstalledUtilities that will list all installed components on your device in the log (returned in the form of a List from the class containing the inline Java code)
     

    Attached Files:

    Jaames, RauchG and cimperia like this.
  9. cimperia

    cimperia Active Member Licensed User

    Will you ever stop? ;)

    Thank you for sharing.
     
  10. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Have added some more inline java code utilities:
    MathUtilities - BubbleSortDecending, BubbleSortAscending, isPrime, sortArrayOfDoublesFromSmallToLarge, sortArrayOfDoublesFromLargeToSmall, ConvertIntToHex, ConvertIntToOct, ConvertIntToBin, getFactorial, locGreatCircleNm, hsGreatCircleNm, hsGreatCircleKm

    ...GreatCircle... calculates the great circle distance via law-of-cosines (loc) and via the Haversine Formula (nautical miles and kilometers)

    NetworkUtilities - getConnectivityStatusString, isOnline, checkWifiMobileStatus, getWifiName, getMobileNetworkName, getGateway

    isOnline could be useful for prepaid. ".....I found it very useful on devices that use prepaid cards. When they run out of money, they have internet access available, but no internet..."

    TelephonyUtilities - getTelephonyInfo

    All of the above methods in the 3 x additional classes reporting results to the log.
     

    Attached Files:

    Last edited: May 16, 2015
  11. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Added listPackages to class InstalledUtilities. It lists all app names, package names, version names, and version codes of apps on your device to the log. Also added DisplayUtilities that reports to the log.

    All code found somewhere on Stackoverflow.
     

    Attached Files:

    Jaames and inakigarm like this.
  12. stari

    stari Active Member Licensed User

    upload_2015-5-23_19-2-59.png upload_2015-5-23_19-2-59.png Hi, i trayed but i get error:
    upload_2015-5-23_19-2-59.png
     
  13. Johan Schoeman

    Johan Schoeman Expert Licensed User

  14. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Added class InfoUtilities that returns an array of Strings with LOTS of info about:
    BUILD Information
    System Properties
    Memory Info
    Display Info
    SystemAvailableFeatures
    SystemSharedLibraryNames

    to the B4A project - and then prints it to the log.
     

    Attached Files:

    Jaames and stari like this.
  15. stari

    stari Active Member Licensed User

    Johan Schoeman likes this.
  16. stari

    stari Active Member Licensed User

    ops, in this new version i get error, i thing is asocciated with InfoUtilities.
    upload_2015-5-24_14-25-48.png
     
  17. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Comment out the part in InfoUtilities that reports on the memory and then see if it returns the remainder of the information. Comment it out like this:

    Code:
    //  cntr = cntr + 1;
    //  mInfo[cntr] = 
    " ";
    //  strInfo += 
    "\n - Memory Info - \n";
    //  cntr = cntr + 
    1;
    //  mInfo[cntr] =  
    " - Memory Info - ";
    //  MemoryInfo memoryInfo = new MemoryInfo();
    //  ActivityManager activityManager = (ActivityManager)BA.applicationContext.getSystemService(Context.ACTIVITY_SERVICE);
    //  activityManager.getMemoryInfo(memoryInfo);
    //  strInfo += 
    "Total Memory : " + memoryInfo.totalMem + "\n"; //API Level 16
    //  cntr = cntr + 
    1;
    //  mInfo[cntr] = 
    "Total Memory : " + memoryInfo.totalMem;
    //  strInfo += 
    "Available Memory : " + memoryInfo.availMem + "\n";
    //  cntr = cntr + 
    1;
    //  mInfo[cntr] = 
    "Available Memory : " + memoryInfo.availMem;
    //  strInfo += 
    "Threshold : " + memoryInfo.threshold + "\n";
    //  cntr = cntr + 
    1;
    //  mInfo[cntr] = 
    "Threshold : " + memoryInfo.threshold;
    //  strInfo += 
    "Low Memory : " + memoryInfo.lowMemory + "\n";
    //  cntr = cntr + 
    1;
    //  mInfo[cntr] = 
    "Low Memory : " + memoryInfo.lowMemory;
    Code runs OK on my S4 mini with Android 4.2.2 but getting the same error on my tablet with Android 4.0.4. The "memory" part of the code only works with API Level >= 16 (Andoid 4.1)
     
  18. stari

    stari Active Member Licensed User

    Hi,
    on my HTC Desire 820 , Android 4.4.4. also i get the same error.
    I commented the memory part, as you say, and things are Ok.
    All in all: i will use your stuf.

    stari from Celje, slovenia

    http://www.zlatnajedra.com
     
    Johan Schoeman likes this.
  19. Johan Schoeman

    Johan Schoeman Expert Licensed User

    If you replace the (blocked) code in post #17 with this:

    Code:
    cntr = cntr + 1;
      mInfo[cntr] = 
    " ";
      strInfo += 
    "\n - Memory Info - \n";
      cntr = cntr + 
    1;
      mInfo[cntr] =  
    " - Memory Info - ";
      MemoryInfo memoryInfo = new MemoryInfo();
      ActivityManager activityManager = (ActivityManager)BA.applicationContext.getSystemService(Context.ACTIVITY_SERVICE);
      activityManager.getMemoryInfo(memoryInfo);
    //  strInfo += 
    "Total Memory : " + memoryInfo.totalMem + "\n"; API Level 16
      cntr = cntr + 
    1;
    //  mInfo[cntr] = 
    "Total Memory : " + memoryInfo.totalMem;
      mInfo[cntr] = 
    "";
      strInfo += 
    "Available Memory : " + memoryInfo.availMem + "\n";
      cntr = cntr + 
    1;
      mInfo[cntr] = 
    "Available Memory : " + memoryInfo.availMem;
      strInfo += 
    "Threshold : " + memoryInfo.threshold + "\n";
      cntr = cntr + 
    1;
      mInfo[cntr] = 
    "Threshold : " + memoryInfo.threshold;
      strInfo += 
    "Low Memory : " + memoryInfo.lowMemory + "\n";
      cntr = cntr + 
    1;
      mInfo[cntr] = 
    "Low Memory : " + memoryInfo.lowMemory;
    then it should report on all 3 the other parameters (i.e Available Memory, Threshold, and Low Memory). Need to figure out the Total Memory part when time allows.
     
    Last edited: May 28, 2015
  20. stari

    stari Active Member Licensed User

    OK, everything works fine at all Android-s. ops, not tested on Froyo, because I do not have any. However, it does not matter.
    Thks, Johan .
     
    Johan Schoeman likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice