How to implement phonegap/cordova in android webview?
If you want to load an url in a phonegap app then you may use the below code to load your first url from asset
public class MyPhoneGapActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html", 10000);
}
For embedding a cordova webview in native android application and loading an url use the below code
public class CordovaActivity extends Activity implements CordovaInterface {
private CordovaWebView cordova_webview;
private String TAG = "CORDOVA_ACTIVITY";
private final ExecutorService threadPool = Executors.newCachedThreadPool();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cordova_layout);
cordova_webview = (CordovaWebView) findViewById(R.id.cordova_web_view);
// Config.init(this);
String url = "file:///android_asset/www/index.html";
cordova_webview.loadUrl(url, 10000);
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onDestroy() {
super.onDestroy();
if (this.cordova_webview != null) {
this.cordova_webview
.loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};");
this.cordova_webview.loadUrl("about:blank");
cordova_webview.handleDestroy();
}
}
@Override
public Activity getActivity() {
return this;
}
@Override
public ExecutorService getThreadPool() {
return threadPool;
}
@Override
public Object onMessage(String message, Object obj) {
Log.d(TAG, message);
if (message.equalsIgnoreCase("exit")) {
super.finish();
}
return null;
}
@Override
public void setActivityResultCallback(CordovaPlugin cordovaPlugin) {
Log.d(TAG, "setActivityResultCallback is unimplemented");
}
@Override
public void startActivityForResult(CordovaPlugin cordovaPlugin,
Intent intent, int resultCode) {
Log.d(TAG, "startActivityForResult is unimplemented");
}
}
Alex Stanese
Updated on June 04, 2022Comments
-
Alex Stanese almost 2 years
I need just a few minutes for someone to tell me if these steps are correct for implementing cordova in a android webview:
EDIT: Ok I finally got it working these are the right steps:
1) I create project:
cordova create hello com.example.hello HelloWorld
and enter the folder2)
cordova platform add android
,cordova run android
(cordova.jar is created) => the app is launched => device is ready is shown3) I create a cordova_layout.xml in "/res/layout" with this code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <org.apache.cordova.CordovaWebView android:id="@+id/cordova_web_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> </LinearLayout>
4)Import the project (as an "existing project" in eclipse) and add to the main java file after imports:
public class HelloWorld extends Activity implements CordovaInterface { private CordovaWebView cordova_webview; private String TAG = "CORDOVA_ACTIVITY"; private final ExecutorService threadPool = Executors.newCachedThreadPool(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cordova_layout); cordova_webview = (CordovaWebView) findViewById(R.id.cordova_web_view); // Config.init(this); String url = "file:///android_asset/www/index.html"; cordova_webview.loadUrl(url, 10000); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); } @Override protected void onDestroy() { super.onDestroy(); if (this.cordova_webview != null) { this.cordova_webview .loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};"); this.cordova_webview.loadUrl("about:blank"); cordova_webview.handleDestroy(); } } @Override public Activity getActivity() { return this; } @Override public ExecutorService getThreadPool() { return threadPool; } @Override public Object onMessage(String message, Object obj) { Log.d(TAG, message); if (message.equalsIgnoreCase("exit")) { super.finish(); } return null; } @Override public void setActivityResultCallback(CordovaPlugin cordovaPlugin) { Log.d(TAG, "setActivityResultCallback is unimplemented"); } @Override public void startActivityForResult(CordovaPlugin cordovaPlugin, Intent intent, int resultCode) { Log.d(TAG, "startActivityForResult is unimplemented"); } }
NOTE: the activity name must match the one in manifest.xml
Hope it will help you. Have a nice day!
-
Karthick over 9 yearsThe above code crashes the android application. Since, the stopLoading() method of webview is called and "viewClient" is always null. How can I resolve this issue?. I have tried in code instead of xml layout.