给webapp加上一个apk外壳

Posted ChHM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给webapp加上一个apk外壳相关的知识,希望对你有一定的参考价值。

原文:http://blog.csdn.net/cmyh100/article/details/77862962

1、在android Studio里创建一个项目

2.创建MyApplication.java  1.创建webview  2.创建一个线程把文件复制到一个文件夹里  3.解压缩包  4.webview的url指向本地

public class stuGuide extends AppCompatActivity {
    private  WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_guide);
        /*WebView main = (WebView)findViewById(R.id.main);*/
        webView = (WebView) findViewById(R.id.main);
        new Thread(new Runnable() {
            @Override
            public void run() {
                putfile();
                try {
                    unZipWebZipInThread();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(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 void putfile() {
        InputStream is = null;
        try {
            is = this.getAssets().open("studentGuide.zip");
            File path = Environment.getExternalStorageDirectory();
            System.out.println("path== " + path);
            File file = new File(path + "/stuGuide");
            file.mkdir();
            File absoluteFile = file.getAbsoluteFile();
            System.out.println("absoluteFile===" + absoluteFile);
            if (file.exists()) {
                System.out.println("file exists");
            }


            FileOutputStream fos = new FileOutputStream(new File(file.getAbsolutePath()+"/studentGuide.zip"));
            byte[] buffer = new byte[1024];
            int byteCount;
            while ((byteCount = is.read(buffer)) != -1) {
                fos.write(buffer, 0, byteCount);
            }
            fos.flush();
            is.close();
            fos.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void unZipWebZipInThread() throws Exception {

        File path = Environment.getExternalStorageDirectory();
        String dstPath = path + "/stuGuide/studentGuide.zip";
        ZipFile zipFile = new ZipFile(dstPath);
        if (zipFile.isValidZipFile()) {
            final ProgressMonitor progressMonitor = zipFile.getProgressMonitor();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        int percentDone;
                        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

网络允许
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
sdcard外部存储权限允许
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
webview全屏去头部
android:theme="@style/Theme.AppCompat.Light.NoActionBar" 
手机转屏不重新加载activity
android:configChanges="keyboardHidden|orientation|screenSize">

5、在main文件夹下新建一个assets文件夹,把webapp打包成zip压缩包,粘贴放入

需要解压缩,所以得导入(java处理zip压缩包,搜索下载)zip4j_1.3.2.jar

build.gradle 需要配置一下刚刚导入的jar包

dependencies {
    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
    compile ‘com.android.support:appcompat-v7:23.1.1‘

    compile files(‘libs/zip4j_1.3.2.jar‘)
}

 

以上是关于给webapp加上一个apk外壳的主要内容,如果未能解决你的问题,请参考以下文章

Promise 异步函数的加上外壳终止Promise

通过 webapp 提供时,SVG 片段标识符在 Safari 中被交换

Android项目实战(三十六):给背景加上阴影效果

将HTML5封装成android应用APK文件的几种方法(转载)

Android 将HTML5封装成android应用APK文件的几种方法

如何从一个片段导航到另一个片段?