Programmatically clear the PhoneGap / Cordova application cache on Android to simulate a new installation?

This is related to my previous question, β€œ How can I clean the localStorage application of the application on my Android emulator every time I install it? '.

It is also based on How to clear the Android application cache? and How to programmatically clear application data? .

None of the above questions gives a direct answer that applies to Android PhoneGap / Cordova applications. This blog post by Igor Khrupin describes the situation in the context of native Android, so this question expands it to cover Cordoba.

I will send the answer myself, but I am full Java noob, so please edit to improve.

+4
source share
3 answers

I was unable to get android to reload new versions:

super.loadUrl("file:///android_asset/www/index.html"); 

between calls, so I first borrowed your code that worked quite well, except that all the data that was written by my application was also deleted.

A quick and easy fix is ​​to change the file url above:

 super.loadUrl("file:///android_asset/www/index.html?1"); 

And change the number behind? with every load. I finally added:

 import java.util.Random; public class MainActivity extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { Random generator = new Random(); int r = generator.nextInt(); super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/www/index.html?" + r); } } 

Now every time I rebuild the application, it actually updates the cached version of the html file.

I would really like to know how to force loadUrl to be updated from the application itself or force a reboot.

+5
source

just saw it. This can be done with one line. I'm not sure if this is what you want, but it serves my purpose, which clears the cache on startup

https://coderwall.com/p/aj79eg

 super.clearCache(); // just add This Line super.loadUrl("file:///android_asset/www/index.html"); 
+15
source

This answer borrows a lot of code from this blog post by Igor Khrupin .

Your Project > src > [com/org/whatever].[YourNameSpace].[ActivityNameHere] > [ActivityNameHere].java should look something like this for a regular PhoneGap application.

 package [com/org/whatever].[YourNameSpace].[ActivityNameHere]; import org.apache.cordova.*; import android.os.Bundle; public class [ActivityNameHere] extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/www/index.html"); } } 

What we do is change it so it looks like this. Immutable lines are commented out for clarity, but should be uncommented when implemented.

Remember to replace [com/org/whatever] , [YourNameSpace] and a few [ActivityNameHere] fill in your own values.

 // package [com/org/whatever].[YourNameSpace].[ActivityNameHere]; // import org.apache.cordova.DroidGap; // import android.os.Bundle; import java.io.File; import android.util.Log; // public class [ActivityNameHere] extends DroidGap { private static [ActivityNameHere] instance; // @Override // public void onCreate(Bundle savedInstanceState) { instance = this; [ActivityNameHere].getInstance().clearApplicationData(); // super.onCreate(savedInstanceState); // super.loadUrl("file:///android_asset/www/index.html"); // } public static [ActivityNameHere] getInstance() { return instance; } public void clearApplicationData() { File cache = getCacheDir(); File appDir = new File(cache.getParent()); if (appDir.exists()) { String[] children = appDir.list(); for (String s : children) { if (!s.equals("lib")) { deleteDir(new File(appDir, s)); Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************"); } } } } public static boolean deleteDir(File dir) { if (dir != null && dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } return dir.delete(); } // } 

As stated in my question, I don't have Java skills, so I hacked it together from what I could find, and it looks like it works. Please edit accordingly - I think this is what should be accessible for people to effectively copy and paste when they need it, since the whole point of PhoneGap on Android is to distract the developer from Java (which, for writing, I will get into training, as soon as my current project is completed).

The code works, at least in my case. It would be nice to add -

(1) Ability to call this function from JS, these lines .

and

(2) The choice to clear the cache is only the first time the application is launched , so you can simulate an application that is "force closed."

+2
source

All Articles