简体   繁体   English

如何在服务中创建XWalkView?

[英]How to create an XWalkView within a service?

I am working towards replacing the native webview in our Android app with Crosswalk's implementation. 我正在努力以Crosswalk的实现方式替换我们Android应用中的本地webview。

We have been able to get most of the functionality of the app working but creating an XWalkView within a service is still an issue we are trying to get past. 我们已经能够使应用程序的大多数功能正常运行,但是在服务中创建XWalkView仍然是我们要解决的问题。 Creating a Webview is not an issue but an XWalkView requires an activity context to be used. 创建Webview不是问题,但是XWalkView需要使用活动上下文。 If anyone here has run across this problem and knows of a possible solution or work-around, I would appreciate it greatly. 如果这里有人遇到了这个问题,并且知道可能的解决方案或解决方法,我将不胜感激。 Thanks, and if you need any other information please ask away. 谢谢,如果您需要其他任何信息,请询问。

From butelo in GitHub : GitHub中的 butelo

So, what is crosswalk and why do I care? 那么,什么是人行横道,我为什么要在乎呢? Take a look at the website: https://crosswalk-project.org/ 看一下网站: https//crosswalk-project.org/

CrossWalk is a HTML5 runtime, you can use it to create HTML5 applications with 'native features' You can use CrossWalk to create HTML5-only applications for Android (x86 and arm architectures) and Tizen but you can also use CrossWalk as a View within an android project. CrossWalk是HTML5运行时,您可以使用它来创建具有“本机功能”的HTML5应用程序。您可以使用CrossWalk为Android(x86和arm架构)和Tizen创建仅HTML5的应用程序,但是您也可以将CrossWalk用作内部的View android项目。

This means you can replace Android WebView with XWalkView and get some extra features like: 这意味着您可以用XWalkView替换Android WebView并获得一些额外的功能,例如:

-WebGl -WebGl

-WebRTC -WebRTC

-WebAudio -WebAudio

http://software.intel.com/en-us/html5/articles/crosswalk-application-runtime http://software.intel.com/zh-CN/html5/articles/crosswalk-application-runtime

How do I embed a CrossWalk WebView, from now on a XWalkView, inside an Android application to have all this goodies on my hybrid application (Android Native with html5 features) 从现在开始,如何将CrossWalk WebView嵌入XWalkView的Android应用程序中,以便将所有这些功能添加到混合应用程序中(具有html5功能的Android Native)

First you have to download the runtime: 首先,您必须下载运行时:

https://crosswalk-project.org/#documentation/downloads https://crosswalk-project.org/#documentation/downloads

Download any of the Android(ARM) versions. 下载任何Android(ARM)版本。

Inside the file is everything you need to start working in an html5 application. 在文件中包含开始在html5应用程序中工作所需的一切。

For this test we'll need to import the xwalk-core-library project inside our Eclipse 对于此测试,我们需要将xwalk-core-library项目导入我们的Eclipse中

Create a new Android project with a basic Activity link it with the library and put this code in the Activity: 使用基本的Activity创建一个新的Android项目,并将其与库链接,然后将以下代码放入Activity中:

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

Put this on your main layout 放在您的主要布局上

 <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> 

finally inside your /assets/www folder put your html stuff and that's it 最终在您的/assets/www文件夹中放入html内容,仅此而已

I looked for the XWalkView.java code so I can make something useful but it is not published yet. 我寻找了XWalkView.java代码,因此可以使它有用,但是尚未发布。 But there is at least a good solution that can work: Create for the first time the XWalkView instance inside an activity. 但是至少有一个好的解决方案可以起作用:第一次在活动中创建XWalkView实例。 then find a way to store it in a service and reuse that same instance whenever your activity connects to the service (this way, your html and js are not reloaded ;) ) 然后找到一种将其存储在服务中的方法,并在您的活动连接到该服务时重用同一实例(通过这种方式,不会重新加载html和js;))

After googling, I understood that the XWalkView instance needed and activity so it registers some life cycle listeners. 谷歌搜索之后,我了解到XWalkView实例是必需的并且是活动的,因此它注册了一些生命周期侦听器。 so when the activity is destroyed for example the XwalkView.onDestroy method is called (so I had to disable it in my case to keep the same instance and reuse it) 因此,当活动被破坏时,例如,将调用XwalkView.onDestroy方法(因此,在我的情况下,必须禁用它以保留相同的实例并重新使用它)

Here is My simple example: MainActivity.Java 这是我的简单示例: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) {

    }

}

The service class 服务等级

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

}

I hope it will work for you. 希望它对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM