自定义Facebook登录以与MethodChannel一起使用

时间:2017-06-23 09:52:51

标签: java android facebook flutter

我试图通过使用一些本机代码为Flutter创建一个Facebook身份验证插件。我已经让代码处理完全原生的测试项目,但无法在android插件项目中使用它。我不是Android人,所以我对此的了解非常有限。但这就是我所拥有的:

public class FacebookSignInPlugin implements MethodCallHandler {

  CallbackManager callbackManager;
  AccessToken token;

  /**
   * Plugin registration.
   */
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "facebook_sign_in");
    channel.setMethodCallHandler(new FacebookSignInPlugin());
  }


  @Override
  public void onMethodCall(MethodCall call, Result result) {
    if (call.method.equals("signInUser")) {
      callbackManager = CallbackManager.Factory.create();
      LoginManager.getInstance().registerCallback(callbackManager, new     FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

          token = loginResult.getAccessToken();
          Log.d("Facebook", token.toString());
        }

        @Override
        public void onCancel() {
          System.out.println("cancel");
          Log.d("Facebook", "Cancel");
        }

        @Override
        public void onError(FacebookException error) {

        }
      });
      login();
      result.success("It works on Android");
    } else {
      result.notImplemented();
    }
  }

  @Override
  protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
  }

  public void login() {
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
  }

  public void logout() {
    LoginManager.getInstance().logOut();
  }
}

我想使用最后两个功能:登录,注销。但是在方法&#34; onActivityResult&#34;我遇到的问题是无法解决onActivityResult和&#34; login&#34;我在loginWithReadPermissions中遇到了同样的问题。 一切都应该正确导入。 任何一个有一些Android经验可以提供帮助的人?

2 个答案:

答案 0 :(得分:5)

要在Flutter插件中使用onActivityResult,您必须做两件事:

  1. 实施PluginRegistry.ActivityResultListener界面
  2. 将插件添加到registerWith静态方法中的ActivityResultListeners列表中。
  3. 删除MethodCall位的简化示例:

    public class FacebookSignInPlugin implements MethodCallHandler,
          // Implement PluginRegistry.ActivityResultListener
          PluginRegistry.ActivityResultListener {
    
      public static void registerWith(Registrar registrar) {
          final MethodChannel channel = new MethodChannel(registrar.messenger(), "my_plugin");
          final FacebookSignInPlugin instance = new MyPlugin();
    
          // Register your plugin as an ActivityResultListener
          registrar.addActivityResultListener(instance);
          channel.setMethodCallHandler(instance);
      }
    
      private CallbackManager callbackManager = CallbackManager.Factory.create();
    
      @Override
      public void onMethodCall(MethodCall call, final Result result) {
      }
    
      @Override
      public boolean onActivityResult(int i, int i1, Intent intent) {
          // Forward the activity result to the Facebook CallbackManager
          callbackManager.onActivityResult(i, i1, intent);
          return false;
      }
    }
    

答案 1 :(得分:1)

如果您在2020年编写自己的Flutter插件并尝试实现调用onActivitResult的方法,请尝试此操作。同时,插件API发生了变化,除了仅在registerWith中注册侦听器外,您还必须实现ActivityAware接口并在ActivityResultListner方法中注册onAttachedToActivity

class PayFlutterPlugin() : FlutterPlugin, MethodCallHandler, ActivityAware,
    ActivityResultListener {
   
    private var activityPluginBinding: ActivityPluginBinding? = null
    private var result: Result? = null

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        activityPluginBinding = binding
        binding.addActivityResultListener(this)
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {}

    override fun onDetachedFromActivityForConfigChanges() {}

    override fun onDetachedFromActivity() {
        activityPluginBinding?.removeActivityResultListener(this)
        activityPluginBinding = null
    }

    override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent
    ): Boolean {
        // React to activity result and if request code == ResultActivity.REQUEST_CODE
        return when (resultCode) {
            Activity.RESULT_OK -> {
                result?.success("success") // pass your result data
                true
            }
            else -> false
        }
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        this.result = result
        val activity = activityPluginBinding?.activity ?: return
        when (call.method) {
            "yourMethod" -> {
                val intent = Intent(activity, ResultActivity::class.java)

                activity.startActivityForResult(
                    intent,
                    ResultActivity.REQUEST_CODE
                )
            }
            else -> result.notImplemented()
        }
    }

}