tencent cloud

Feedback

Custom API example

Last updated: 2024-06-27 10:55:40

    Launching an Activity in a custom API

    Start a new Activity in the plugin and retrieve the data returned by the Activity.
    Sample code:
    Extending BaseJsPlugin to determine the custom API event name:
    @JsPlugin(secondary = true)
    public class CustomPlugin extends BaseJsPlugin {
    @JsEvent("testStartActivityForResult")
    public void testStartActivityForResult(final RequestEvent req) {
    }
    }
    Add a listener for the Activity result and remove it once processing is complete:
    Note:
    It is recommended to pair the registration and removal of listeners to avoid memory leak.
    @JsPlugin(secondary = true)
    public class CustomPlugin extends BaseJsPlugin {
    @JsEvent("testStartActivityForResult")
    public void testStartActivityForResult(final RequestEvent req) {
    Activity activity = req.activityRef.get();
    TmfMiniSDK.addActivityResultListener(new IActivityResultListener() {
    @Override
    public boolean doOnActivityResult(int requestCode, int resultCode, Intent data) {
    TmfMiniSDK.removeActivityResultListener(this);
    
    Log.i(ModuleApplet.TAG, data.getStringExtra("key"));
    req.ok();
    return true;
    }
    });
    }
    }
    To start a new activity:
    Note:
    The requestCode must be >= 1000000 to avoid conflicts with internal request codes, which could cause unexpected issues.
    @JsPlugin(secondary = true)
    public class CustomPlugin extends BaseJsPlugin {
    @JsEvent("testStartActivityForResult")
    public void testStartActivityForResult(final RequestEvent req) {
    Activity activity = req.activityRef.get();
    TmfMiniSDK.addActivityResultListener(new IActivityResultListener() {
    @Override
    public boolean doOnActivityResult(int requestCode, int resultCode, Intent data) {
    TmfMiniSDK.removeActivityResultListener(this);
    
    Log.i(ModuleApplet.TAG, data.getStringExtra("key"));
    req.ok();
    return true;
    }
    });
    
    //Note: requestCode must be >= 1000000 to avoid conflicts with internal request codes
    activity.startActivityForResult(new Intent(activity, TransActivity.class), 1000000);
    }
    }

    Calling third-party apps in custom APIs

    When calling other third-party apps (e.g., for sharing, payment, login) within a custom API, ensure that the operation returns directly to the mini program instead of the host app.
    Firstly, developers create transparent auxiliary Activity.
    Define a transparent helper activity, such as TransActivity. The transparency settings depend on the theme configuration, which is related to the system Activity that your app's Activity inherits from.
    <activity android:name=".activity.TransActivity"
    android:exported="false"
    android:screenOrientation="portrait"
    android:theme="@style/TransparentTheme"/>
    And then invoke that business logic in the TransActivity.
    public class TransActivity extends AppCompatActivity {
       @Override
       protected void onCreate(@Nullable Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.applet_activity_tran);
     
           /*************************
                   Business development
           *************************/
           
           //After completing the business logic, return data and call finish
           Intent intent = new Intent();
           intent.putExtra("key", "value");
           setResult(RESULT_OK, intent);
           finish();    
        }
    }
    The plug-in starts the activity and listens for the data to return.
    @JsPlugin(secondary = true)
    public class CustomPlugin extends BaseJsPlugin {
    @JsEvent("testStartActivityForResult")
    public void testStartActivityForResult(final RequestEvent req) {
    Activity activity = req.activityRef.get();
    TmfMiniSDK.addActivityResultListener(new IActivityResultListener() {
    @Override
    public boolean doOnActivityResult(int requestCode, int resultCode, Intent data) {
    TmfMiniSDK.removeActivityResultListener(this);
    
    Log.i(ModuleApplet.TAG, data.getStringExtra("key"));
    req.ok();
    return true;
    }
    });
    
    //Note: requestCode must be >= 1000000 to avoid conflicts with internal request codes
    activity.startActivityForResult(new Intent(activity, TransActivity.class), 1000000);
    }
    }
    
    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