In the implementation of a requirement in the Remcovery mode, in the/cache/recovery/directory to create a log file to record the relevant log information, in order to read the log information after the boot, and then according to the log information to make corresponding processing, But it is strange that every time the file is not automatically deleted by the system, and the system's original log files (such as upgrade log---last_log_r) are still retained, so in the global search for the keyword "/cache/recovery/" found the reason for this.
In other words, the system will be deleted after the system boot is not the "Last_" in the/cache/recovery/directory is not prefixed with the file, then how to implement it, let's take a look.
First, there is a Bootreceiver class in the framework layer that handles the aftermath of system startup, such as deleting old unrelated log information.
First step: Register the broadcast receiver Bootreceiver in the manifest file Androidmanifest.xml under Framework/base/core/res, and the broadcast to receive system boot completion is as follows:
<receiver android:name= "Com.android.server.BootReceiver" android:primaryuseronly= "true" > < intent-filter> <action android:name= "Android.intent.action.BOOT_COMPLETED"/> </ Intent-filter> </receiver>
Second step: When the system is started, the broadcast receiver will receive a broadcast after the system has started, execute the logbootevents function under OnReceive and execute the Handleaftermath () function under Recoverysystem, and finally delete the/ Cache/recovery is not a file prefixed with Last_. The specific functions are as follows:
/** * Called after booting to process and remove recovery-related files. * @return The log file from recovery, or null if none is found. * * @hide */public static String Handleaftermath () {//Record the tail of the Log_file log.d (TA G, "Entering the Handleaftermath Method"); String log = null; try {log = Fileutils.readtextfile (Log_file,-log_file_max_length, "... \ n"); } catch (FileNotFoundException e) {log.i (TAG, "No recovery Log file"); } catch (IOException e) {log.e (TAG, "Error Reading Recovery Log", e); }//Delete everything in recovery_dir except those beginning//with last_prefix string[] names = RE Covery_dir.list (); for (int i = 0; names! = null && i < names.length; i++) {if (Names[i].startswith (last_prefix)) cont Inue; File F = new file (Recovery_dir, names[i]); if (!f.delete ()) {LOG.E (TAG, "Can ' t Delete:" + f); } else {log.i (TAG, "Deleted:" + f); }} return log; }
Note: If we need to save the file in the/cache/recovery/directory, we can name it as "Last_" or modify the relevant logic in Recoverysystem.
Recovery mode under/cache/recovery/directory to create files, after boot files are deleted