public final class Vm
extends java.lang.Object
This class contains methods to copy arrays, obtain a timestamp, sleep and get platform and version information, among many other things.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ALTERNATIVE_DEBUG
Deprecated.
In Android, it writes things on DebugConsole.txt and on ADB, so this is now useless
|
static boolean |
disableDebug
Set to
true to disable Vm.debug message output. |
static java.lang.String |
ERASE_DEBUG
Pass this to Vm.debug to erase the memo/txt file used to store the output.
|
static int[] |
keysBeingIntercepted
Internal use only.
|
static int |
TWEAK_AUDIBLE_GC
Each time the garbage collector runs, it will issue a beep.
|
static int |
TWEAK_DISABLE_GC
Disables the Garbage Collector.
|
static int |
TWEAK_DUMP_MEM_STATS
When the program ends, it will dump the memory status of the program with the following information:
Times gc was called: number of times the garbage collector was called.
|
static int |
TWEAK_MEM_PROFILER
Computes the maximum memory in use between two consecutive calls.
|
static int |
TWEAK_TRACE_CREATED_CLASSOBJS
This flag turns on the trace of created class objects.
|
static int |
TWEAK_TRACE_LOCKED_OBJS
This flag turns on the trace of locked objects, which are objects that are locked by the vm and will
never be released.
|
static int |
TWEAK_TRACE_METHODS
Enables dump of executed methods to the console.
|
static int |
TWEAK_TRACE_OBJECTS_LEFT_BETWEEN_2_GCS
This flag turns on the trace objects that are left behind between two garbage collector calls.
|
Modifier and Type | Method and Description |
---|---|
static void |
alert(java.lang.String s)
Shows an alert IMMEDIATELY on screen.
|
static boolean |
arrayCopy(java.lang.Object srcArray,
int srcStart,
java.lang.Object dstArray,
int dstStart,
int length)
Copies the elements of one array to another array.
|
static boolean |
attachLibrary(java.lang.String name)
Deprecated.
This method never worked in TotalCross. Previously, in SuperWaba, all class and resources were placed in a PDB file,
so attaching a pdb file was ok. However, in TotalCross, everything is placed in TCZ files, so attaching a PDB is useless.
To load an image that you saved with Image.saveTo, use Image.loadFrom.
|
static boolean |
attachNativeLibrary(java.lang.String name)
Attaches a native library to this application instance (only works at device).
|
static void |
clipboardCopy(java.lang.String s)
Copies the specific string to the clipboard.
|
static java.lang.String |
clipboardPaste()
Gets the last string from the clipboard.
|
static void |
debug(java.lang.String s)
Sends a text, preceeded with the current time stamp and followed by a line feed, to:
Windows CE / 32: the "DebugConsole.txt" file at the current application's folder
Blackberry: the event logger, which can be accessed using alt+LGLG
iPhone: the "DebugConsole.txt" file in the application's folder.
|
static int |
exec(java.lang.String command,
java.lang.String args,
int launchCode,
boolean wait)
Executes a command.
|
static void |
exitAndReboot()
Reboots the device.
|
static void |
gc()
Calls the Garbage Collector.
|
static byte[] |
getFile(java.lang.String name)
Returns a byte array of a file contained in the same tcz where this one resides, or in some attached library (or JAR,
if application is running in a browser).
|
static int |
getFreeMemory()
Returns the free memory in the device.
|
static int |
getRemainingBattery()
Returns the percentage of the remaining battery life of this device.
|
static java.lang.String |
getStackTrace()
A shortcut for
try {throw new Exception();} catch (Exception e) {return getStackTrace(e);} |
static java.lang.String |
getStackTrace(java.lang.Throwable t)
Returns the given throwable stack trace as a String.
|
static int |
getTimeStamp()
Returns a time stamp in milliseconds.
|
static int |
identityHashCode(java.lang.Object object)
Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class
overrides
hashCode() . |
static void |
interceptSpecialKeys(int[] keys)
Specify which special keys to intercept; use the constants available in SpecialKeys or pass device-specific
constants, which can be retrieved calling Vm.showKeyCodes(true).
|
static boolean |
isKeyDown(int key)
Gets the current keys being pressed.
|
static void |
preallocateArray(java.lang.Object sample,
int length)
This method pre-allocates space for an array of objects with the given length.
|
static void |
printStackTrace()
Prints the stack trace to the debug console file.
|
static void |
printStackTrace(java.lang.String msg)
Prints the stack trace to the debug console file with the given message.
|
static void |
safeSleep(int millis)
Causes the VM (or the current thread, if called from it) to pause execution for aproximately the given number
of milliseconds.
|
static void |
setAutoOff(boolean enabled)
Enables or disables device automatic turn-off, which happens after a period of inactivity of the device.
|
static void |
setTime(Time t)
Deprecated.
It won't work on most devices.
|
static void |
showKeyCodes(boolean on)
Set to true to popup an alert for each key pressed.
|
static void |
sleep(int millis)
Causes the VM (or the current thread, if called from it) to pause execution for the given number of milliseconds.
|
static boolean |
turnScreenOn(boolean on)
Turns the screen on or off, but keeps the device running.
|
static void |
tweak(int param,
boolean set)
Tweak some parameters of the virtual machine.
|
static void |
vibrate(int millis)
Vibrates the device for the specified number of milliseconds.
|
static void |
warning(java.lang.String s)
This function can be used to show permanent warnings.
|
public static int[] keysBeingIntercepted
public static final java.lang.String ERASE_DEBUG
@Deprecated public static final java.lang.String ALTERNATIVE_DEBUG
public static boolean disableDebug
true
to disable Vm.debug
message output. Note that the method calls will still
remain, and can slowdown your program. Note also that Vm.warning is not affected by this flag.public static final int TWEAK_AUDIBLE_GC
public static final int TWEAK_DUMP_MEM_STATS
public static final int TWEAK_MEM_PROFILER
Vm.debug("P Starting profiler for xxxx"); Vm.tweak(Vm.TWEAK_MEM_PROFILER,true); // now run the program during some time Vm.tweak(Vm.TWEAK_MEM_PROFILER,false); Vm.debug("P Stopping profiler for xxxx");When it starts, it prints the currently used memory. When you set it off, it will dump the currently used memory (if different of the maximum), and then the maximum memory used between the two calls. For example:
P Now allocated: 40852408 // printed when turning on P Now allocated: 1137060 // printed when turning off P Max allocated: 40868844 // printed when turning offAll profiler messages are prefixed with P in the debug console, because it makes easy to filter them. To be used in the tweak method.
public static final int TWEAK_DISABLE_GC
Vm.tweak(TWEAK_DISABLE_GC,true); ... Vm.tweak(TWEAK_DISABLE_GC,false); // the gc is automatically called here
public static final int TWEAK_TRACE_CREATED_CLASSOBJS
public static final int TWEAK_TRACE_LOCKED_OBJS
public static final int TWEAK_TRACE_OBJECTS_LEFT_BETWEEN_2_GCS
public static final int TWEAK_TRACE_METHODS
public static boolean arrayCopy(java.lang.Object srcArray, int srcStart, java.lang.Object dstArray, int dstStart, int length)
srcArray
- the array to copy elements fromsrcStart
- the starting position in the source arraydstArray
- the array to copy elements todstStart
- the starting position in the destination arraylength
- the number of elements to copyjava.lang.ArrayStoreException
- If the source array and destination array are not compatiblepublic static int getTimeStamp()
@Deprecated public static void setTime(Time t)
Settings.timeZone
.
Does not work on Android, which does not allow to do this programatically.public static void exitAndReboot()
public static int exec(java.lang.String command, java.lang.String args, int launchCode, boolean wait)
As an example, the following call could be used to run the command "scandir /p mydir" under Java, Win32 or WinCE:
int result = Vm.exec("scandir", "/p mydir", 0, true);The args parameter passed to this method is the arguments string to pass to the program being executed.
The launchCode parameter was only used under PalmOS. It is no longer used.
The wait parameter passed to this method determines whether to execute the command asynchronously. If false, then the method will return without waiting for the command to complete execution. If true, the method will wait for the program to finish executing and the return value of the method will be the value returned from the application under Java, Win32 and WinCE.
To run another TotalCross program from Windows CE devices, use:
Vm.exec("\\TotalCross\\Path\\Program.exe", null, 0, false);Here's a sample of how to run the Internet Explorer in Windows CE:
Vm.exec("\\windows\\iexplore.exe","about:blank",0,false);You cannot pass null nor "" in the args, or you'll get error 87. IExplore requires a page to open; in this case, we open the blank page. In other words, you must call the program's executable.
When calling a TotalCross program, you can pass a command line parameter to the calling application, just placing
the parameters in the proper argument. It can be retrieved with getCommandLine
method from the
MainWindow.
In some platforms, the caller application must be quit by calling exit
.
In Windows 98 and beyond, if you don't pass the full path to the file, it is searched in the current PATH environment variable. The command must contain only the exe name, and all the other arguments must being the args parameter. For example, the following code uses the CMD to open a file with the proper editor:
Vm.exec("cmd.exe", "/c start C:\\WINNT\\DirectX.log", 0, false);Important notes about path separators:
In Android, you can launch an application, an url at the browser, execute a shell command, or install an apk:
/sdcard
. Be sure that the sdcard is NOT MOUNTED, otherwise your application will not have access to it.
In iOS, the following work as in Android. Note that since iOS does not have a sdcard, you must extract or copy the files to your application's directory (Settings.appPath).
UIWebView
component can show. A PDF reader is NOT required.
command
parameter for Vm.exec is the "Process" description, and the
args
parameter is the activitie's name. Note, however, that there's no guarantee that the program
will be available in a real device.
To install a cab file in Windows Mobile, you can use:
Vm.exec("wceload.exe", fullPathToCabFile, 0, true);However, trying to update the program itself or the vm will close the program. So, update it at last.
command
- the command to executeargs
- command arguments.launchCode
- no longer used.wait
- whether to wait for the command to complete execution before returning. If wait is false,
don't forget to call the exit
method right after this command is called, otherwise the application may
not be called. In Android, if you're calling a TotalCross program, the wait parameter is ignored (and defaults to false).public static void setAutoOff(boolean enabled)
public static void sleep(int millis)
millis
- time to sleep in millisecondspublic static void safeSleep(int millis)
millis
- time to sleep in millisecondspublic static void vibrate(int millis)
public static int getFreeMemory()
public static void gc()
public static void interceptSpecialKeys(int[] keys)
public static boolean isKeyDown(int key)
interceptSpecialKeys(int[])
public static void debug(java.lang.String s)
Vm.debug(Vm.ERASE_DEBUG);
.
Note: if you're debugging a String that contains \0 in it, all chars after the first \0 are ignored.
Settings.nvfsVolume
,
disableDebug
,
ERASE_DEBUG
public static void alert(java.lang.String s)
public static void warning(java.lang.String s)
Vm.debug("Warning! "+s)
, so it differs from it only in a semanthical way.
Note: you cannot use this function with ERASE_DEBUG; it won't work.
public static void clipboardCopy(java.lang.String s)
public static java.lang.String clipboardPaste()
@Deprecated public static boolean attachLibrary(java.lang.String name)
This method is useful if you have a library and want to make its classes available to your app. At the
initialization, TotalCross scans all libraries in the device and automatically attach them. However, if
you downloaded a new library from a server or saved a bitmap into a file, you will need to attach the
file where it is located so you can instantiate its classes or load the image using new Image(filename)
.
After a library is added, its file is blocked and thus cannot be modified.
To see what libraries are in the device, you can use PDBFile.listPDBFiles
or
File.listFiles
and check the files that ends with Lib
A check is made to prevent a library from being loaded twice.
name
- the library's name, with a 'Lib' suffix. Cannot contain a creator neither a type.Image.loadFrom(totalcross.io.PDBFile, java.lang.String)
public static boolean attachNativeLibrary(java.lang.String name)
name
- the library's name. The suffix 'Lib' is not mandatory. Cannot contain a creator neither a type.public static byte[] getFile(java.lang.String name)
byte[] b = Vm.getFile("textfiles/About.txt"); if (b != null) new MessageBox("About", new String(b, 0, b.length)).popup();The TotalCross deployer will insert the .txt file into the program's tcz/pdb.
name
- The name of the file, exactly as shown by tc.Deploy in the output window. In desktop it is case
insensitive, while in device it is CASE SENSITIVE.public static int getRemainingBattery()
public static void tweak(int param, boolean set)
Vm.tweak(Vm.TWEAK_AUDIBLE_GC,true); Vm.tweak(Vm.TWEAK_DUMP_MEM_STATS,true);
TWEAK_AUDIBLE_GC
,
TWEAK_DUMP_MEM_STATS
,
TWEAK_MEM_PROFILER
public static java.lang.String getStackTrace(java.lang.Throwable t)
try { ... } catch (Exception e) { MessageBox.showException(e, false); // last parameter dumps to the console if true }
public static java.lang.String getStackTrace()
try {throw new Exception();} catch (Exception e) {return getStackTrace(e);}
public static void showKeyCodes(boolean on)
public static boolean turnScreenOn(boolean on)
public static void printStackTrace()
try { throw new Exception("Stack trace"); } catch (Exception e) { e.printStackTrace(); }
public static void printStackTrace(java.lang.String msg)
public static void preallocateArray(java.lang.Object sample, int length)
int gcc = Settings.gcCount, gct = Settings.gcTime, ini = Vm.getTimeStamp(); ... run some memory-consuming routine Vm.debug("gcCount: "+(Settings.gcCount-gcc)+" ("+(Settings.gcTime-gct)+"ms), elapsed: "+(Vm.getTimeStamp()-ini)+"ms");This is a sample that pre-allocates space for a ListContainer, taken from the PopupMenu class:
Vm.preallocateArray(new ListContainer.Item(layout), itemCount);This method does nothing under Blackberry and Java SE.
public static int identityHashCode(java.lang.Object object)
hashCode()
.
The hash code for the null
reference is zero.object
- Object for which the hash code is to be calculated.