給webapp加上一個apk外殼

來源:互聯網
上載者:User

標籤:over   onkeydown   hidden   pfile   mpi   pos   nts   外部   oncreate   

原文: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" 手機轉屏不重新載入activityandroid: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外殼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.