1
0
Fork 0

增加自定义H5 API示例

master
xiaoyu 2020-12-03 16:48:01 +08:00
parent 8e3f724f7f
commit 0f29f6b0c1
7 changed files with 123 additions and 8 deletions

View File

@ -11,6 +11,9 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
/**
*
*/
public class InputContentActivity extends AppCompatActivity { public class InputContentActivity extends AppCompatActivity {
public static final String EXTRA_NAME_INPUT_CONTENT = "input_content"; public static final String EXTRA_NAME_INPUT_CONTENT = "input_content";

View File

@ -49,7 +49,19 @@ public class MainActivity extends AppCompatActivity {
btnCustomApi.setOnClickListener(new View.OnClickListener() { btnCustomApi.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fc8934aefb8c600019e9747"); Map<String, String> params = new HashMap<>();
params.put("path", "pages/index/index");
FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fc8934aefb8c600019e9747", params);
}
});
Button btnH5Api = findViewById(R.id.btn_h5_api);
btnH5Api.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, String> params = new HashMap<>();
params.put("path", "pages/webview/webview");
FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fc8934aefb8c600019e9747", params);
} }
}); });
} }

View File

@ -9,6 +9,7 @@ import com.finogeeks.lib.applet.client.FinAppClient;
import com.finogeeks.lib.applet.client.FinAppConfig; import com.finogeeks.lib.applet.client.FinAppConfig;
import com.finogeeks.lib.applet.interfaces.FinCallback; import com.finogeeks.lib.applet.interfaces.FinCallback;
import com.finogeeks.mop.demo.customapi.CustomApi; import com.finogeeks.mop.demo.customapi.CustomApi;
import com.finogeeks.mop.demo.customapi.CustomH5Api;
public class MopApplication extends MultiDexApplication { public class MopApplication extends MultiDexApplication {
@ -39,8 +40,10 @@ public class MopApplication extends MultiDexApplication {
@Override @Override
public void onSuccess(Object result) { public void onSuccess(Object result) {
Log.d(TAG, "init result : " + result); Log.d(TAG, "init result : " + result);
// 注册小程序自定义API // 注册自定义小程序API
FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new CustomApi(MopApplication.this)); FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new CustomApi(MopApplication.this));
// 注册自定义H5 API
FinAppClient.INSTANCE.getExtensionWebApiManager().registerApi(new CustomH5Api(MopApplication.this));
} }
@Override @Override

View File

@ -18,11 +18,11 @@ import static com.finogeeks.mop.demo.InputContentActivity.EXTRA_NAME_INPUT_CONTE
/** /**
* API * API
* APP * APP{@link InputContentActivity}
*/ */
public class CustomApi extends AbsApi { public class CustomApi extends AbsApi {
private static final int REQ_CODE_INPUT = 0x01; private static final int REQ_CODE_INPUT_CONTENT = 0x01;
private static final String API_NAME_ON_NATIVE = "onNative"; private static final String API_NAME_ON_NATIVE = "onNative";
@ -52,14 +52,14 @@ public class CustomApi extends AbsApi {
public void invoke(String event, JSONObject param, ICallback callback) { public void invoke(String event, JSONObject param, ICallback callback) {
if (API_NAME_ON_NATIVE.equals(event)) { if (API_NAME_ON_NATIVE.equals(event)) {
Intent intent = new Intent(mContext, InputContentActivity.class); Intent intent = new Intent(mContext, InputContentActivity.class);
callback.startActivityForResult(intent, REQ_CODE_INPUT); callback.startActivityForResult(intent, REQ_CODE_INPUT_CONTENT);
} }
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data, ICallback callback) { public void onActivityResult(int requestCode, int resultCode, Intent data, ICallback callback) {
super.onActivityResult(requestCode, resultCode, data, callback); super.onActivityResult(requestCode, resultCode, data, callback);
if (requestCode == REQ_CODE_INPUT) { if (requestCode == REQ_CODE_INPUT_CONTENT) {
if (resultCode == RESULT_OK && data != null) { if (resultCode == RESULT_OK && data != null) {
String inputContent = data.getStringExtra(EXTRA_NAME_INPUT_CONTENT); String inputContent = data.getStringExtra(EXTRA_NAME_INPUT_CONTENT);
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();

View File

@ -0,0 +1,85 @@
package com.finogeeks.mop.demo.customapi;
import android.content.Context;
import android.content.Intent;
import androidx.annotation.NonNull;
import com.finogeeks.lib.applet.api.AbsApi;
import com.finogeeks.lib.applet.interfaces.ICallback;
import com.finogeeks.mop.demo.InputContentActivity;
import com.finogeeks.mop.demo.R;
import org.json.JSONException;
import org.json.JSONObject;
import static android.app.Activity.RESULT_OK;
import static com.finogeeks.mop.demo.InputContentActivity.EXTRA_NAME_INPUT_CONTENT;
/**
* H5 API
* APP{@link InputContentActivity}
*/
public class CustomH5Api extends AbsApi {
private static final int REQ_CODE_INPUT_CONTENT = 0x02;
private static final String API_NAME_USER_DEFINE_NATIVE = "user_define_native";
@NonNull
private Context mContext;
public CustomH5Api(@NonNull Context context) {
mContext = context;
}
/**
* @return api
*/
@Override
public String[] apis() {
return new String[]{API_NAME_USER_DEFINE_NATIVE};
}
/**
* apiapi
*
* @param event api
* @param param
* @param callback
*/
@Override
public void invoke(String event, JSONObject param, ICallback callback) {
if (API_NAME_USER_DEFINE_NATIVE.equals(event)) {
Intent intent = new Intent(mContext, InputContentActivity.class);
callback.startActivityForResult(intent, REQ_CODE_INPUT_CONTENT);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data, ICallback callback) {
super.onActivityResult(requestCode, resultCode, data, callback);
if (requestCode == REQ_CODE_INPUT_CONTENT) {
if (resultCode == RESULT_OK && data != null) {
String inputContent = data.getStringExtra(EXTRA_NAME_INPUT_CONTENT);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("text", inputContent);
callback.onSuccess(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
callback.onFail();
}
} else {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("errMsg", mContext.getString(R.string.fin_clip_get_input_content_failed));
callback.onFail(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
callback.onFail();
}
}
}
}
}

View File

@ -56,8 +56,19 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:text="@string/fin_clip_custom_api" android:text="@string/fin_clip_custom_api"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toTopOf="@+id/btn_h5_api"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_profile" /> app:layout_constraintTop_toBottomOf="@+id/btn_profile" />
<Button
android:id="@+id/btn_h5_api"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/fin_clip_custom_h5_api"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_custom_api" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -3,7 +3,8 @@
<string name="fin_clip_charts_applet">绘图小程序</string> <string name="fin_clip_charts_applet">绘图小程序</string>
<string name="fin_clip_demo_applet">官方小程序</string> <string name="fin_clip_demo_applet">官方小程序</string>
<string name="fin_clip_profile_applet">智能对账单</string> <string name="fin_clip_profile_applet">智能对账单</string>
<string name="fin_clip_custom_api">自定义API</string> <string name="fin_clip_custom_api">自定义小程序API示例</string>
<string name="fin_clip_custom_h5_api">自定义H5 API示例</string>
<string name="fin_clip_input_content_hint">请输入内容</string> <string name="fin_clip_input_content_hint">请输入内容</string>
<string name="fin_clip_confirm">确定</string> <string name="fin_clip_confirm">确定</string>
<string name="fin_clip_get_input_content_failed">获取输入内容失败</string> <string name="fin_clip_get_input_content_failed">获取输入内容失败</string>