1. 程式人生 > >如何給webAPP加上一個apk外殼

如何給webAPP加上一個apk外殼

webAPP打包成apk流程(原理:手機本地解webapp壓縮包後開啟index.html)

1.首先你需要一個Android開發軟體,推薦Android Studio。

下載地址:http://www.android-studio.org/

安裝過程預設即可,會遇到的問題就一些環境變數,sdk路徑選擇什麼的,這裡不詳細介紹。

2.建立一個專案

進入軟體介面我們開始新建一個專案,專案名自取,專案路徑選擇,其他的預設即可

3.MyApplication.java(懶,就不解釋了,程式碼直接上。主要就是  1.建立webview  2.建立一個執行緒把檔案複製到一個資料夾裡  3.解壓縮包  4.webview的url指向本地)

package com.example.cmyh.xxx;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import 
android.webkit.WebSettings; import android.webkit.WebView; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.progress.ProgressMonitor; 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/"); } } }

4.Activity.xml(隨便上個最簡單的)

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">

</WebView>

5.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">

6.在main資料夾下新建一個assets資料夾,把webapp打包成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')
}

============================================================================  增加相容性

引入一個  ProgressWebView.java  裡面有分裝各鍾相容屬性
Activity.xml 裡面修改為:
<com.example.cmyh.stuguide.ProgressWebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">

</com.example.cmyh.stuguide.ProgressWebView>

MyApplication.java下
private  WebView webView;修改為private  ProgressWebView webView;
webView = (ProgressWebView) findViewById(R.id.main);
webView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        System.out.println("showMyUrl:"+url);
        view.loadUrl(url);
        return true;
    }
});