gpt4 book ai didi

android - 如何在android webview中实现phonegap/cordova?

转载 作者:搜寻专家 更新时间:2023-11-01 07:58:12 25 4
gpt4 key购买 nike

我只需要几分钟就有人告诉我这些步骤对于在 android webview 中实现 cordova 是否正确:

编辑:好的,我终于成功了,这些是正确的步骤:

1) 我创建项目:cordova create hello com.example.hello HelloWorld 并进入文件夹

2) cordova platform add android, cordova run android (cordova.jar 已创建) => 启动应用程序 => 显示设备准备就绪

3) 我使用以下代码在“/res/layout”中创建了一个 cordova_layout.xml:

<?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文件中:

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

注意: Activity 名称必须与 manifest.xml 中的名称相匹配

希望对您有所帮助。祝你有美好的一天!

最佳答案

如果你想在 phonegap 应用程序中加载一个 url 那么你可以使用下面的代码从 Assets 加载你的第一个 url

public class MyPhoneGapActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

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

要在 native android 应用程序中嵌入 cordova webview 并加载 url,请使用以下代码

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 - 如何在android webview中实现phonegap/cordova?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24694522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com