如何实现在Android的WebView中的PhoneGap /科尔多瓦? [英] How to implement phonegap/cordova in android webview?

查看:261
本文介绍了如何实现在Android的WebView中的PhoneGap /科尔多瓦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要几分钟,有人告诉我,如果这些步骤都是为一款Android的WebView实现科尔多瓦正确的:

I need just a few minutes for someone to tell me if these steps are correct for implementing cordova in a android webview:

编辑:好吧,我终于得到它的工作这些都是正确的步骤:

Ok I finally got it working these are the right steps:

1)创建工程:科尔多瓦创建Hello输入com.example.hello的HelloWorld 键,输入文件夹

1) I create project: cordova create hello com.example.hello HelloWorld and enter the folder

2)科尔多瓦平台添加的android 科尔多瓦运行的Andr​​oid (创建cordova.jar)=>应用程序启动=>设备已准备就绪显示

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

3)我创建一个cordova_layout.xml/ RES /布局这一code:

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)导入项目(如在Eclipse中现有项目)与进口后添加到主的Java文件:

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");
    } 
}

请注意:活动名称必须与之一的manifest.xml匹配

NOTE: the activity name must match the one in manifest.xml

希望这会帮助你。
有一个愉快的一天!

Hope it will help you. Have a nice day!

推荐答案

如果你想在PhoneGap的应用程序加载一个URL,那么你可以使用下面的code从资产加载你的第一个网址

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);
}

有关嵌入在原生Android应用程序的WebView科尔多瓦和装入URL中使用以下code

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");
            } 
}

这篇关于如何实现在Android的WebView中的PhoneGap /科尔多瓦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆