react native 接入QQ登陆(Android)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react native 接入QQ登陆(Android)相关的知识,希望对你有一定的参考价值。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  • 注册 Activity
<activity
    android:name="com.tencent.tauth.AuthActivity"
    android:launchMode="singleTask"
    android:noHistory="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="tencent你的AppId" />
    </intent-filter>
</activity>
<activity
    android:name="com.tencent.connect.common.AssistActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="behind"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
  • MainActivity.java
public class MainActivity extends ReactActivity {

    static MyListener listener;
    static Tencent tencent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tencent = Tencent.createInstance("AppId", this);
        listener = new MyListener();

    }

    private static class MyListener implements IUiListener {
        @Override
        public void onComplete(Object o) {
            JSONObject jsonObject = (JSONObject) o;
            try {
                String openid = jsonObject.getString("openid");
                WritableMap map = Arguments.createMap();
                map.putString("openid", openid);
                MyModule.promise.resolve(map);
            } catch (JSONException e) {
                MyModule.promise.reject(new Exception("操作失败"));
            }

        }

        @Override
        public void onError(UiError uiError) {
            MyModule.promise.reject(new Exception("操作失败"));
        }

        @Override
        public void onCancel() {
            MyModule.promise.reject(new Exception("操作失败"));
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, listener);
    }
}
  • MyModule.java 原生模块,供JS调用
public class MyModule extends ReactContextBaseJavaModule {

    private static ReactApplicationContext reactApplicationContext;
    public static Promise promise;

    @NonNull
    @Override
    public String getName() {
        return "MyModule";
    }

    MyModule(@NonNull ReactApplicationContext reactContext) {
        super(reactContext);
        reactApplicationContext = reactContext;
    }

    @ReactMethod
    public void qqLogin(Promise promise) {
        MyModule.promise = promise;
        MainActivity.tencent.login(getCurrentActivity(), "all", MainActivity.listener);
    }

}
  • MyPackage.java 注册模块
public class MyPackage implements ReactPackage {
    @NonNull
    @Override
    public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new MyModule(reactContext));
        return modules;
    }

    @NonNull
    @Override
    public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }
}
import {
  ToastAndroid,
  NativeModules
} from ‘react-native‘;

NativeModules.MyModule.qqLogin().then(({ openid }) => {
      ToastAndroid.show(openid, ToastAndroid.SHORT)
    }).catch(reason => {
      console.log(reason)
    })

以上是关于react native 接入QQ登陆(Android)的主要内容,如果未能解决你的问题,请参考以下文章

Android项目接入React Native方案

React Native 接入微博微信QQ 登录功能

react native 接入支付宝支付

react-native安卓接入高德地图

react Native环境 搭建

第三方登陆——QQ登陆详解