Date and time related methods. DateTime is a predefined object. You should not declare it yourself. Date and time values are stored as ticks. Ticks are the number of milliseconds since January 1, 1970 00:00:00 UTC. This value is too large to be stored in an Int variable. It should only be stored in a Long variable. The methods DateTime.Date and DateTime.Time convert the ticks value to a string. You can get the current time with DateTime.Now. Example: DimnowAsLong now = DateTime.Now Msgbox("The date is: " & DateTime.Date(now) & CRLF & _
"The time is: " & DateTime.Time(now), "")
Holds a thrown exception. You can access the last thrown exception by calling LastException. For example: Try DiminAsInputStream in = File.OpenInput(File.DirInternal, "SomeMissingFile.txt")
'... Catch Log(LastException.Message)
EndTry Ifin.IsInitializedThenin.Close
Intent objects are messages which you can send to the OS in order to do some external action. The Intent object should be sent with StartActivity keyword. See this page for a list of standard constants. Example, launch YouTube application: DimIntent1AsIntent Intent1.Initialize(Intent1.ACTION_MAIN, "")
Intent1.SetComponent("com.google.android.youtube/.HomeActivity")
StartActivity(Intent1)
Holds values related to the display. You can get the values of the the current display by calling GetDeviceLayoutValues. For example: DimlvAsLayoutValues lv = GetDeviceLayoutValues Log(lv) 'will print the values to the log Activity.LoadLayout and Panel.LoadLayout return a LayoutValues object with the values of the chosen layout variant.
A status bar notification. The user can open the notifications screen and press on the notification. Pressing on the notification will start an activity as set by the notification object. Notifications are usually used by services as services are not expected to directly start activities. The notification must have an icon and its "info" must be set. Example: DimnAsNotification n.Initialize n.Icon = "icon" n.SetInfo("This is the title", "and this is the body.", Main) 'Change Main to "" if this code is in the main module. n.Notify(1)
Each Service module includes a Service object. This object is used to bring the service in and out of the foreground state. See the Services tutorial for more information.
Strings are immutable in Basic4android, which means that you can change the value of a string variable but you cannot change the text stored in a string object. So methods like SubString, Trim and ToLowerCase return a new string, they do not change the value of the current string. Typical usage: DimsAsString s = "some text" s = s.Replace("a", "b") You can use StringBuilder if you need a mutable string. Note that string literals are also string objects: Log(" some text ".Trim)
StringBuilder is a mutable string, unlike regular strings which are immutable. StringBuilder is especially useful when you need to concatenate many strings. The following code demonstrates the performance boosting of StringBuilder: DimstartAsLong start = DateTime.Now 'Regular string DimsAsString Fori = 1To5000 s = s & i Next Log(DateTime.Now - start)
'StringBuilder start = DateTime.Now DimsbAsStringBuilder sb.Initialize Fori = 1To5000 sb.Append(i)
Next Log(DateTime.Now - start) Tested on a real device, the first 'for loop' took about 20 seconds and the second took less then tenth of a second. The reason is that the code: s = s & i creates a new string each iteration (strings are immutable). The method StringBuilder.ToString converts the object to a string.
A Timer object generates ticks events at specified intervals. Using a timer is a good alternative to a long loop, as it allows the UI thread to handle other events and messages. Note that the timer events will not fire while the UI thread is busy running other code. The timer Enabled property is set to False by default. To make it start working you should change it to True. Timer events will not fire when the activity is paused, or if a blocking dialog (like Msgbox) is visible. Timers should be declared in Sub Process_Globals. Otherwise you may get multiple timers running when the activity is recreated. It is also important to disable the timer when the activity is pausing and then enable it when it resumes. This will save CPU and battery.