官方文檔及SDK的下載地址為:http://wiki.open.qq.com/wiki/YSDK%E4%B8%8B%E8%BD%BD
在接入之前,需先在應(yīng)用寶后臺(tái)創(chuàng)建游戲,獲取一些接入?yún)?shù)。
客戶(hù)端所需要參數(shù)包括:手Q的app_id、微信的app_id 。
準(zhǔn)備工作做為,正式開(kāi)始接入,首先把應(yīng)用寶的YSDKLibrary引用庫(kù)導(dǎo)入到我們的工作環(huán)境中去,然后用我們demo工程去引用那個(gè)工程。
我這邊的接入環(huán)境是Eclipse
至于demo工程如何創(chuàng)建在上一篇的接入文檔中有詳細(xì)教程,請(qǐng)至:
http://www.cnblogs.com/laohaizei/p/6724250.html
首先我們打開(kāi)我們demo工程的AndroidManifest.xml文件。
然后復(fù)制以下代碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<!-- TODO GAME SDK接入必須權(quán)限模塊 START -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- TODO GAME SDK Midas接入配置 設(shè)置屏幕兼容聲明 START -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- TODO GAME SDK接入YSDK登陸接入配置 START -->
<activity
android:name="com.tencent.ysdk.module.realName.impl.RegisterRealNameActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
</activity>
<!-- TODO GAME SDK接入YSDK登陸接入配置 END -->
<!-- TODO GAME SDK QQ接入配置 START -->
<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@qq_app_id@" />
<!-- TODO GAME 這里為游戲QQ登錄的配置,游戲需要修改為自己手Q的appid -->
</intent-filter>
</activity>
<activity
android:name="com.tencent.connect.common.AssistActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- TODO GAME SDK QQ接入配置 END -->
<!-- TODO GAME SDK 微信接入配置 START -->
<activity
android:name="com.tencent.tmgp.yybtestsdk.wxapi.WXEntryActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:excludeFromRecents="true"
android:exported="true"
android:label="WXEntryActivity"
android:launchMode="singleTop"
android:taskAffinity="@package@.diff"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<!-- TODO GAME 這里為游戲微信登錄的配置,游戲需要修改為自己的包名加.diff -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="@wx_app_id@" />
<!-- TODO GAME 這里為游戲微信登錄的配置,游戲需要修改為自己的微信appid -->
</intent-filter>
</activity>
<!-- TODO GAME SDK 微信接入配置 END -->
<!-- TODO GAME SDK Midas接入配置 START -->
<activity
android:name="com.tencent.midas.proxyactivity.APMidasPayProxyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="sensorLandscape"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name="com.tencent.midas.wx.APMidasWXPayActivity"
android:exported="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name="com.tencent.midas.qq.APMidasQQWalletActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="true"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="qwallet100703379" />
<!-- TODO GAME 這里為米大師的默認(rèn)配置,游戲不要修改 -->
</intent-filter>
</activity>
<activity
android:name="com.tencent.midas.jsbridge.APWebJSBridgeActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateAlwaysHidden" />
<!-- TODO GAME SDK Midas接入配置 END -->
</application>
</manifest>
以上是需要添加在AndroidManifest.xml中的。(@xxx@部分請(qǐng)注意替換修改)
然后到我們demo工程的AndroidManifest.xml全選替換粘貼
這樣AndroidManifest.xml修改好了。
做完以上步驟,接下來(lái),我們開(kāi)始接入SDK的接口。
首先打開(kāi)我們的MainActivity.java
打開(kāi)的MainActivity.java應(yīng)該是這個(gè)樣子的
然后把下面的代碼復(fù)制進(jìn)去,注意別把第一行的代碼覆蓋了。
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import com.tencent.ysdk.api.YSDKApi;
import com.tencent.ysdk.framework.common.eFlag;
import com.tencent.ysdk.framework.common.ePlatform;
import com.tencent.ysdk.module.user.PersonInfo;
import com.tencent.ysdk.module.user.UserListener;
import com.tencent.ysdk.module.user.UserLoginRet;
import com.tencent.ysdk.module.user.UserRelationRet;
import com.tencent.ysdk.module.user.WakeupRet;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private final static String TAG = "demo";
public static String openId = "";
public static String pf = "";
public static String pfKey = "";
public static String accessToken = "";
public static String payToken = "";
public Context context = this;
public Activity activity = MainActivity.this;
public boolean isLogin = true;
int logintype = 0;// 1是qq登錄,2是微信登錄
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YSDKApi.onCreate(activity);
YSDKApi.setUserListener(ysdkUser);
YSDKApi.handleIntent(activity.getIntent());
}
/--------------------------------登錄接口-----------------------------------------/
public void qqLogin(){
YSDKApi.login(ePlatform.QQ);
}
public void wxLogin(){
YSDKApi.login(ePlatform.WX);
}
public void logout(){
YSDKApi.logout();
}
/--------------------------------生命周期-----------------------------------------/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
YSDKApi.onDestroy(activity);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
YSDKApi.onPause(activity);
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
YSDKApi.onRestart(activity);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
YSDKApi.onResume(activity);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
YSDKApi.onStop(activity);
}
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
YSDKApi.handleIntent(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
YSDKApi.onActivityResult(requestCode, resultCode,data);
}
/--------------------------------監(jiān)聽(tīng)回調(diào)-----------------------------------------/
UserListener ysdkUser = new UserListener() {
@Override
public void OnWakeupNotify(WakeupRet ret) {
// TODO Auto-generated method stub
Log.d(TAG, "called");
Log.d(TAG, "flag:" + ret.flag);
Log.d(TAG, "msg:" + ret.msg);
Log.d(TAG, "platform:" + ret.platform);
int platform = ret.platform;
// TODO GAME 游戲需要在這里增加處理異賬號(hào)的邏輯
if (eFlag.Wakeup_YSDKLogining == ret.flag) {
// 用拉起的賬號(hào)登錄,登錄結(jié)果在OnLoginNotify()中回調(diào)
} else if (ret.flag == eFlag.Wakeup_NeedUserSelectAccount) {
// 異賬號(hào)時(shí),游戲需要彈出提示框讓用戶(hù)選擇需要登錄的賬號(hào)
Log.d(TAG, "diff account");
} else if (ret.flag == eFlag.Wakeup_NeedUserLogin) {
// 沒(méi)有有效的票據(jù),登出游戲讓用戶(hù)重新登錄
Log.d(TAG, "need login");
} else {
Log.d(TAG, "logout");
}
}
@Override
public void OnRelationNotify(UserRelationRet ret) {
// TODO Auto-generated method stub
String result = "";
result = result + "flag:" + ret.flag + "\n";
result = result + "msg:" + ret.msg + "\n";
result = result + "platform:" + ret.platform + "\n";
if (ret.persons != null && ret.persons.size() > 0) {
PersonInfo personInfo = (PersonInfo) ret.persons.firstElement();
StringBuilder builder = new StringBuilder();
builder.append("UserInfoResponse json: \n");
builder.append("nick_name: " + personInfo.nickName + "\n");
builder.append("open_id: " + personInfo.openId + "\n");
builder.append("userId: " + personInfo.userId + "\n");
builder.append("gender: " + personInfo.gender + "\n");
builder.append("picture_small: " + personInfo.pictureSmall
+ "\n");
builder.append("picture_middle: " + personInfo.pictureMiddle
+ "\n");
builder.append("picture_large: " + personInfo.pictureLarge
+ "\n");
builder.append("provice: " + personInfo.province + "\n");
builder.append("city: " + personInfo.city + "\n");
builder.append("country: " + personInfo.country + "\n");
result = result + builder.toString();
} else {
result = result + "relationRet.persons is bad";
}
Log.d(TAG, "OnRelationNotify" + result);
// 發(fā)送結(jié)果到結(jié)果展示界面
}
@Override
public void OnLoginNotify(UserLoginRet ret) {
// TODO Auto-generated method stub
switch (ret.flag) {
case eFlag.Succ:
Log.e(TAG, "Login success:" + ret.toString());
openId = ret.open_id;
payToken = ret.getPayToken();
pf = ret.pf;
pfKey = ret.pf_key;
accessToken = ret.getAccessToken();
Log.i(TAG, "platform:" + ret.platform);
logintype = ret.platform;
// token和uid
Log.d(TAG, "uid:" + openId + "token:" + accessToken);
new NetHelper().execute();
break;
// 游戲邏輯,對(duì)登錄失敗情況分別進(jìn)行處理
case eFlag.QQ_UserCancel:
Log.e(TAG, "用戶(hù)取消授權(quán),請(qǐng)重試");
// showTips("用戶(hù)取消授權(quán),請(qǐng)重試",localContext);
break;
case eFlag.QQ_LoginFail:
Log.e(TAG, "QQ登錄失敗,請(qǐng)重試");
// showTips("QQ登錄失敗,請(qǐng)重試",localContext);
break;
case eFlag.QQ_NetworkErr:
Log.e(TAG, "QQ登錄失敗,請(qǐng)重試");
// showTips("QQ登錄失敗,請(qǐng)重試",localContext);
break;
case eFlag.QQ_NotInstall:
Log.e(TAG, "手機(jī)未安裝手Q,請(qǐng)安裝后重試");
// showTips("手機(jī)未安裝手Q,請(qǐng)安裝后重試", localContext);
break;
case eFlag.QQ_NotSupportApi:
Log.e(TAG, "手機(jī)手Q版本太低,請(qǐng)升級(jí)后重試");
// showTips("手機(jī)手Q版本太低,請(qǐng)升級(jí)后重試", localContext);
break;
case eFlag.WX_NotInstall:
Log.e(TAG, "手機(jī)未安裝微信,請(qǐng)安裝后重試");
// showTips("手機(jī)未安裝微信,請(qǐng)安裝后重試", localContext);
break;
case eFlag.WX_NotSupportApi:
Log.e(TAG, "手機(jī)微信版本太低,請(qǐng)升級(jí)后重試");
// showTips("手機(jī)微信版本太低,請(qǐng)升級(jí)后重試", localContext);
break;
case eFlag.WX_UserCancel:
Log.e(TAG, "用戶(hù)取消授權(quán),請(qǐng)重試");
// showTips("用戶(hù)取消授權(quán),請(qǐng)重試",localContext);
break;
case eFlag.WX_UserDeny:
Log.e(TAG, "用戶(hù)拒絕了授權(quán),請(qǐng)重試");
// showTips("用戶(hù)拒絕了授權(quán),請(qǐng)重試",localContext);
break;
case eFlag.WX_LoginFail:
Log.e(TAG, "微信登錄失敗,請(qǐng)重試");
// showTips("微信登錄失敗,請(qǐng)重試",localContext);
break;
case eFlag.Login_TokenInvalid:
Log.e(TAG, "您尚未登錄或者之前的登錄已過(guò)期,請(qǐng)重試");
// showTips("您尚未登錄或者之前的登錄已過(guò)期,請(qǐng)重試",localContext);
break;
case eFlag.Login_NotRegisterRealName:
// 顯示登錄界面
Log.e(TAG, "您的賬號(hào)沒(méi)有進(jìn)行實(shí)名認(rèn)證,請(qǐng)實(shí)名認(rèn)證后重試");
// showTips("您的賬號(hào)沒(méi)有進(jìn)行實(shí)名認(rèn)證,請(qǐng)實(shí)名認(rèn)證后重試", localContext);
break;
default:
// 顯示登錄界面
break;
}
}
};
// 客戶(hù)端收到登錄成功回調(diào)(正常登錄,票據(jù)刷新),發(fā)送SDK服務(wù)端刷新票據(jù)
private class NetHelper extends AsyncTask {
@Override
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub
try {
Map map = new HashMap();
map.put("openid", openId);
map.put("openkey", accessToken);
map.put("pf", pf);
map.put("pfkey", pfKey);
map.put("payToken", payToken);
String beforeSignString = "openid=" + openId + "&openkey="
+ accessToken + "&payToken=" + payToken + "&pf=" + pf
+ "&pfkey=" + pfKey + "clientKey";
Log.e(TAG, "login beforeSignString:" + beforeSignString);
String sign = md5(beforeSignString);
map.put("sign", sign);
// 以下為把map post到服務(wù)端
// refreshResult = HttpUtil._post(TypeSDKBonjour_Tencent
// .Instance().platform.GetData("tokenUrl"), map);
// Log.i(TAG, "tokenUrl reuslt:" + refreshResult);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
public static String md5(String string) {
byte[] hash;
try {
hash = MessageDigest.getInstance("MD5").digest(
string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Huh, MD5 should be supported?", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Huh, UTF-8 should be supported?", e);
}
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10)
hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
}
}
至此我們就把所有應(yīng)用寶上線(xiàn)所需的登錄接口完成了,添加UI并添加相應(yīng)事件調(diào)用相應(yīng)接口后,就是一個(gè)最簡(jiǎn)單的demo了
如果想要了解具體的接口作用,和相應(yīng)參數(shù)的說(shuō)明,查看代碼中的注釋或查看官方文檔
如果想了解更多,請(qǐng)聯(lián)系我們或關(guān)注官網(wǎng)
了解更多:www.typesdk.com
問(wèn)題解答:1771930259
聯(lián)系郵箱:qianyuzhou@typesdk.com
項(xiàng)目地址:https://github.com/typesdk
更多建議: