增加自定义H5 API示例
parent
8e3f724f7f
commit
0f29f6b0c1
|
@ -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";
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接收到对应的api调用时,会调用此方法,在此方法中处理api调用的功能逻辑
|
||||||
|
*
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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>
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue