I have an Android Phonegap/Cordova app useing a remote index page which implements html5 caching so the app can be used offline. This works great once the user has started the app with an active connection to fill the cache initially, it works seamlessly and feels really native when offline.

However I want to gracefully handle someone starting it for the very first time when offline, ie when the remote pages have not been cached yet. At the moment it gives a page not found.

I can check if there is a network connection prior to redirecting to the remote page but this means they have to have a connection to start it up even when the remote index.html has been cached and would be available offline.

Is there a good way to avoid the default page not found? Ideally I want to show a dialog saying the user needs a data connection to open the app for the first time to allow the remote assets to be cached locally.

有帮助吗?

解决方案

Found the answer - I added this to the main activity which extends Droidgap

 public void onReceivedError( int errorCode, String description, String failingUrl) {
        super.loadUrl("file:///android_asset/www/offline.html");
        return;
 }

In this case I included an offline.html file to display an offline message, with a try again button which attempts to redirect to the remote index page. If the webview has issues loading that remote page it will gracefully fall back to the local offline.html page until it can cache the remote files.

No nasty 404 pages, nice!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top