How to create an XWalkView in a service?

I am working on replacing the native web browser in our Android app with Crosswalk implementation.

We were able to get most of the functionality of the application, but creating an XWalkView inside the service is still the problem we are trying to overcome. Creating a Webview is not a problem, but XWalkView requires the use of an activity context. If someone here has run into this problem and is aware of a possible solution or workaround, I would really appreciate it. Thank you, and if you need any other information, please ask.

+8
java android service android-webview crosswalk
source share
2 answers

From butelo to github :

So what is a pedestrian crossing and why does it bother me? Take a look at the website: https://crosswalk-project.org/

CrossWalk is an HTML5 runtime, you can use it to create HTML5 applications with native features. You can use CrossWalk to create an HTML5-only application for Android (x86 and arm architecture) and Tizen, but you can also use CrossWalk as a view in an android project.

This means that you can replace Android WebView with XWalkView and get additional features, such as:

-Webgl

-WebRTC

-WebAudio

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

How to embed CrossWalk WebView with XWalkView in an Android application to have all this on my hybrid application (Android Native with html5 functions)

First you need to load the runtime:

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

Download any version of Android (ARM).

Inside the file is all you need to get started in your html5 application.

For this test, we will need to import the xwalk-core-library project inside our Eclipse

Create a new Android project with a basic action, move it to the library and put this code in 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 it 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, enter your html file and that it

+3
source share

I was looking for XWalkView.java code, so I can do something useful, but it has not been published yet. But there is at least a good solution that can work: Create the first time an XWalkView instance inside the action. then find a way to keep it in the service and reuse the same instance whenever your activity connects to the service (this way your html and js do not restart;))

After searching on Google, I realized that an XWalkView instance and activity was needed to register some lifecycle listeners. therefore, when the action is destroyed, for example, the XwalkView.onDestroy method is called (so I had to disable it in my case in order to save the same instance and reuse it)

Here is my simple example: 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) { } } 

Class of service

 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 this works for you.

+1
source share

All Articles