Java 如何在服务中创建XWalkView?

Java 如何在服务中创建XWalkView?,java,android,service,android-webview,crosswalk,Java,Android,Service,Android Webview,Crosswalk,我正在努力用Crosswalk的实现取代Android应用程序中的原生webview 我们已经能够使应用程序的大部分功能正常工作,但在服务中创建XWalkView仍然是我们试图解决的问题。创建Webview不是问题,但XWalkView需要使用活动上下文。如果这里有人遇到过这个问题,并且知道可能的解决方案或解决方法,我将非常感激。谢谢,如果您需要任何其他信息,请立即询问。来自: 那么,人行横道是什么?我为什么要在意?请浏览以下网站: CrossWalk是一个HTML5运行时,您可以使用它创建具有

我正在努力用Crosswalk的实现取代Android应用程序中的原生webview

我们已经能够使应用程序的大部分功能正常工作,但在服务中创建XWalkView仍然是我们试图解决的问题。创建Webview不是问题,但XWalkView需要使用活动上下文。如果这里有人遇到过这个问题,并且知道可能的解决方案或解决方法,我将非常感激。谢谢,如果您需要任何其他信息,请立即询问。

来自:

那么,人行横道是什么?我为什么要在意?请浏览以下网站:

CrossWalk是一个HTML5运行时,您可以使用它创建具有“本机功能”的HTML5应用程序,您可以使用CrossWalk创建 仅适用于Android的HTML5应用程序(x86和arm体系结构)和 Tizen,但您也可以在android中使用人行横道作为视图 项目

这意味着您可以将Android WebView替换为XWalkView,并获得一些额外功能,如:

-WebGl

-WebRTC

-网络音频

如何在Android应用程序中嵌入CrossWalk WebView(从现在开始是XWalkView),从而在我的混合应用程序中拥有所有这些优点 应用程序(具有html5功能的Android本机)

首先,您必须下载运行时:

下载任何Android(ARM)版本

文件中包含了在html5应用程序中开始工作所需的所有内容

对于这个测试,我们需要在Eclipse中导入xwalk核心库项目

使用基本活动创建一个新的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 class XWalkEmbedLib extends Activity {
    private LinearLayout commentsLayout;
    private XWalkView xWalkWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xwalk_embed_lib);
         commentsLayout=(LinearLayout)findViewById(R.id.principal);
         xWalkWebView = new XWalkView(this, this);
         xWalkWebView.load("file:///android_asset/www/index.html", null);
         commentsLayout.addView(xWalkWebView);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.xwalk_embed_lib, menu);
        return true;
    }
}
把这个放在你的主布局上

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".XWalkMain" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <LinearLayout
                android:id="@+id/principal"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="86dp"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

最后,在你的
/assets/www
文件夹中放入你的html内容,就这样


我寻找了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;
    }

}

我希望它能为您工作。

您是在android studio还是eclipse上工作?由于使用XWalkView非常简单且与WebView类似,问题到底出在哪里,您在配置方面有问题吗?嵌入东西?特定的html5片段?我让XWalkView在活动中正常工作。但是,在服务中使用它是我遇到麻烦的地方,因为没有活动可以传递给构造函数。感谢您的详细介绍。但是,我完全理解你刚才解释的内容。我们的问题不是在正常情况下创建一个XWalkView,而是在一个服务中创建一个XWalkView,其中一个活动可能已创建,也可能尚未创建,这是我们的问题。