如何在React-Native中运行Java方法

时间:2018-09-26 18:29:01

标签: javascript java react-native

我有一个已经可以使用的Java方法,我想在react-native组件中获得该方法的结果。我对Java一点也不了解,但是我知道这段代码有效。无论如何,我可以在任何javascript文件中运行此java方法。

// Java文件

import com.twilio.jwt.accesstoken.AccessToken;
import com.twilio.jwt.accesstoken.VideoGrant;

  public class Main {

// Substitute your Twilio AccountSid and ApiKey details
    public static final String ACCOUNT_SID = "ACCOUNT_SID";
    public static final String API_KEY_SID = "API_KEY_SID";
    public static final String API_KEY_SECRET = "API_KEY_SECRET";

    public static void main(String[] args) throws Exception {
    // Create a VideoGrant
        final VideoGrant grant = new VideoGrant();
        grant.setRoom("cool room");

    // Create an Access Token
        final AccessToken token = new AccessToken.Builder(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET)
        .identity("example-user") // Set the Identity of this token
        .grant(grant) // Grant access to Video
        .build();

    // Serialize the token as a JWT
    final String jwt = token.toJWT();
    System.out.println(jwt);
  }
}

1 个答案:

答案 0 :(得分:1)

我假设您显示的代码实际上是特定于Android的。 在这种情况下,您将需要使用本机模块将Java方法的结果发送回JavaScript端。 官方文档提供了一个不错的示例:https://facebook.github.io/react-native/docs/native-modules-android