安卓app打包chrome

安卓应用程序可以使用WebView来渲染网页内容,而WebView的内核可以使用安卓系统默认的WebKit内核或者Chrome的内核。在大多数情况下,使用Chrome内核的WebView会提供更好的兼容性和性能。

下面介绍如何在安卓应用程序中集成Chrome内核的WebView。

1. 添加依赖库

在项目的build.gradle文件中添加以下依赖:

```

dependencies {

implementation 'com.android.support:appcompat-v7:28.0.0'

implementation 'com.android.support:support-v4:28.0.0'

implementation 'com.android.support:design:28.0.0'

implementation 'com.android.support:customtabs:28.0.0'

}

```

2. 创建Chrome内核WebView

在布局文件中添加Chrome内核WebView:

```

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/custom_tabs_intent"

xmlns:android="http://schemas.android.com/apk/res/android"

/>

android:id="@+id/webview"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

```

在代码中加入以下代码创建Chrome内核的WebView:

```

private com.android.chrome.webkit.WebView webView;

// Create a WebView instance

webView = new com.android.chrome.webkit.WebView(this);

// set the WebView as the main container for the Activity

setContentView(webView);

```

3. 添加Chrome浏览器

为了使用户获得更好的浏览体验,在应用程序中集成Chrome浏览器是非常必要的。以下是如何添加Chrome浏览器的步骤。

在应用程序中添加以下代码:

```

import com.google.android.gms.common.GoogleApiAvailability;

import com.google.android.gms.common.ConnectionResult;

import com.google.android.gms.common.api.GoogleApiClient;

import com.google.android.gms.appindexing.Action;

import com.google.android.gms.appindexing.AppIndex;

public class MainActivity extends AppCompatActivity

implements GoogleApiClient.ConnectionCallbacks,

GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mClient;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Create a new GoogleApiClient instance

mClient = new GoogleApiClient.Builder(this)

.addApi(AppIndex.API)

.build();

// Connect to the GoogleApiClient

mClient.connect();

// ... Other code

}

@Override

public void onStart() {

super.onStart();

// Connect the client

mClient.connect();

}

@Override

public void onStop() {

super.onStop();

// Disconnect the client

mClient.disconnect();

}

@Override

public void onConnected(Bundle connectionHint) {

// Create a new Action for viewing the current URL in Chrome

Action viewAction = Action.newAction(

Action.TYPE_VIEW,

"Web Page",

Uri.parse("http://example.com"),

Uri.parse("android-app://com.example.app/http/example.com")

);

// Call the start(...) method to index the Action

AppIndex.AppIndexApi.start(mClient, viewAction);

}

@Override

public void onConnectionFailed(ConnectionResult connectionResult) {

// Ignore

}

@Override

public void onConnectionSuspended(int cause) {

// Ignore

}

}

```

4. 配置Chrome浏览器

打开Chrome Developer Tools 设置Android WebView userAgent,打开连续提供页面功能。

附Chrome Developer Tools入口:

1. 访问 URL:

chrome://flags/#remote-debugging

2. 可以搜到这个开关:

直接启用 Remote debugging on Android (Deprecated)

注意:打开连续提供页面功能,在web页面设定文件,添加如下代码:

```

```

这样,就成功添加了Chrome内核的WebView和Chrome浏览器。通过这种方式,你可以让你的用户在你的应用程序中获取更好的浏览体验。

川公网安备 51019002001728号