码迷,mamicode.com
首页 > 移动开发 > 详细

react native 接入QQ登陆(Android)

时间:2020-07-10 11:28:57      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:override   arraylist   can   scree   object   ctc   short   rom   stc   

<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();
    }
}
  • JavaScript 端访问
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)

标签:override   arraylist   can   scree   object   ctc   short   rom   stc   

原文地址:https://www.cnblogs.com/stefzi/p/13278148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!