Android programming method for obtaining and displaying the current memory usage through the floating window

This example describes the method of Android programming to obtain and display the current memory usage. Share with you for your reference, as follows:

Operation effect:

Of which:

This is the floating window, which can be dragged at will to dynamically display the current memory usage.

Here's how the code is implemented:

The implementation of the floating window uses a service. Why use a service? Understand the general characteristics of service. Let's take a look:

Its main functions are in the createview method:

First, the code uses the WindowManager excuse. The entire Android window mechanism is based on an interface called WindowManager, which can add views to the screen or delete views from the screen. One end of the object it faces is the screen and the other end is the view. It directly ignores things such as our previous activity or dialog. In fact, the underlying implementation of our activity or diolog is also through WindowManager, which is global and the whole system is the only thing. It is the bottom layer of the display view. (this text comes from the network) the method is very simple. Basically, three addviews, removeview and updateviewlayout are used. Another: an error occurred when setting the height and width of the view, that is, get getWidth () and getHeight () in the view constructor. When a view object is created, Android does not know its size, so the results returned by getWidth () and getHeight () are 0. The real size is calculated only when calculating the layout, so an interesting thing will be found, that is, in ondraw() The reason why we can get length and width. Use the following methods:

The following is layoutparams. Set its properties:

Here, it is set to the form above all applications and below the status bar. When moving, case motionevent.action will be called_ MOVE:

The following codes are mainly:

Newly set the position parameters of the floating bar. In this way, the function of dragging is realized. How to obtain and update its memory data in time?

We noticed the handler:

We find the datarefresh method. The delaytime is set to 1000, that is, update the data every second.

Finally, let's take a look at the definition of meminfo:

There are only two methods defined to obtain total memory and used memory.

More readers interested in Android related content can view the special topics of this site: summary of Android window related operating skills, introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>