Is It Possible That A .apk File Be Run Without Wifi?
Solution 1:
First store your website's file in asset folder.
Now everytime you open the app, check if the website file exists or not to prevent app from crashing.
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.
Filefile=newFile(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) -
privatevoidcopyAssets() {
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);
}
}
}
privatevoidcopyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = newbyte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Then check internet connection of user (in oncreate method) -
ConnectivityManagercm=
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfonetInfo= cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//user is connected to internet//put the code given ahead over here
}
Permission -
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" />
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) -
URLurl=newURL(YOUR URL);
URLConnectionyc= url.openConnection();
BufferedReaderin=newBufferedReader(newInputStreamReader(
yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuildera=newStringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
Stringsource= a.toString();
Now once you have the source code, update your HTML file in device storage like this -
Filegpxfile=newFile(File address, "filename.html");
BufferedWriter bW;
try {
bW = newBufferedWriter(newFileWriter(gpxfile));
bW.write(source); //our new source code
bW.newLine();
bW.flush();
bW.close();
} catch (IOException e) {
e.printStackTrace();
}
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");
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.
Post a Comment for "Is It Possible That A .apk File Be Run Without Wifi?"