有没有可能是一个.apk文件将不带WiFi上运行? [英] Is it possible that a .apk file be run without Wifi?

查看:140
本文介绍了有没有可能是一个.apk文件将不带WiFi上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用来创建自己的网站的.apk文件。难道这个文件中,而无需访问数据连接或WiFi上运行?但所有的相同,当无线或数据切换网站上应该进行更新。任何人有任何可以帮助我吗?

I just used this to create a .apk file of my website. Is it possible that this file be run without accessing data connections or wifi? But all the same, the website should get updated when the Wifi or data is switched on. Anyone have anything that can help me?

推荐答案

第一家专卖店网站的文件

First store your website's file in asset folder.

现在每次你打开应用程序,检查网站文件崩溃的存在与否来prevent应用程序。

Now everytime you open the app, check if the website file exists or not to prevent app from crashing.

低于,如果它不存在,检查给出的code,然后调用一个从资产文件拷贝到设备存储方法。

The code given below checks that and if it doesn't exist, then it calls a method which copies the file from asset to device storage.

File file = new File(YOUR FILE PATH);
if(!file.exists()){    
    //Doesn't exist. Create it in sdcard
    copyAssets();
}

下面是从资产的网站文件复制到设备存储(把它们放在你的类)的方法 -

Here are the methods to copy the website file from asset to device storage (put them in your class) -

private void copyAssets() {

    AssetManager assetManager = getAssets();
    String[] files = null;

    try {

        files = assetManager.list("");

    } catch (IOException e) {
    Log.e("tag", "Failed to get asset file list.", e);
    }

    for(String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            File outFile = new File(DIRECTORY, filename);
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }       
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
}

然后检查用户的网络连接(在onCreate方法) -

Then check internet connection of user (in oncreate method) -

ConnectivityManager cm =
    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {

    //user is connected to internet
    //put the code given ahead over here

}

权限 -

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

现在,如果用户连接到互联网,访问互联网,让你的网站的新来源$ C ​​$ C像这样(把这个code在上述网络检查code) -

Now if the user is connected to the internet, access the internet and get your website's new source code like this (put this code in internet checking code given above) -

URL url = new URL(YOUR URL);
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
                                       yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
    a.append(inputLine);
    in.close();

String source = a.toString();

现在,一旦你有源$ C ​​$ C,更新你的HTML文件中设备存储这样的 -

Now once you have the source code, update your HTML file in device storage like this -

File gpxfile = new File(File address, "filename.html");

BufferedWriter bW;

try {
    bW = new BufferedWriter(new FileWriter(gpxfile));
    bW.write(source); //our new source code
    bW.newLine();
    bW.flush();
    bW.close();
} catch (IOException e) {
    e.printStackTrace();
}

您完成了!现在,从这样的存储加载文件web视图(在onCreate方法后,所有的code,我们以前写的) -

You are done! Now load your file to webView from storage like this (in oncreate method after all the code that we wrote before) -

  index.loadUrl("file://"+Environment.getExternalStorageDirectory()+ "Your address in storage");

建议发送用户请求不时地将他们的网络来更新网站和prevent使用它过时的副本。

It is recommended to send user requests time to time to turn on their internet to update the website and prevent use of outdated copy of it.

这篇关于有没有可能是一个.apk文件将不带WiFi上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆