2016-01-06 17 views
8

Tôi đang làm việc để thay thế chế độ xem web bản địa trong ứng dụng Android của chúng tôi bằng cách triển khai Lối đi qua đường.Cách tạo XWalkView trong một dịch vụ?

Chúng tôi đã có thể tận dụng tối đa chức năng của ứng dụng nhưng việc tạo XWalkView trong một dịch vụ vẫn là vấn đề mà chúng tôi đang cố gắng vượt qua. Tạo một Webview không phải là một vấn đề nhưng XWalkView yêu cầu một bối cảnh hoạt động được sử dụng. Nếu bất cứ ai ở đây đã chạy qua vấn đề này và biết về một giải pháp có thể có hoặc làm việc xung quanh, tôi sẽ đánh giá cao nó rất nhiều. Cảm ơn và nếu bạn cần bất kỳ thông tin nào khác, vui lòng yêu cầu.

+0

bạn đang làm việc trên android studio hay nhật thực? và chính xác đâu là vấn đề khi sử dụng XWalkView rất đơn giản và tương tự như WebView, bạn có gặp vấn đề với cấu hình không? nhúng nội dung? đoạn trích html5 cụ thể? –

+0

Tôi có XWalkView hoạt động bình thường trong một Hoạt động. Tuy nhiên, việc sử dụng nó trong Dịch vụ là nơi tôi gặp sự cố vì không có Hoạt động nào để truyền cho hàm tạo. – JGray

Trả lời

3

Từ butelo trong GitHub:

Vì vậy, crosswalk là gì và tại sao tôi quan tâm? Hãy xem tại website: https://crosswalk-project.org/

crosswalk là một thời gian chạy HTML5, bạn có thể sử dụng nó để tạo ra các ứng dụng HTML5 với 'tính năng bản địa' Bạn có thể sử dụng crosswalk để tạo HTML5 chỉ các ứng dụng dành cho Android (x86 và cánh tay kiến ​​trúc) và Tizen nhưng bạn cũng có thể sử dụng CrossWalk làm Chế độ xem trong dự án android .

Điều này có nghĩa bạn có thể thay thế Android WebView với XWalkView và nhận được một số tính năng bổ sung như:

-WebGl

-WebRTC

-WebAudio

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

Làm thế nào để nhúng một WebView CrossWalk, từ bây giờ trên một XWalkView, bên trong một ứng dụng Android để có tất cả các tính năng này trên hy vọng của tôi Brid ứng dụng (Android Native với các tính năng html5)

Trước tiên, bạn phải tải về runtime:

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

Tải về bất kỳ (ARM) phiên bản Android.

Bên trong tệp là mọi thứ bạn cần để bắt đầu làm việc trong ứng dụng html5.

Đối với thử nghiệm này, chúng tôi sẽ cần phải nhập khẩu các dự án xwalk-core-thư viện bên trong Eclipse của chúng tôi

Tạo một dự án Android mới với một liên kết Hoạt động cơ bản nó với thư viện và đặt mã này trong Hoạt động:

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

đặt này trên bố cục chính của bạn

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

cuối cùng bên trong thư mục /assets/www bạn đặt công cụ html của bạn và đó là nó

+1

Cảm ơn bạn đã viết kỹ lưỡng. Tuy nhiên, tôi hoàn toàn hiểu những gì bạn vừa giải thích. Vấn đề của chúng tôi không phải là tạo ra XWalkView trong các trường hợp bình thường mà là tạo ra một dịch vụ trong một dịch vụ mà một Hoạt động có thể hoặc có thể không được tạo ra là vấn đề của chúng tôi. – JGray

1

Tôi đã tìm mã XWalkView.java để tôi có thể tạo một thứ gì đó hữu ích nhưng chưa được xuất bản. Nhưng có ít nhất một giải pháp tốt có thể hoạt động: Tạo lần đầu tiên phiên bản XWalkView bên trong một hoạt động.sau đó tìm cách lưu trữ nó trong một dịch vụ và tái sử dụng cùng một trường hợp bất cứ khi nào hoạt động của bạn kết nối với dịch vụ (theo cách này, html và js của bạn không được tải lại;))

Sau khi googling, tôi hiểu rằng trường hợp XWalkView cần và hoạt động để nó đăng ký một số người nghe vòng đời. vì thế khi hoạt động bị phá hủy ví dụ phương pháp XwalkView.onDestroy được gọi là (vì vậy tôi đã phải vô hiệu hóa nó trong trường hợp của tôi để giữ cho các ví dụ tương tự và tái sử dụng nó)

Dưới đây là ví dụ đơn giản của tôi: 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) { 

    } 

} 

lớp dịch vụ

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

} 

tôi hy vọng nó sẽ làm việc cho bạn.

Các vấn đề liên quan