This article explains
Now we are formally entering the function and code analysis
The main function of the interpretation of it.
Copy a word book function in the first interface
That is, in the Welcome interface welcomeactivity
The overall architecture of the project is the MVC pattern
Only display data is called in the UI primarily
The real data processing logic is behind the client.
Then first introduce the UI and process, and then describe the background copy of the Word book function.
Effect
Execution flowchart
Process Detail Description
1> need to check the word book first
First, you need to check whether there is a word book file in the SDcard, it does not exist to copy
private void Checkwordbookfiles () { Boolean result = Resmanager.checkwordbookfiles (); if (!result) { Toast.maketext (context, "Word book db file not present-prepare copy", Toast.length_long). Show (); Copy the word book installwordbookfiles (); } else { Toast.maketext (context, "There is a Word book db file-prepare to check the Learning Plan", Toast.length_long). Show ();
Check the Learning Plan checkstudyplanfiles (); }}
2> Copy the Word book Method
Need progress bar hint, then copy Word book need to manipulate SQLite database
The time-consuming operation must be done in multiple threads, otherwise the master UI will be stuck.
private void Installwordbookfiles () { installwordbookfilesprogressdialog.setmessage ("Initializing Word Data"); Installwordbookfilesprogressdialog.setmax (wordbookconstant.allwordbookfilebytelength); Installwordbookfilesprogressdialog.show (); Thread installwordbookfilesthread = new Thread (new Installwordbookfilesthread ()); Installwordbookfilesthread.start ();}
3> copy a word book thread
Here the thread class implements the inner class of the Runnable interface, invoking the resource management copy of the Word book method
Class Installwordbookfilesthread implements Runnable { @Override public void Run () { resmanager.init ( context, installwordbookfilesprogressdialog); Resmanager.installwordbookfiles (); Myhandler.sendemptymessage (0);} }
Background copy Word book function detailed
This function is applied to the resource management module Resmanager
1> Copy all file functions
public static void Installwordbookfiles () {try {//Create project directory file Path = new file (Pathconstant.wdbook_path); if (path.exists () = = False) {LOG.D ("Vrbword", "path didn ' t exist, creating!!!"); Path.mkdirs (); }//Install file Installfile (Wordbookconstant.dict_str + ". SMF", Pathconstant.wdbook_path + " /"+ wordbookconstant.dict_str); Installfile (WORDBOOKCONSTANT.CET4_STR, Pathconstant.wdbook_path + "/" + wordbookconstant.cet4_str); Installfile (WORDBOOKCONSTANT.CET6_STR, Pathconstant.wdbook_path + "/" + wordbookconstant.cet6_str); Installfile (WORDBOOKCONSTANT.KAOYAN_STR, Pathconstant.wdbook_path + "/" + Wordbookconstant.kao YAN_STR); Installfile (WORDBOOKCONSTANT.TOFEL_STR, Pathconstant.wdbook_path + "/" + wordbookconstant.tofel_str); Installfile (Wordbookconstant.ielts_str, PathconStant. Wdbook_path + "/" + wordbookconstant.ielts_str); Installfile (WORDBOOKCONSTANT.GRE_STR, Pathconstant.wdbook_path + "/" + wordbookconstant.gre_str); } catch (Exception e) {e.printstacktrace (); }}
a> First to create the project directory, is copied from the assets directory to the SDcard directory, you can use the constant class constant to determine the directory.
b> Directory is created, you can copy it in sequence.
The installfile(from, dest) method is used here to copy a single file
2> Copy File function
Copy a single file
is to copy the specified file data from the assets directory to a file in the specified directory of SDcard
private static void Installfile (string from, String dest) throws Exception { //judgment file fp = new files (dest); if (!fp.exists () | | fp.length () < * 1024x768) { //determine out and in stream inputstream in = Context.getassets (). open (from);
outputstream out = new FileOutputStream (FP); byte[] buf = new Byte[operatefilebytelength] for each read and write section while ( -1! = In.read (buf)) { out.write (BUF); Sleep can enhance the progress display effect SYSTEMUTIL.SLEEPVD (5); Continuously set the progress value Progressvalue + = operatefilebytelength; Myprogressdialog.setprogress (Progressvalue); } Close in and out stream in.close (); Out.close (); }}
a> First to determine the file size, the file exists and the file satisfies the size condition to copy
b> then determines the out and in streams, and writes the read data to the file according to the specified byte length.
c> here used to sleep just to show better
d> Each time a specified byte of data is written, the progress value is incremented and displayed in a custom dialog box.
More please download the Source code interpretation
Application Download: please click
Source Download: please click
Android Project Combat-recite the word dev01-copy Word book realization