How to run a native (API) function

Monkey Targets Forums/Android/How to run a native (API) function

dragon(Posted 2012) [#1]
Do someone know how can i run a native function from android API?

this one:

http://developer.android.com/reference/android/util/DisplayMetrics.html#density


Midimaster(Posted 2012) [#2]
oh!! this is very interesting for me too! Last wek I asked here, if somebody had heared anything about different dpi's for x and y axis on smartphones. an noww.. your linked page has a link to...

...
public float xdpi
Since: API Level 1

The exact physical pixels per inch of the screen in the X dimension.
public float ydpi
Since: API Level 1

The exact physical pixels per inch of the screen in the Y dimension.
...


which belong to the "android.util.DisplayMetrics" And should be called in Java like this:
DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);



Thank your for this!


Maybe I can help you with another sample, which shows cooperation between Android and Monkey. I am not very firm with Java and somebody learned me how to call simple Parameters. This example asks for the Language on the smartphone:

Only Monkey is necessary:
Import mojo
Extern
	#If TARGET="android"
		Function GetAndroidLanguageID:String() = "Locale.getDefault().getLanguage"
	#Endif

Public

Class MyApp Extends App
     .....
  Method OnRender()
      Cls
      DrawText "Language: " + SystemInfo.GetLanguageID(), 10, 60
    End
End




And I learned last week to combine Java and Monkey, if you have to do more, than fetching one parameter. This example asks for the SystemTimer:

This is monkey:
Import mojo
Import "C:\basic\monkey\MyApp\SystemInfo.java"
Extern
	#If TARGET="android"
Function GetAndroidMillisecs:Int()="SystemPeter.AndroidMilli"
	#Endif

Public

Class MyApp Extends App
     .....
  Method OnRender()
      Cls
      DrawText "Zeit: " + SystemInfo.RealMillisecs(), 10, 60
    End
End




Function Main()
   New Language
End

Class SystemInfo
	Function RealMillisecs:INT()
		#If TARGET="android"
			Return  GetAndroidMillisecs()
		#Else
			Return 0
		#Endif
    End
End


And this is my "SystemInfo.java":
class SystemPeter {
   static int AndroidMilli() {
   		long longms =System.currentTimeMillis();
   		longms = longms % 1000000000;
   		int ms =(int)longms;
      return ms;
   }
}



golomp(Posted 2012) [#3]
Thank you


byo(Posted 2012) [#4]
Thanks for sharing this. Very useful!


Samah(Posted 2012) [#5]
What's with the % 1000000000?


Midimaster(Posted 2012) [#6]
My sample should only demonstrate the cooperation of Java and Monkey. Not important, what's inside the function...

...as I am not very firm in this I did not know how to return the LONG result of System.currentTimeMillis() to Monkey. So I made a MODULO 1,000,000,000 to reduce the result to a INT size. Without this I got error messages, while running the code or the result was negative! I know in your DIDDY module you are able to return the complete value, but I do not understand how!