Announcement

Collapse
No announcement yet.

PluriBASIC - Adding OS features to Android apps

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PluriBASIC - Adding OS features to Android apps

    Suppose you want to do something that PluriBASIC still does not do natively, but there are examples online... Let's say, sending an SMS.

    All you have to do is....
    • Download at least build 168543 of PluriBASIC (just uploaded), because this example uses it.
    • Search for examples in Google (I found these: https://www.javatpoint.com/how-to-send-sms-in-android but stackoverflow has much more).
    • Copy the relevant portion of code in Java language.
    • Create a function like this in PluriBASIC:

      Code:
      [FONT="Courier New"][B]
      	[COLOR="Blue"]FUNCTION[/COLOR] [COLOR="Black"]SendSMS[/COLOR][COLOR="Black"]([/COLOR][COLOR="Blue"]BYVAL[/COLOR] [COLOR="Black"]PhoneNumber[/COLOR] [COLOR="Blue"]AS[/COLOR] [COLOR="Blue"]STRING[/COLOR][COLOR="Black"],[/COLOR] [COLOR="Blue"]BYVAL[/COLOR] [COLOR="Black"]message[/COLOR] [COLOR="Blue"]AS[/COLOR] [COLOR="Blue"]STRING[/COLOR][COLOR="Black"])[/COLOR] [COLOR="Blue"]AS[/COLOR] [COLOR="Blue"]LONG[/COLOR]
      
      	[COLOR="Blue"]LOCAL[/COLOR] [COLOR="Black"]Result[/COLOR] [COLOR="Blue"]AS[/COLOR] [COLOR="Blue"]LONG[/COLOR]
      
      	[COLOR="Blue"]JAVA[/COLOR] [COLOR="Blue"]START[/COLOR]
      
      
      	[COLOR="Blue"]END[/COLOR] [COLOR="Blue"]JAVA[/COLOR]
      
      	[COLOR="Blue"]FUNCTION[/COLOR] [COLOR="Black"]=[/COLOR] [COLOR="Black"]Result[/COLOR]
      
      	[COLOR="Blue"]END[/COLOR] [COLOR="Blue"]FUNCTION[/COLOR][COLOR="Black"][/COLOR][/B][/FONT]
    • Next, paste the code you copied from the online example inside the JAVA tags... like this:

      Code:
      [FONT="Courier New"][B][COLOR="Blue"]FUNCTION[/COLOR] [COLOR="Black"]SendSMS[/COLOR][COLOR="Black"]([/COLOR][COLOR="Blue"]BYVAL[/COLOR] [COLOR="Black"]PhoneNumber[/COLOR] [COLOR="Blue"]AS[/COLOR] [COLOR="Blue"]STRING[/COLOR][COLOR="Black"],[/COLOR] [COLOR="Blue"]BYVAL[/COLOR] [COLOR="Black"]message[/COLOR] [COLOR="Blue"]AS[/COLOR] [COLOR="Blue"]STRING[/COLOR][COLOR="Black"])[/COLOR] [COLOR="Blue"]AS[/COLOR] [COLOR="Blue"]LONG[/COLOR]
      
      	[COLOR="Blue"]LOCAL[/COLOR] [COLOR="Black"]Result[/COLOR] [COLOR="Blue"]AS[/COLOR] [COLOR="Blue"]LONG[/COLOR]
      
      	[COLOR="Blue"]#IMPORTS[/COLOR] [COLOR="DarkGreen"]"android.content.Intent"[/COLOR]
      	[COLOR="Blue"]#IMPORTS[/COLOR] [COLOR="DarkGreen"]"android.app.PendingIntent"[/COLOR]
      	[COLOR="Blue"]#IMPORTS[/COLOR] [COLOR="DarkGreen"]"android.telephony.SmsManager"[/COLOR]
      	[COLOR="Blue"]#PERMITS[/COLOR] [COLOR="DarkGreen"]"SEND_SMS"[/COLOR] 
      
      	[COLOR="Blue"]JAVA[/COLOR] [COLOR="Blue"]START[/COLOR]
      
      	[COLOR="Blue"]try[/COLOR] 
      	  [COLOR="Black"]{[/COLOR]
      	    [COLOR="Black"]//[/COLOR][COLOR="Black"]Getting[/COLOR] [COLOR="Black"]intent[/COLOR] [COLOR="Black"]and[/COLOR] [COLOR="Black"]PendingIntent[/COLOR] [COLOR="Black"]instance[/COLOR]  
      	    [COLOR="Black"]Intent[/COLOR] [COLOR="Black"]intent[/COLOR] [COLOR="Black"]=[/COLOR] [COLOR="Blue"]new[/COLOR] [COLOR="Black"]Intent[/COLOR][COLOR="Black"]([/COLOR][COLOR="Black"]getApplicationContext[/COLOR][COLOR="Black"]([/COLOR][COLOR="Black"])[/COLOR][COLOR="Black"],[/COLOR] [COLOR="Black"]{[[[/COLOR][COLOR="Black"]appname[/COLOR][COLOR="Black"]]]}[/COLOR][COLOR="Black"].class[/COLOR][COLOR="Black"])[/COLOR][COLOR="Black"];[/COLOR]  
      	    [COLOR="Black"]PendingIntent[/COLOR] [COLOR="Blue"]pi[/COLOR] [COLOR="Black"]=[/COLOR] [COLOR="Black"]PendingIntent.getActivity[/COLOR][COLOR="Black"]([/COLOR][COLOR="Black"]getApplicationContext[/COLOR][COLOR="Black"]([/COLOR][COLOR="Black"])[/COLOR][COLOR="Black"],[/COLOR] [COLOR="Black"]0[/COLOR][COLOR="Black"],[/COLOR] [COLOR="Black"]intent[/COLOR][COLOR="Black"],[/COLOR][COLOR="Black"]0[/COLOR][COLOR="Black"])[/COLOR][COLOR="Black"];[/COLOR]  
      
      	    [COLOR="Black"]//[/COLOR][COLOR="Black"]GET[/COLOR] [COLOR="Black"]the[/COLOR] [COLOR="Black"]SmsManager[/COLOR] [COLOR="Black"]instance[/COLOR] [COLOR="Black"]and[/COLOR] [COLOR="Black"]call[/COLOR] [COLOR="Black"]the[/COLOR] [COLOR="Black"]sendTextMessage[/COLOR] [COLOR="Black"]method[/COLOR] [COLOR="Black"]to[/COLOR] [COLOR="Black"]send[/COLOR] [COLOR="Black"]message[/COLOR]  
      	    [COLOR="Black"]SmsManager[/COLOR] [COLOR="Black"]sms[/COLOR] [COLOR="Black"]=[/COLOR] [COLOR="Black"]SmsManager.getDefault[/COLOR][COLOR="Black"]([/COLOR][COLOR="Black"])[/COLOR][COLOR="Black"];[/COLOR]  
      	    [COLOR="Black"]sms.sendTextMessage[/COLOR][COLOR="Black"]([/COLOR][COLOR="Black"]phonenumber[/COLOR][COLOR="Black"][[/COLOR][COLOR="Black"]0[/COLOR][COLOR="Black"]][/COLOR][COLOR="Black"],[/COLOR] [COLOR="Black"]null[/COLOR][COLOR="Black"],[/COLOR] [COLOR="Black"]message[/COLOR][COLOR="Black"][[/COLOR][COLOR="Black"]0[/COLOR][COLOR="Black"]][/COLOR][COLOR="Black"],[/COLOR] [COLOR="Blue"]pi[/COLOR][COLOR="Black"],[/COLOR] [COLOR="Black"]null[/COLOR][COLOR="Black"])[/COLOR][COLOR="Black"];[/COLOR]
      
      	    [COLOR="Black"]result[/COLOR][COLOR="Black"][[/COLOR][COLOR="Black"]0[/COLOR][COLOR="Black"]][/COLOR] [COLOR="Black"]=[/COLOR] [COLOR="Black"]-[/COLOR][COLOR="Black"]1[/COLOR][COLOR="Black"];[/COLOR]  
      	  [COLOR="Black"]}[/COLOR] [COLOR="Blue"]catch[/COLOR] [COLOR="Black"]([/COLOR][COLOR="Black"]Exception[/COLOR] [COLOR="Black"]ee[/COLOR][COLOR="Black"])[/COLOR] [COLOR="Black"]{}[/COLOR] 
      
      	[COLOR="Blue"]END[/COLOR] [COLOR="Blue"]JAVA[/COLOR]
      
      	[COLOR="Blue"]FUNCTION[/COLOR] [COLOR="Black"]=[/COLOR] [COLOR="Black"]Result[/COLOR]
      
      	[COLOR="Blue"]END[/COLOR] [COLOR="Blue"]FUNCTION[/COLOR][COLOR="Black"][/COLOR][/B][/FONT]
      Notice that i replaced the hard coded class name with {[[appname]]}, that will be replaced to whatever the class is named, based in the filename you save your code with. It is important that whenever you refer to the app class, you use this instead of a hard coded name, because this way it will work in whatever other projects you use this function in.

      Also notice that the variables for the phone number and the message where replaced, in BASIC the character case doesn't matter but when pasted into JAVA tags the lowercase is important. Also notice that they are not variables, but arrays. This is because PluriBASIC can use them as BYVAL parameters and variables cannot be used BYVAL in Java, but arrays can.
    • Check wich classes does the snippet of code uses, and import them into your code. This is done with the #IMPORTS meta-statement followed by the class name with path. It is easy to find wich classes are required if you google, for example "Android SmsManager", the relevant page is usually the first one listed.
    • Some portions of code requires special permissions from the user (in this case SEND_SMS), you can add the permission name in the #PERMITS meta-statement. The application will be compiled automatically requiring this permission.

      For older versions of the Android OS, the permissions are given at install-time, but for newer versions it is requested at run-time. This will usually work fine when testing and when a user downloads your app from the Play Store, but if you pass it to someone else to install via internet, the Android OS may behave weird, denying permissions. This is usually fixed by doing the installation via local testing or via PlayStore.
    That's it. From now on you can use your function in PluriBASIC like usual. Be aware that Java is very annoying and some times a lot of qork arounds are required for different versions of the Android OS. What works fine on one device, could fail on another but there are usually workarounds.

    Having this kind of functions is very useful, because otherwise you have to remember A LOT of workarounds for Java code that you find online. The code i pasted above is untested, you can try it to see what happens.


    www.patreon.com/pluribasic
Working...
X