tencent cloud

Feedback

Customization by the Mini Program host

Last updated: 2024-03-04 22:47:43
    Through the following configurations, the host is required to customize certain features of the mini program.
    @ProxyService(proxy = MiniAppProxy.class)
    public class MiniAppProxyImpl extends BaseMiniAppProxy{}
    Define an implementation class and inherit BaseMiniAppProxyImpl, and decorate it with the aforementioned annotations.

    Customize the Capsule

    /**
    * Selecting the "Close" option on the capsule button.
    * Invocation Environment: Subprocess
    *
    * @param miniAppContext - The operating environment of the mini program (mini program process, not the main process)
    * @param onCloseClickedListener - Callback upon clicking to close the mini program.
    * @return - If this interface is not supported, please return "false".
    */
    public abstract boolean onCapsuleButtonCloseClick(IMiniAppContext miniAppContext,
    DialogInterface.OnClickListener onCloseClickedListener);
    
    /**
    * Return the buttons for the capsule's extended panel. The ID of the extended button must be set within the range of [100, 200], otherwise, the addition will be invalid.
    * Invocation Environment: Subprocess
    *
    * @param builder
    * @return
    */
    public abstract ArrayList<MoreItem> getMoreItems(MoreItemList.Builder builder);
    
    /**
    * Return to the capsule and click the "Listener" button in the "More" panel that pops up.
    * Invocation Environment: Subprocess
    *
    * @return - Listener
    */
    public abstract OnMoreItemSelectedListener getMoreItemSelectedListener();
    Reference implementation example for getMoreItems.
    @Override
    public ArrayList<MoreItem> getMoreItems(IMiniAppContext miniAppContext, MoreItemList.Builder builder) {
    MoreItem item1 = new MoreItem();
    item1.id = ShareProxyImpl.OTHER_MORE_ITEM_1;
    item1.text = getString(miniAppContext, R.string.applet_mini_proxy_impl_other1);
    item1.drawable = R.mipmap.mini_demo_about;
    
    MoreItem item2 = new MoreItem();
    item2.id = ShareProxyImpl.OTHER_MORE_ITEM_2;
    item2.text = getString(miniAppContext, R.string.applet_mini_proxy_impl_other2);
    item2.shareKey = SHARE_TWITTER; // The key for custom sharing, which must be set and unique, and will be used when the Mini Program end calls for control settings.
    item2.drawable = R.mipmap.mini_demo_about;
    
    MoreItem item3 = new MoreItem();
    item3.id = DemoMoreItemSelectedListener.CLOSE_MINI_APP;
    item3.text = getString(miniAppContext, R.string.applet_mini_proxy_impl_float_app);
    item3.drawable = R.mipmap.mini_demo_about;
    
    MoreItem item4 = new MoreItem();
    item4.id = ShareProxyImpl.OTHER_MORE_ITEM_INVALID;
    item4.text = getString(miniAppContext, R.string.applet_mini_proxy_impl_out_of_effect);
    item4.drawable = R.mipmap.mini_demo_about;
    
    // Adjust the sequence independently.
    builder.addMoreItem(item1)
    .addMoreItem(item2)
    .addShareQQ("QQ", R.mipmap.mini_demo_channel_qq)
    .addMoreItem(item3)
    .addShareQzone(getString(miniAppContext, R.string.applet_mini_proxy_impl_Qzone),
    R.mipmap.mini_demo_channel_qzone)
    .addShareWxFriends(getString(miniAppContext, R.string.applet_mini_proxy_impl_wechat_friend),
    R.mipmap.mini_demo_channel_wx_friend)
    .addShareWxMoments(getString(miniAppContext, R.string.applet_mini_proxy_impl_wechat_group),
    R.mipmap.mini_demo_channel_wx_moment)
    .addRestart(getString(miniAppContext, R.string.applet_mini_proxy_impl_restart),
    R.mipmap.mini_demo_restart_miniapp)
    .addAbout(getString(miniAppContext, R.string.applet_mini_proxy_impl_about),
    R.mipmap.mini_demo_about)
    .addDebug(getString(miniAppContext, R.string.mini_sdk_more_item_debug),
    R.mipmap.mini_demo_about)
    .addMonitor(getString(miniAppContext, R.string.applet_mini_proxy_impl_performance),
    R.mipmap.mini_demo_about)
    .addComplaint(getString(miniAppContext, R.string.applet_mini_proxy_impl_complain_and_report),
    R.mipmap.mini_demo_browser_report)
    .addSetting(getString(miniAppContext, R.string.mini_sdk_more_item_setting),
    R.mipmap.mini_demo_setting);
    
    return builder.build();
    }
    
    private String getString(IMiniAppContext miniAppContext, int id) {
    return miniAppContext.getContext().getString(id);
    }
    Reference implementation for getMoreItemSelectedListener.
    /** * Click 'listener' within the additional panel buttons of the capsule. * * @return */ @Override public OnMoreItemSelectedListener getMoreItemSelectedListener() { return new DemoMoreItemSelectedListener(); }
    
    public class DemoMoreItemSelectedListener extends DefaultMoreItemSelectedListener { public static final int CLOSE_MINI_APP = 150; @Override public void onMoreItemSelected(IMiniAppContext miniAppContext, int moreItemId) { // Handle developer-defined click events (excluding custom sharing events) switch (moreItemId) { case CLOSE_MINI_APP: close(miniAppContext); return; case OTHER_MORE_ITEM_1: miniAppContext.getAttachedActivity().runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(miniAppContext.getAttachedActivity(), "custom menu click", Toast.LENGTH_SHORT).show(); } }); return; } // Handle built-in sharing and developer-defined sharing, such as: Weibo, Twitter, etc. super.onMoreItemSelected(miniAppContext, moreItemId); } public void close(IMiniAppContext miniAppContext) { Activity activity = miniAppContext.getAttachedActivity(); if (activity != null && !activity.isFinishing()) { boolean moved = activity.moveTaskToBack(true); if (!moved) { QMLog.e("Demo", "moveTaskToBack failed, finish the activity."); activity.finish(); } } } }

    Album Selection and Image Preview

    /**
    * Open the image selection interface.
    * Invocation Environment: Subprocess
    *
    * @param context Current Activity
    * @param maxSelectedNum Maximum number of selections allowed.
    * @param listener Callback interface.
    * @return - If this interface is not supported, please return "false".
    */
    public abstract boolean openChoosePhotoActivity(Context context, int maxSelectedNum, IChoosePhotoListner listner);
    
    /**
    * Open the image preview interface.
    * Invocation Environment: Subprocess
    *
    * @param context Current Activity
    * @param selectedIndex Index of the currently selected image.
    * @param pathList List of image paths.
    * @return - If this interface is not supported, please return "false".
    */
    public abstract boolean openImagePreview(Context context, int selectedIndex, List<String> pathList);

    Customize the authorization UI

    When the Mini Program API requires authorization, the SDK provides the following authorization UI style by default. Developers can also customize the authorization UI style using the following methods.
    /**
    * Customize the authorization pop-up view.
    * Invocation Environment: Subprocess
    *
    * @param context
    * @param authInfo
    * @param authView
    * @return true: Customized authorization view; false: Use the built-in feature.
    */
    @Override
    public boolean authView(Context context, MiniAuthInfo authInfo, IAuthView authView) {
    return true;
    }

    Customize the authorized user information

    Implementing the following code in BaseMiniAppProxyImpl allows customization of user nickname and avatar in user authorization information.
    /**
    * Obtain scope.userInfo for user authorization authorization.
    * Invocation Environment: Subprocess
    *
    * @param appId
    * @param result
    */
    @Override
    public void getUserInfo(String appId, AsyncResult result) {
    JSONObject jsonObject = new JSONObject();
    try {
    //Return nickname.
    jsonObject.put("nickName", "userInfo Test");
    //Return avatar URL.
    jsonObject.put("avatarUrl", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.daimg.com%2Fuploads%2Fallimg%2F210114%2F1-210114151951.jpg&refer=http%3A%2F%2Fimg.daimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1673852149&t=e2a830d9fabd7e0818059d92c3883017");
    result.onReceiveResult(true, jsonObject);
    } catch (JSONException e) {
    e.printStackTrace();
    }
    }
    Consider the following method for implementing avatar loading:
    public Drawable getDrawable(Context context, String source, int width, int hight, Drawable defaultDrawable)

    Isolated storage of mini app data by account

    When switching accounts, if there are running Mini programs under the current account, it is recommended to call stopAllMiniApp to stop the running applets, and then switch accounts.
    /**
    * Specify a unique user account to enable isolated data storage for that account. It's recommended not to use sensitive information.
    * Calling environment: main process
    */
    @Override
    public String getAccount() {
    return "tmf_test";
    }

    Mini programs image loading

    When loading network or local pictures in Mini programs, the following interfaces need to be implemented:
    /**
    * Get Drawable Object
    * Calling environment: child process
    *
    * @param context Context
    * @param source Source, either local or network
    * @param width picture width
    * @param hight picture height
    * @param defaultDrawable Default picture for loading and failed loads
    */
    @Override
    public Drawable getDrawable(Context context, String source, int width, int hight, Drawable defaultDrawable)
    Reference example in demo
    @Override
    public Drawable getDrawable(Context context, String source, int width, int hight, Drawable defaultDrawable) {
    //Access party accesses its own ImageLoader
    //Open source universalimageloader in demo
    UniversalDrawable drawable = new UniversalDrawable();
    if (TextUtils.isEmpty(source)) {
    return drawable;
    }
    drawable.loadImage(context, source);
    return drawable;
    }

    Customizing domain names

    Mini programs call the default virtual domain name. This domain name is not a real domain name and cannot be accessed in the browser. If you need to customize it, you can set it according to the following configuration.
    
    @Override public MiniConfigData configData(Context context, int configType, JSONObject params) { if(configType == MiniConfigData.TYPE_DOMAIN) { //Virtual Domain Name Configuration MiniConfigData.DomainConfig domainConfig = new MiniConfigData.DomainConfig(); domainConfig.domain = "test.com"; return new MiniConfigData .Builder() .domainConfig(domainConfig) .build(); } return new MiniConfigData .Builder() .build(); }

    Customizing userAgent

    You can customize the userAgent of the WebView in the following ways.
    @Override
    public MiniConfigData configData(Context context, int configType, JSONObject params) {
    if(configType == MiniConfigData.TYPE_WEBVIEW) {
    //webView userAgent
    String ua = params.optString(MiniConfigData.WebViewConfig.WEBVIEW_CONFIG_UA);
    MiniConfigData.WebViewConfig webViewConfig = new MiniConfigData.WebViewConfig();
    //Set the userAgent that the developer needs to add. Note: Do not set the original ua.
    webViewConfig.userAgent = "key/value";
    
    return new MiniConfigData
    .Builder()
    .webViewConfig(webViewConfig)
    .build();
    }
    
    return new MiniConfigData
    .Builder()
    .build();
    }

    Customizing code scanning

    The code scanning API function of Mini programs can use the code scanning extension library provided by the platform to scan code, and also supports custom code scanning.
    /**
    * Scan QR code
    *
    * @param context Context
    * @param onlyFromCamera only allows the camera to scan
    * @param result scan results
    * @return true: Custom scan code;false: Use built-in scan code
    */
    @Override
    public boolean enterQRCode(Context context, boolean onlyFromCamera, AsyncResult result) {
    return false;
    }
    /*** Scan QR code** @param context        Context* @param onlyFromCamera only allows the camera to scan* @param result         scan results* @return true: Custom scan code;false: Use built-in scan code*/@Overridepublic boolean enterQRCode(Context context, boolean onlyFromCamera, AsyncResult result) {return false;}

    Save file directory

    /**
    * specify the Mini programs API to save images, videos to the system album directory
    *
    */
    public abstract String getSaveFileDir();

    Mini programs Life Cycle

    /**
    * Mini programs Lifecycle Event Callbacks:
    * Call Context: Main Process UI Thread
    *
    * @param appState The event state
    * @param event event
    */
    public abstract void onAppStateChange(@AppState int appState, MiniAppEvent event);
    

    Base Library Update

    /**
    * base library detects updates, whether to update
    * @param context
    * @param data Basic library information data
    * @return `true`: Update;`false`: Don't update, default to true
    */
    public abstract boolean isUpdateBaseLib(Context context, JSONObject data);
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support