Skip to content

Commit

Permalink
Merge pull request #5 from qluan/master
Browse files Browse the repository at this point in the history
支持native调用js方法同时传递json数据
  • Loading branch information
qluan authored Oct 26, 2016
2 parents a871645 + edb575b commit 6ef6234
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Demo 中使用 github 的 raw 文件服务提供一个简单的路由表文件 r
#### gradle

```groovy
compile 'com.douban.rexxar:core:0.1.1'
compile 'com.douban.rexxar:core:0.1.2'
```


Expand Down Expand Up @@ -205,6 +205,20 @@ Rexxar Container 提供了一些原生 UI 组件,供 Rexxar Web 使用。Rexxa
.build());
```

## 高级使用

### native调用js方法

```
// 方法名
RexxarWebView.callFunction(String functionName)
// 方法名和json数据
RexxarWebView.callFunction(String functionName, String jsonString)
```

## Partial RexxarWebView

如果,你发现一个页面无法全部使用 Rexxar 实现。你可以在一个原生页面内内嵌一个 `RexxarWebView`,部分功能使用原生实现,另一部分功能使用 Rexxar 实现。
Expand Down
4 changes: 2 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "0.1.0"
versionCode 2
versionName "0.1.2"
}
buildTypes {
release {
Expand Down
2 changes: 1 addition & 1 deletion core/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PROJ_GROUP=com.douban.rexxar
PROJ_VERSION=0.1.1
PROJ_VERSION=0.1.2
PROJ_NAME=rexxar-android
PROJ_WEBSITEURL=https://github.com/douban/rexxar-android
PROJ_ISSUETRACKERURL=https://github.com/douban/rexxar-android/issues
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/com/douban/rexxar/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,14 @@ public class Constants {
* Used return network error in Request Error
*/
public static final String KEY_NETWORK_ERROR = "_network_error";

/**
* Java call js function format with parameters
*/
public static final String FUNC_FORMAT_WITH_PARAMETERS = "javascript:window.%s('%s')";

/**
* Java call js function format without parameters
*/
public static final String FUNC_FORMAT = "javascript:window.%s()";
}
33 changes: 31 additions & 2 deletions core/src/main/java/com/douban/rexxar/view/RexxarWebView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.douban.rexxar.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -262,11 +263,11 @@ public void addRexxarWidget(RexxarWidget widget) {
}

public void onPageVisible() {
mCore.loadUrl("javascript:window.Rexxar.Lifecycle.onPageVisible()");
callFunction("Rexxar.Lifecycle.onPageVisible");
}

public void onPageInvisible() {
mCore.loadUrl("javascript:window.Rexxar.Lifecycle.onPageInvisible()");
callFunction("Rexxar.Lifecycle.onPageInvisible");
}

@Override
Expand Down Expand Up @@ -306,4 +307,32 @@ public void reload() {
mCore.loadPartialUri(mUri, this);
}
}

/**
* Native调用js方法, 传递参数
*
* @param functionName 方法名
*/
public void callFunction(String functionName) {
callFunction(functionName, null);
}

/**
* Native调用js方法, 传递参数
*
* @param functionName 方法名
* @param jsonString 参数,需要是json格式
*/
public void callFunction(String functionName, String jsonString) {
if (TextUtils.isEmpty(functionName)) {
return;
}
if (TextUtils.isEmpty(jsonString)) {
mCore.loadUrl(String.format(Constants.FUNC_FORMAT, functionName));
} else {
jsonString = jsonString.replaceAll("(\\\\)([^utrn])", "\\\\\\\\$1$2");
jsonString = jsonString.replaceAll("(?<=[^\\\\])(\")", "\\\\\"");
mCore.loadUrl(String.format(Constants.FUNC_FORMAT_WITH_PARAMETERS, functionName, jsonString));
}
}
}
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.douban.rexxar.example"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "0.1"
versionCode 2
versionName "0.1.2"
}

lintOptions {
Expand Down
21 changes: 21 additions & 0 deletions sample/src/main/java/com/douban/rexxar/example/RexxarActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.douban.rexxar.example.widget.menu.MenuWidget;
import com.douban.rexxar.view.RexxarWebView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -65,6 +68,23 @@ protected void onCreate(Bundle savedInstanceState) {
mRexxarWebView.loadUri(uri);
}

@Override
protected void onResume() {
super.onResume();
// testFunc();
}

private void testFunc() {
try {
JSONObject user = new JSONObject();
user.put("name", "name");
user.put("age", 18);
mRexxarWebView.callFunction("alert", user.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}

public void setMenuItems(List<MenuItem> menuItems) {
if (null == menuItems || menuItems.size() == 0) {
return;
Expand All @@ -81,4 +101,5 @@ public boolean onCreateOptionsMenu(Menu menu) {
}
return super.onCreateOptionsMenu(menu);
}

}

0 comments on commit 6ef6234

Please sign in to comment.