ZeroyiQ: Unity multi-platform native SDK access Overview (I) : wechat open platform

ZeroyiQ: Unity multi-platform native SDK access overview (ii) : QQ Interconnection

ZeroyiQ: Unity multi-platform native SDK Access Quick overview (3) : Facebook

ZeroyiQ: Unity multi-platform native SDK Access Overview (4) : Twitter

ZeroyiQ: Unity multi-platform native SDK access overview (five) : Weibo

One, foreword

QQ Interconnection, currently (2020-6-29) support individual developer certification and enterprise certification. Create an application and obtain the AppID and AppKey after approval.

2. SDK access

1. Add the JAR package

Download the JAR package and add it to the project LIBS path. (The Demo project is included in the Android SDK.)

2. Set permissions

Add network permissions to the androidmanifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Copy the code

3. To configure the Activity

Androidmanifest.xml to add the Activity configuration, fill in the AppId. Note the format is Tencent +AppId. For example, if AppId is 2222, enter Tencent2222.

<application> <activity android:name="com.tencent.tauth.AuthActivity" android:noHistory="true" android:launchMode="singleTask" > <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 "/> </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" /> <application>Copy the code

4. The initialization

Create an Tencent instance.

Public static final String QQ_ID = "your AppId"; Public static final String AUTHORITIES = ".fileProvider "; public static Tencent QQ_API; private void init() { if (! bIsInitialized) { bIsInitialized = true; // Tencent class is the main implementation class of the SDK. Developers can access Tencent's OpenAPI through Tencent class. // APP_ID is the APPID assigned to the third-party application. The type is String. QQ_API = tceh.createInstance (QQ_ID, activity.getApplicationContext(), AUTHORITIES); }}Copy the code

Note that the initialization depends on the FileProvider. If it is not added, you can find the Jar package from the Demo and add the Jar package as in step 1.

In androidmanifest.xml, replace the package name.

The create instance above passes in the AUTHORITIES value as Android: AUTHORITIES.

The < application > < the provider of the android: name = "android. Support. The v4. Content. FileProvider" android: authorities = "< package name >. FileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application>Copy the code

5. Implement the callback

By implementing the corresponding interface, creating and passing in the corresponding Listener, the callback listening of the request is realized. However, there are currently two callback interfaces.

IUiListener (SDK wrapper case)

For example, interfaces such as login, quick payment login, application sharing, and application invitation need to be passed the instance of the callback.

private class BaseUiListener implements IUiListener { public static final String TAG = "QQUiListener"; @Override public void onComplete(Object response) { if (null == response) { UnityCallApi.unityLogError(TAG, "QQ request error. response is null."); ToastUtils.show(activity, R.string.errcode_null); return; } JSONObject jsonResponse = (JSONObject) response; if (jsonResponse.length() == 0) { UnityCallApi.unityLogError(TAG, "QQ request error. response length is 0."); ToastUtils.show(activity, R.string.errcode_null); return; } UnityCallApi.unityLogInfo(TAG, "QQ request successfully."); ToastUtils.show(activity, R.string.errcode_success); // handlePrizeShare(); onSuccessful((JSONObject) response); } @Override public void onError(UiError e) { UnityCallApi.unityLogError(TAG, "QQ request error" + e.toString()); ToastUtils.show(activity, activity.getString(R.string.errcode_unknown) + ", type=" + e.errorMessage); onFailed(); } @Override public void onCancel() { UnityCallApi.unityLogInfo(TAG, "QQ request cancel."); ToastUtils.show(activity, R.string.errcode_cancel); onFailed(); } protected void onFailed() { } protected void onSuccessful(JSONObject values) { } }Copy the code

IRequestListener (SDK unwrapped)

When a common method such as requestAsync or Request is used to invoke an interface that is not encapsulated by the SDK, such as uploading a picture or viewing an album, an instance of the callback is required. (Slight use of SDK will not involve this interface, so the interface content will not be displayed)

In particular, in order to receive the callback, you need to add the following method to the onActivityResult of the Activity that calls the SDK interface. The method in the official documentation has been deprecated. The method in the Demo is currently used. The loginListener is an instance of BaseUiListener that handles login callbacks. The response callback can be determined based on the requestCode. For onActivityResult, see the introduction article, which switches back from otherActivity to run while the current Activity is running.

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, "-->onActivityResult " + requestCode + " resultCode=" + resultCode); If (requestCode = = the REQUEST_LOGIN | | requestCode = = the REQUEST_APPBAR) {/ / callback Tencent.onActivityResultData(requestCode,resultCode,data,loginListener); } super.onActivityResult(requestCode, resultCode, data); }Copy the code

Third, the login

1. Initiate a login request

After initializing Tencent, initiate the login request.

public void login(Activity activity) { this.activity = activity; // Check whether the session is valid if (! Qq_api.issessionvalid ()) {// all all scope, loginListener is the above login callback qq_api.login (activity, "all", loginListener); Log.d("SDKQQAgentPref", "FirstLaunch_SDK:" + SystemClock.elapsedRealtime()); }}Copy the code

2. Instantiate the callback

Instantiate the BaseUiListener class of the IUiListener we implemented earlier.

IUiListener loginListener = new BaseUiListener() { @Override protected void onSuccessful(JSONObject values) { UnityCallApi.unityLogInfo(TAG, "Login successful."); // Initialize openId and Token initOpenidAndToken(values); UpdateUserInfo (); } @Override protected void onFailed() { UnityCallApi.unityLogInfo(TAG, "Login failed."); }};Copy the code

Remember to add the corresponding onActivityResultData to the Activity’s onActivityResult.

3. Initialize the OpenId and Token

After successfully logging in using the login interface, the token returned by logging in and the expiration time are saved in the login callback interface.

private void initOpenidAndToken(JSONObject jsonObject) { try { String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN); String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN); String openId = jsonObject.getString(Constants.PARAM_OPEN_ID); if (! TextUtils.isEmpty(token) && ! TextUtils.isEmpty(expires) && ! TextUtils.isEmpty(openId)) { QQ_API.setAccessToken(token, expires); QQ_API.setOpenId(openId); QQ_API.saveSession(jsonObject); } } catch (Exception e) { e.printStackTrace(); UnityCallApi.unityLogError(TAG, "Parse response to json error."); }}Copy the code

4. Obtain user information

After login, send a request for user information.

private void updateUserInfo() { if (QQ_API ! = null && QQ_API.isSessionValid()) { IUiListener listener = new BaseUiListener() { @Override protected void OnSuccessful (JSONObject values) {/ / is passed to the Unity of parsing UnityCallApi. SendLoginInfoToUnity (true values. The toString ()); } @Override protected void onFailed() { UnityCallApi.sendLoginInfoToUnity(false, ""); }}; // Only after initializing the token can get UserInfo info = new UserInfo(activity, qq_api.getQqToken ()); info.getUserInfo(listener); }}Copy the code

Pass the returned information to Unity for parsing.

Json returned correctly.

Five, share,

1. Graphics (web page)

QQ sharing does not support direct sharing of pure text, must pass in the jump URL.

public void shareWebLink(Bundle params) { Bundle paramsIn = new Bundle(); paramsIn.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT); // Type default (required) paramsin.putString (qqshare.share_to_qq_target_URL," link "); // Link (required) paramsin.putString (qqshare.share_to_qq_title," title "); // Link title (required) paramsin.putString (qqshare.share_to_QQ_summary," Summary "); // Link summary (required) Paramsin.putString (qqshare.share_to_qq_image_url," image URL"); // Local or network image URL (Optional) Paramsin.putString (qqshare.share_to_QQ_app_name, APP_NAME); // App name Client top replace return button text (optional) Paramsin.putint (qqshare.share_to_QQ_ext_int, QQshare.share_to_QQ_FLAG_qzone_ITEM_hide); (Optional) hide QZone // Share Listener, ShareListener = new BaseUiListener() {@override protected void onSuccessful(JSONObject values) } @override protected void onFailed() {// failed}}; QQ_API.shareToQQ(activity, paramsIn, shareListener); }Copy the code

The qqshare. SHARE_TO_QQ_EXT_INT parameter values are shared to QZone in the following interface.

Because of the jump to QQ, don’t forget to add the following code to OnActivityResult in order to respond to the callback.

Override // This code is very important, Protected void onActivityResult(int requestCode, int resultCode, Intent data) {/ / it is QQ to share the results if (requestCode = = the REQUEST_QQ_SHARE) {Tencent. OnActivityResultData (requestCode, resultCode, data, shareListener); } super.onActivityResult(requestCode, resultCode, data); }Copy the code

2. Pure image

Note that pure image sharing only supports local images, not online images. The image size cannot exceed 5M.

public void shareImage(Bundle params) { Bundle paramsIn = new Bundle(); paramsIn.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE); // Type Image (required) paramsin.putString (qqshare.share_to_QQ_image_LOCAL_URL, "local Image Path"); // Local image path (required) Paramsin.putString (qqshare.share_to_QQ_app_name, APP_NAME); // App name Client top replace return button text (optional) Paramsin.putint (qqshare.share_to_QQ_ext_int, QQshare.share_to_QQ_FLAG_qzone_ITEM_hide); // Hide QZone (Optional) shareListener = new BaseUiListener() {@override protected void onSuccessful(JSONObject values) } @override protected void onFailed() {// failed}}; String localPath = paramsIn.getString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL); if (! TextUtils.isEmpty(localPath)) { File file = new File(localPath); If (file.length() >= 5 * 1024 * 1024) {// Size cannot exceed 5M shareListener.onError(new UiError(Constants.ERROR_IMAGE_TOO_LARGE, Constants. Constants.MSG_SHARE_IMAGE_TOO_LARGE_ERROR, null)); return; } } QQ_API.shareToQQ(activity, paramsIn, shareListener); }Copy the code

3. The application of

Sharing is actually very similar to text and text, the focus is still on the web page set by SHARE_TO_QQ_TARGET_URL.

public void shareApp(Bundle params) { Bundle paramsIn = new Bundle(); paramsIn.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_APP); // Type App (required) Paramsin.putString (qqshare.share_to_qq_title, "title "); // Link title (required) paramsin.putString (qqshare.share_to_QQ_summary, "Summary "); // Link abstract (required) Paramsin.putString (qqshare.share_to_qq_target_URL, "app link "); // Apply link (required) Paramsin.putString (qqshare.share_to_qq_image_url, "Apply icon link "); // Apply icon link (required) Paramsin.putString (qqshare.share_to_QQ_app_name, APP_NAME); // App name Client top replace return button text (optional) Paramsin.putint (qqshare.share_to_QQ_ext_int, QQshare.share_to_QQ_FLAG_qzone_ITEM_hide); // Hide QZone (Optional) shareListener = new BaseUiListener() {@override protected void onSuccessful(JSONObject values) } @override protected void onFailed() {// failed}}; QQ_API.shareToQQ(activity, paramsIn, shareListener); }Copy the code

Six, summarized

QQ interconnection access and use or simple. There is no need for wechat to obtain the code through login request, and then obtain the access token through code. And QQ Internet sharing, do not need to login is also possible. The official SDK compression package, not only SDK, but also Demo APK and engineering projects, this really want to praise.

Seven, references,

  1. Android_SDK environment construction – QQ Internet WIKI
  2. Using the Android FileProvider
  3. OnActivityResult -CSDN blog_protected void onActivityResult