其他分享
首页 > 其他分享> > react native 接入QQ登陆(Android)

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
    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" />
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 one rror(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);
    }
}
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);
    }

}
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)
    })

标签:QQ,void,public,react,promise,Override,new,Android,MyModule
来源: https://www.cnblogs.com/stefzi/p/13278148.html