diff --git a/app/src/main/java/com/finogeeks/mop/demo/InputContentActivity.java b/app/src/main/java/com/finogeeks/mop/demo/InputContentActivity.java index 4933275..aa60e15 100644 --- a/app/src/main/java/com/finogeeks/mop/demo/InputContentActivity.java +++ b/app/src/main/java/com/finogeeks/mop/demo/InputContentActivity.java @@ -11,6 +11,9 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +/** + * 输入内容页面 + */ public class InputContentActivity extends AppCompatActivity { public static final String EXTRA_NAME_INPUT_CONTENT = "input_content"; diff --git a/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java b/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java index 43d878c..32b273a 100644 --- a/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java +++ b/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java @@ -49,7 +49,19 @@ public class MainActivity extends AppCompatActivity { btnCustomApi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fc8934aefb8c600019e9747"); + Map 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 params = new HashMap<>(); + params.put("path", "pages/webview/webview"); + FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fc8934aefb8c600019e9747", params); } }); } diff --git a/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java b/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java index e8dc925..80fd5aa 100644 --- a/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java +++ b/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java @@ -9,6 +9,7 @@ import com.finogeeks.lib.applet.client.FinAppClient; import com.finogeeks.lib.applet.client.FinAppConfig; import com.finogeeks.lib.applet.interfaces.FinCallback; import com.finogeeks.mop.demo.customapi.CustomApi; +import com.finogeeks.mop.demo.customapi.CustomH5Api; public class MopApplication extends MultiDexApplication { @@ -39,8 +40,10 @@ public class MopApplication extends MultiDexApplication { @Override public void onSuccess(Object result) { Log.d(TAG, "init result : " + result); - // 注册小程序自定义API + // 注册自定义小程序API FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new CustomApi(MopApplication.this)); + // 注册自定义H5 API + FinAppClient.INSTANCE.getExtensionWebApiManager().registerApi(new CustomH5Api(MopApplication.this)); } @Override diff --git a/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomApi.java b/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomApi.java index b689eae..602c96d 100644 --- a/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomApi.java +++ b/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomApi.java @@ -18,11 +18,11 @@ import static com.finogeeks.mop.demo.InputContentActivity.EXTRA_NAME_INPUT_CONTE /** * 自定义小程序API - * 跳转到原生APP中输入内容的页面,获取到输入内容后将数据回传给小程序 + * 跳转到原生APP的输入内容页面{@link InputContentActivity},输入内容提交后,把输入的内容回传给小程序 */ 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"; @@ -52,14 +52,14 @@ public class CustomApi extends AbsApi { public void invoke(String event, JSONObject param, ICallback callback) { if (API_NAME_ON_NATIVE.equals(event)) { Intent intent = new Intent(mContext, InputContentActivity.class); - callback.startActivityForResult(intent, REQ_CODE_INPUT); + 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) { + if (requestCode == REQ_CODE_INPUT_CONTENT) { if (resultCode == RESULT_OK && data != null) { String inputContent = data.getStringExtra(EXTRA_NAME_INPUT_CONTENT); JSONObject jsonObject = new JSONObject(); diff --git a/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomH5Api.java b/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomH5Api.java new file mode 100644 index 0000000..93b7152 --- /dev/null +++ b/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomH5Api.java @@ -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(); + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4109c59..8f6bb0f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -56,8 +56,19 @@ android:layout_height="wrap_content" android:layout_marginTop="20dp" 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_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btn_profile" /> + +