我正在努力用Crosswalk的实现替换我们Android应用程序中的原生webview。
我们已经能够让应用程序的大部分功能正常工作,但在服务中创建XWalkView仍然是我们试图克服的问题。创建Webview不是问题,但XWalkView需要使用活动上下文。如果在座的任何人遇到过这个问题,并知道可能的解决方案或变通方法,我将不胜感激。谢谢,如果您需要任何其他信息,请尽管询问。
发布于 2016-01-14 22:27:08
那么,什么是人行横道,我为什么要关心?看看这个网站:https://crosswalk-project.org/
CrossWalk是一个HTML5运行时,你可以用它来创建具有‘原生特性’的HTML5应用你可以使用CrossWalk为安卓(x86和arm架构)和Tizen创建仅支持HTML5的应用,但你也可以在安卓项目中使用CrossWalk作为一个视图。
这意味着你可以用XWalkView替换安卓WebView,并获得一些额外的功能,比如:
-WebGl
-WebRTC
-WebAudio
http://software.intel.com/en-us/html5/articles/crosswalk-application-runtime
从现在起,我如何在安卓应用程序中嵌入CrossWalk WebView,以便在我的混合应用程序(具有html5功能的安卓原生应用程序)上拥有所有这些好处?
首先,你必须下载运行时:
https://crosswalk-project.org/#documentation/downloads
下载任意Android(ARM)版本。
该文件中包含开始在html5应用程序中工作所需的所有内容。
对于这个测试,我们需要在Eclipse中导入xwalk-core-library项目
创建一个包含基本活动的新Android项目,将其与库链接,并将以下代码放入该活动中:
package com.example.xwalkwithlibrary;import org.xwalk.core.XWalkView;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.widget.LinearLayout;public XWalkEmbedLib extends { private LinearLayout commentsLayout;private XWalkView xWalkWebView;@Override protected void (Bundle savedInstanceState) { super.onCreate(savedInstanceState);Override protected void ActivityxWalkWebView =新菜单(XWalkView,this);xWalkWebView.load("file:///android_asset/www/index.html",null);commentsLayout.addView(xWalkWebView);} @Override public boolean onCreateOptionsMenu(菜单菜单){ //使菜单膨胀;这会将项目添加到操作栏(如果存在)。getMenuInflater().inflate(R.menu.xwalk_embed_lib,菜单);返回true;}}
把这个放在你的主布局上
< android:paddingBottom="@dimen/activity_vertical_margin“android:paddingLeft="@dimen/activity_horizontal_margin”android:paddingRight="@dimen/activity_horizontal_margin“android:paddingTop="@dimen/activity_vertical_margin”xmlns:android=“.XWalkMain”><文本视图Android:id=“@+id/textView1 1”android:layout_width="wrap_content“android:layout_height="wrap_content”android:text="@string/hello_world“/>
发布于 2016-02-02 04:43:31
我寻找了XWalkView.java代码,这样我就可以做一些有用的东西,但它还没有发布。但至少有一个好的解决方案是可行的:第一次在活动中创建XWalkView实例。然后找到一种方法将其存储在服务中,并在您的活动连接到服务时重用该实例(这样,您的html和js就不会重新加载;)
在谷歌搜索之后,我了解到XWalkView实例需要活动,因此它注册了一些生命周期侦听器。因此,当活动被销毁时(例如,调用XwalkView.onDestroy方法)(因此在我的示例中,我必须禁用它,以保留相同的实例并重用它)。
下面是我的简单示例: MainActivity.Java
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.MutableContextWrapper;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import java.util.logging.Logger;
import org.xwalk.core.XWalkView;
public class MainActivity extends Activity implements ServiceConnection {
private Logger logger = Logger.getLogger("com.tr");
private XWalkView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, MyService.class));
bindService(new Intent(this, MyService.class), this, BIND_AUTO_CREATE);
}
@Override
public void setContentView(View view) {
final ViewParent parent = view.getParent();
if (parent != null) {
ViewGroup group = (ViewGroup) parent;
group.removeView(view);
}
super.setContentView(view);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private boolean bound;
@Override
protected void onStop() {
super.onStop();
if (bound) {
unbindService(this);
bound = false;
}
}
public void onServiceConnected(ComponentName name, IBinder s) {
bound = true;
MyService.MyBinder binder = (MyService.MyBinder) s;
if (binder.getView() != null) {
view = binder.getView();
((MutableContextWrapper) view.getContext()).setBaseContext(this);
view.onShow();
} else {
view = new XWalkView(new MutableContextWrapper(this), this) {
@Override
public void onDestroy() {
// super.onDestroy();
//disable this method to keep an insatce in memory
}
};
view.load("http://10.110.23.198:8080/mdl/templates/android-dot-com/", null);
binder.setView(view);
}
setContentView(view);
}
public void onServiceDisconnected(ComponentName name) {
}
}服务类
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import org.xwalk.core.XWalkView;
/**
*
* @author Ramdane
*/
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MyBinder(this);
}
public class MyBinder extends Binder {
private MyService service;
private XWalkView view;
public MyBinder(MyService service) {
this.service = service;
}
public MyBinder() {
}
public void setService(MyService service) {
this.service = service;
}
public MyService getService() {
return service;
}
public XWalkView getView() {
return view;
}
public void setView(XWalkView view) {
this.view = view;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
}我希望它能为你工作。
https://stackoverflow.com/questions/34644232
复制相似问题