How to display Total Memory, Used Memory, and Available Memory in your Android Phone using Basic4Android

This is my other tutorial on how to find and display your available memory, used memory, and total memory in your phone. Since Android devices are usually battery-powered, Android is designed to manage memory (RAM) to keep power consumption at a minimum, in contrast to desktop operating systems which generally assume they are connected to unlimited mains electricity. When an Android app is no longer in use, the system will automatically suspend it in memory – while the app is still technically "open", suspended apps consume no resources and sit idly in the background until needed again. This has the dual benefit of increasing the general responsiveness of Android devices, since apps don't need to be closed and reopened from scratch each time, but also ensuring background apps don't consume power needlessly. Thus, it is really important to manage our memory in Android phones. On this, you need to create Three Labels and named it as "lblAvail" for displaying the Available Memory, "lblTotal" for displaying the Total Memory, and "lblUsed" for displaying the Used Memory. You need also to create one button and named it btnMemory to display all of this memory in one. Next, named your abstract design as "main". Your abstract designer will be like this one below: abstract design Declare your variable in the Sub_Global like this:
  1. Sub Globals
  2.         'These global variables will be redeclared each time the activity is created.
  3.         'These variables can only be accessed from this module.
  4.    Dim ph As ICOSPhone
  5.         Dim btnMemory As Button
  6.         Dim lblAvail As Label
  7.         Dim lblTotal As Label
  8.         Dim lblUsed As Label
  9. End Sub
Dim ph As ICOSPhone - ph here is our variable for ICOSPhone in which it has all the necessary libraries in using phone utilities. Dim lblAvail As Label - we use this label to display the the Available Memory of your phone. Dim lblTotal As Label - we use this label to display the the Total Memory of your phone. Dim lblUsed As Label - we use this label to display the the Used Memory of your phone. Now create a new activity. It should look like this:
  1. Sub Activity_Create(FirstTime As Boolean)
  2.         'Do not forget to load the layout file created with the visual designer. For example:
  3.         Activity.LoadLayout("main")
  4. End Sub
Now your activity when run will look like this below: firstRun Next, type the following code below. That this code means to display the all the values of our memory when our btnMemory is click.
  1. Sub btnMemory_Click
  2.         lblUsed.Text = ph.UsedMemory
  3.         lblAvail.Text = ph.AvailMemory
  4.         lblTotal.Text = ph.TotalMemory
  5. End Sub
The ph.UsedMemory here is the method used to display the Used Memory, ph.AvailMemory for displaying Available Memory, and ph.TotalMemory for displaying Total Memory. When you click this button, it will display like this image below: finalImage Here's the full code for this tutorial:
  1. Sub Process_Globals
  2.         'These global variables will be declared once when the application starts.
  3.         'These variables can be accessed from all modules.
  4. End Sub
  5.  
  6. Sub Globals
  7.         'These global variables will be redeclared each time the activity is created.
  8.         'These variables can only be accessed from this module.
  9.    Dim ph As ICOSPhone
  10.         Dim btnMemory As Button
  11.         Dim lblAvail As Label
  12.         Dim lblTotal As Label
  13.         Dim lblUsed As Label
  14. End Sub
  15.  
  16. Sub Activity_Create(FirstTime As Boolean)
  17.         'Do not forget to load the layout file created with the visual designer. For example:
  18.         Activity.LoadLayout("main")
  19. End Sub
  20. Sub Activity_Resume
  21. End Sub
  22.  
  23. Sub Activity_Pause (UserClosed As Boolean)
  24.  
  25. End Sub
  26.  
  27.  
  28. Sub btnMemory_Click
  29.         lblUsed.Text = ph.UsedMemory
  30.         lblAvail.Text = ph.AvailMemory
  31.         lblTotal.Text = ph.TotalMemory
  32. End Sub
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer STI College - Surigao City Mobile: 09488225971 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky

Add new comment