2

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 folder

2) cordova platform add android, cordova run android (cordova.jar is created) => the app is launched => device is ready is shown

3) 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!

Alex Stanese
  • 695
  • 4
  • 16
  • 33
  • Where from and what for is "Private final ExecutorService threadPool =Executors.newCachedThreadPool()"? – Regent Jul 11 '14 at 09:40
  • @Regent if I only add the code that is on phonegap page: :@Override public ExecutorService getThreadPool() { return threadPool; } it gives me error, and I read somewhere that I need to add this one too so that it'll work.. otherwise if I click on the error the "return threadpool" will transfom in something like getthreadpool() – Alex Stanese Jul 11 '14 at 09:47
  • Can you please post your logcat error. – Siddharth_Vyas Jul 11 '14 at 09:53
  • @SiddharthVyas sure! I edited my question – Alex Stanese Jul 11 '14 at 10:18
  • @AlexStanese : Have you added any jar file? – Siddharth_Vyas Jul 11 '14 at 10:23
  • @SiddharthVyas no because i created the project through cli and when I run cordowa run android the app is build and I think the .jar files are also created and included in the project if the app runs on my phone.. – Alex Stanese Jul 11 '14 at 10:24
  • @AlexStanese : Try this http://stackoverflow.com/a/21898031/1785412 – Siddharth_Vyas Jul 11 '14 at 10:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57145/discussion-between-siddharth-vyas-and-alex-stanese). – Siddharth_Vyas Jul 11 '14 at 10:25
  • now in cordova 6 it enforce to @override `requestPermission`... Can you help in this regard – mumair May 06 '16 at 07:10
  • How can we put Android side's variables into Cordova? – Bay Sep 18 '19 at 11:17

1 Answers1

5

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");
            } 
}
Alok Nair
  • 3,944
  • 3
  • 22
  • 30
  • The 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. – Karthick Aug 08 '14 at 12:43