In this article, we will implement the following functions:
- Start
- Clear Cache
- Kill Process
- Double-click a single suspended window
- Count the available RAM memory size
I. boot
Many mobile phone software have the boot function, and sometimes it is very practical to enable the software to boot. So how does one enable startup? By searching, we know that the system will send a broadcast after startup, intent. action_boot_completed, so we only need to customize a broadcastreciever to receive the broadcast, and then start ourProgramYou can start the instance at startup.
In this program, mybroadcastreceiver is used as an example:
Public Class Mybroadcastreceiver Extends Broadcastreceiver {@ override Public Void Onreceive (context, intent) {string action =Intent. getaction (); If (Action. Equals (intent. action_boot_completed )){ // Start selected or not Boolean Start = Preferencemanager. getdefasharsharedpreferences (context). getboolean (cleaneractivity. key_start_when_boot_completed, True ); If (Start) {intent I = New Intent (); I. setclass (context, floatservice. Class ); Context. startservice (I );}}}}
The declaration in androidmanifest. XML is as follows:
<Receiver er Android: Name = "mybroadcastreceiver"> <intent-filter> <action Android: Name = "android. intent. action. boot_completed "/> </intent-filter> </Cycler>
Ii. Clear Cache
In the source code of settings in Android, The deleteapplicationcachefiles () method of packagemanager is used to clear the cache. However, it is very difficult to use this function as a third-party software. We also found freestorageandnotify () in packagemanager () methods can also be used to clear the cache, And it is relatively difficult to use third-party software. Therefore, we use the freestorageandnotify () method to clear the cache.
When freestorageandpolicy () is used, permission must be declared in androidmanifest. xml.
<! -- This permission is required to clear the cache -->
<Uses-Permission Android: Name = "android. Permission. clear_app_cache"/>
For more information about how to use this function, see the sort AchE () function in myfloatview. java.
3. Killing Processes
In activitymanager. Java, we provide the killbackgroundprocesses (string packagename) function to kill a process. For details about how to kill a process, refer to the killbackgroundprocess () function in myfloatview. java. To kill a process, you must declare the following permissions in androidmanifest:
<! -- This permission is required when the process is killed --> <uses-Permission Android: Name = "android. Permission. kill_background_processes"/>
4. Double-click a single suspended window
There are a lot of tutorials on implementing floating windows on the Internet, but few have implemented click and double-click events for floating windows. In this program, we use multiple threads (timer and timertask) to judge and double-click by adding a flag, recording the time when the user clicks the floating window, and clicking and double-clicking the response. In the ontouchevent () function of myfloatview. Java, double-click a single node to judge.
To distinguish between single-click and single-click operations, you can determine the interval between two clicks. Click the corresponding thread of the event to determine whether to double-click the sleep wait for a certain period of time before execution, and determine whether to execute the function of clicking the response thread Based on the flag.
5. Count the available RAM memory size
A: The meminforeader class in Android reads/proc/meminfo to implement memory size-related functions. However, if a third-party program cannot be called, we will directly change meminforeader. java is directly copied to the project and can be used as needed.
B: Use memoryinfo and activitymanager to obtain the available RAM memory size.
Activitymanager AM = (activitymanager)This. Getsystemservice (context. activity_service); memoryinfo Mi=NewMemoryinfo (); am. getmemoryinfo (MI );//Mi. availmem; available memory of the current systemLog. E ("tag", "getmemoryinfo:" + MI. availmem );
Source code link: http://pan.baidu.com/share/link? Consumer id = 62428 & UK = 2953765628