Original: http://blog.csdn.net/cmyh100/article/details/77862962
1. Create a project in Android studio
2. Create Myapplication.java 1. Create WebView 2. Create a thread to copy the file into a folder 3. The URL of the unpacked 4.webview is pointing to the local
Public classStuguideextendsappcompatactivity {PrivateWebView WebView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_stu_guide); /*WebView main = (WebView) Findviewbyid (r.id.main);*/WebView=(WebView) Findviewbyid (R.id.main); NewThread (NewRunnable () {@Override Public voidrun () {putfile (); Try{unzipwebzipinthread (); } Catch(Exception e) {e.printstacktrace (); }}). Start (); } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if(KeyCode = = Keyevent.keycode_back &&Webview.cangoback ()) {Webview.goback ();//return to previous page return true; } return Super. OnKeyDown (KeyCode, event); } PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {File path=environment.getexternalstoragedirectory (); String Mainurl= "file://" + path + "/stuguide/studentguide/index.html"; WebSettings websettings=webview.getsettings (); Websettings.setjavascriptenabled (true); Webview.loadurl (Mainurl); } }; Private voidPutfile () {InputStream is=NULL; Try{ is= This. Getassets (). Open ("Studentguide.zip"); File Path=environment.getexternalstoragedirectory (); System.out.println ("path==" +path); File File=NewFile (path + "/stuguide")); File.mkdir (); File Absolutefile=File.getabsolutefile (); System.out.println ("absolutefile===" +absolutefile); if(File.exists ()) {System.out.println ("File exists"); } FileOutputStream Fos=NewFileOutputStream (NewFile (File.getabsolutepath () + "/studentguide.zip")); byte[] buffer =New byte[1024]; intByteCount; while((ByteCount = is.read (buffer))! =-1) {fos.write (buffer,0, ByteCount); } fos.flush (); Is.close (); Fos.close (); } Catch(IOException e) {e.printstacktrace (); } } Private voidUnzipwebzipinthread ()throwsException {File path=environment.getexternalstoragedirectory (); String Dstpath= path + "/stuguide/studentguide.zip"; ZipFile ZipFile=NewZipFile (Dstpath); if(Zipfile.isvalidzipfile ()) {FinalProgressmonitor Progressmonitor =Zipfile.getprogressmonitor (); NewThread (NewRunnable () {@Override Public voidrun () {Try { intPercentDone; while(true) {Thread.Sleep (50); PercentDone=Progressmonitor.getpercentdone (); if(PercentDone >= 100) { Break; }} mhandler.sendemptymessage (1); } Catch(interruptedexception e) {//JAVALOG.E (TAG, e);}} ). Start (); Zipfile.extractall (Path+ "/stuguide/"); } }}
3, Activity.xml
<WebView android:id= "@+id/main" android:layout_width= "Match_parent" Android:layout_height= "Match_parent"/>
4, Androidmanifest.xml
Network allows <uses-permission android:name= "Android.permission.INTERNET" ></uses-permission> SDcard External Storage permission allows <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
webview full screen to head android:theme= "@style/theme.appcompat.light.noactionbar" phone to the screen does not reload Activityandroid: Configchanges= "Keyboardhidden|orientation|screensize" >
5, in the main folder to create a new assets folder, the WebApp package into a ZIP archive package, paste into
Need to extract, so have to import (Java Processing zip archive, search download)Zip4j_1.3.2.jar
Build.gradle need to configure the jar package that you just imported
Dependencies { ' libs ', include: [' *.jar ']) ' com.android.support:appcompat-v7:23.1.1 ' Compile files (' Libs/zip4j_1.3.2.jar ')}
Add an apk shell to WebApp