Chinaunix首页 | 论坛 | 博客
  • 博客访问: 317070
  • 博文数量: 65
  • 博客积分: 2570
  • 博客等级: 少校
  • 技术积分: 730
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 14:04
个人简介

苦逼房奴一枚

文章分类

全部博文(65)

文章存档

2017年(19)

2016年(5)

2013年(6)

2012年(1)

2011年(3)

2009年(5)

2008年(26)

我的朋友

分类: Android平台

2017-06-20 15:29:50

官方文档及SDK的下载地址为:%E4%B8%8B%E8%BD%BD

 

在接入之前,需先在应用宝后台创建游戏,获取一些接入参数。

 

客户端所需要参数包括:手Qapp_id、微信的app_id

 

准备工作做为,正式开始接入,首先把应用宝的YSDKLibrary引用库导入到我们的工作环境中去,然后用我们demo工程去引用那个工程。

我这边的接入环境是Eclipse









至于demo工程如何创建在上一篇的接入文档中有详细教程,请至:

http://www.cnblogs.com/laohaizei/p/6724250.html

 

首先我们打开我们demo工程的AndroidManifest.xml文件。


然后复制以下代码


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="com.example.demo"
  4.     android:versionCode="1"
  5.     android:versionName="1.0" >

  6.     <uses-sdk
  7.         android:minSdkVersion="8"
  8.         android:targetSdkVersion="19" />

  9.     <supports-screens
  10.         android:anyDensity="true"
  11.         android:largeScreens="true"
  12.         android:normalScreens="true"
  13.         android:resizeable="true"
  14.         android:smallScreens="true" />

  15.     <!-- TODO GAME SDK接入必须权限模块 START -->
  16.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  17.     <uses-permission android:name="android.permission.GET_TASKS" />
  18.     <uses-permission android:name="android.permission.RESTART_PACKAGES" />
  19.     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

  20.     <!-- TODO GAME SDK Midas接入配置 设置屏幕兼容声明 START -->
  21.     <uses-permission android:name="android.permission.INTERNET" />
  22.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  23.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  24.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  25.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  26.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  27.     <uses-permission android:name="android.permission.RESTART_PACKAGES" />
  28.     <uses-permission android:name="android.permission.GET_TASKS" />
  29.     <uses-permission android:name="android.permission.READ_SMS" />
  30.     <uses-permission android:name="android.permission.SEND_SMS" />
  31.     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  32.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

  33.     <application
  34.         android:allowBackup="false"
  35.         android:icon="@drawable/ic_launcher"
  36.         android:label="@string/app_name" >
  37.         <activity
  38.             android:name=".MainActivity"
  39.             android:label="@string/app_name" >
  40.             <intent-filter>
  41.                 <action android:name="android.intent.action.MAIN" />

  42.                 <category android:name="android.intent.category.LAUNCHER" />
  43.             </intent-filter>
  44.         </activity>

  45.         <!-- TODO GAME SDK接入YSDK登陆接入配置 START -->
  46.         <activity
  47.             android:name="com.tencent.ysdk.module.realName.impl.RegisterRealNameActivity"
  48.             android:configChanges="orientation|screenSize|keyboardHidden"
  49.             android:screenOrientation="landscape"
  50.             android:theme="@android:style/Theme.Translucent.NoTitleBar" >
  51.         </activity>

  52.         <!-- TODO GAME SDK接入YSDK登陆接入配置 END -->
  53.         <!-- TODO GAME SDK QQ接入配置 START -->
  54.         <activity
  55.             android:name="com.tencent.tauth.AuthActivity"
  56.             android:launchMode="singleTask"
  57.             android:noHistory="true" >
  58.             <intent-filter>
  59.                 <action android:name="android.intent.action.VIEW" />

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

  62.                 <data android:scheme="tencent@qq_app_id@" />
  63.                 <!-- TODO GAME 这里为游戏QQ登录的配置,游戏需要修改为自己手Q的appid -->
  64.             </intent-filter>
  65.         </activity>
  66.         <activity
  67.             android:name="com.tencent.connect.common.AssistActivity"
  68.             android:configChanges="orientation|screenSize|keyboardHidden"
  69.             android:screenOrientation="portrait"
  70.             android:theme="@android:style/Theme.Translucent.NoTitleBar" />
  71.         <!-- TODO GAME SDK QQ接入配置 END -->


  72.         <!-- TODO GAME SDK 微信接入配置 START -->
  73.         <activity
  74.             android:name="com.tencent.tmgp.yybtestsdk.wxapi.WXEntryActivity"
  75.             android:configChanges="orientation|screenSize|keyboardHidden"
  76.             android:excludeFromRecents="true"
  77.             android:exported="true"
  78.             android:label="WXEntryActivity"
  79.             android:launchMode="singleTop"
  80.             android:taskAffinity="@package@.diff"
  81.             android:theme="@android:style/Theme.Translucent.NoTitleBar" >

  82.             <!-- TODO GAME 这里为游戏微信登录的配置,游戏需要修改为自己的包名加.diff -->
  83.             <intent-filter>
  84.                 <action android:name="android.intent.action.VIEW" />

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

  86.                 <data android:scheme="@wx_app_id@" />
  87.                 <!-- TODO GAME 这里为游戏微信登录的配置,游戏需要修改为自己的微信appid -->
  88.             </intent-filter>
  89.         </activity>
  90.         <!-- TODO GAME SDK 微信接入配置 END -->


  91.         <!-- TODO GAME SDK Midas接入配置 START -->
  92.         <activity
  93.             android:name="com.tencent.midas.proxyactivity.APMidasPayProxyActivity"
  94.             android:configChanges="orientation|keyboardHidden|screenSize"
  95.             android:screenOrientation="sensorLandscape"
  96.             android:theme="@android:style/Theme.Translucent.NoTitleBar" />
  97.         <activity
  98.             android:name="com.tencent.midas.wx.APMidasWXPayActivity"
  99.             android:exported="true"
  100.             android:theme="@android:style/Theme.Translucent.NoTitleBar" />
  101.         <activity
  102.             android:name="com.tencent.midas.qq.APMidasQQWalletActivity"
  103.             android:configChanges="orientation|screenSize|keyboardHidden"
  104.             android:exported="true"
  105.             android:launchMode="singleTop"
  106.             android:theme="@android:style/Theme.Translucent.NoTitleBar" >
  107.             <intent-filter>
  108.                 <action android:name="android.intent.action.VIEW" />

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

  111.                 <data android:scheme="qwallet100703379" />
  112.                 <!-- TODO GAME 这里为米大师的默认配置,游戏不要修改 -->
  113.             </intent-filter>
  114.         </activity>
  115.         <activity
  116.             android:name="com.tencent.midas.jsbridge.APWebJSBridgeActivity"
  117.             android:theme="@android:style/Theme.Translucent.NoTitleBar"
  118.             android:windowSoftInputMode="stateAlwaysHidden" />
  119.         <!-- TODO GAME SDK Midas接入配置 END -->

  120.     </application>

  121. </manifest>

以上是需要添加在AndroidManifest.xml中的。(@xxx@部分请注意替换修改)

然后到我们demo工程的AndroidManifest.xml全选替换粘贴

这样AndroidManifest.xml修改好了。

 

做完以上步骤,接下来,我们开始接入SDK的接口。

首先打开我们的MainActivity.java

打开的MainActivity.java应该是这个样子的


然后把下面的代码复制进去,注意别把第一行的代码覆盖了。


点击(此处)折叠或打开

  1. import java.io.UnsupportedEncodingException;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. import java.util.HashMap;
  5. import java.util.Map;

  6. import com.tencent.ysdk.api.YSDKApi;
  7. import com.tencent.ysdk.framework.common.eFlag;
  8. import com.tencent.ysdk.framework.common.ePlatform;
  9. import com.tencent.ysdk.module.user.PersonInfo;
  10. import com.tencent.ysdk.module.user.UserListener;
  11. import com.tencent.ysdk.module.user.UserLoginRet;
  12. import com.tencent.ysdk.module.user.UserRelationRet;
  13. import com.tencent.ysdk.module.user.WakeupRet;

  14. import android.app.Activity;
  15. import android.content.Context;
  16. import android.content.Intent;
  17. import android.os.AsyncTask;
  18. import android.os.Bundle;
  19. import android.util.Log;

  20. public class MainActivity extends Activity {

  21.     private final static String TAG = "demo";

  22.     public static String openId = "";
  23.     public static String pf = "";
  24.     public static String pfKey = "";
  25.     public static String accessToken = "";
  26.     public static String payToken = "";
  27.     public Context context = this;
  28.     public Activity activity = MainActivity.this;
  29.     public boolean isLogin = true;
  30.     int logintype = 0;// 1是qq登录,2是微信登录

  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         // TODO Auto-generated method stub
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_main);
  36.         YSDKApi.onCreate(activity);
  37.         YSDKApi.setUserListener(ysdkUser);
  38.         YSDKApi.handleIntent(activity.getIntent());
  39.     }
  40.     
  41.     /*--------------------------------登录接口-----------------------------------------*/
  42.     public void qqLogin(){
  43.         YSDKApi.login(ePlatform.QQ);
  44.     }
  45.     
  46.     public void wxLogin(){
  47.         YSDKApi.login(ePlatform.WX);
  48.     }
  49.     
  50.     public void logout(){
  51.         YSDKApi.logout();
  52.     }

  53.     /*--------------------------------生命周期-----------------------------------------*/
  54.     @Override
  55.     protected void onDestroy() {
  56.         // TODO Auto-generated method stub
  57.         super.onDestroy();
  58.         YSDKApi.onDestroy(activity);
  59.     }

  60.     @Override
  61.     protected void onPause() {
  62.         // TODO Auto-generated method stub
  63.         super.onPause();
  64.         YSDKApi.onPause(activity);
  65.     }

  66.     @Override
  67.     protected void onRestart() {
  68.         // TODO Auto-generated method stub
  69.         super.onRestart();
  70.         YSDKApi.onRestart(activity);
  71.     }

  72.     @Override
  73.     protected void onResume() {
  74.         // TODO Auto-generated method stub
  75.         super.onResume();
  76.         YSDKApi.onResume(activity);
  77.     }

  78.     @Override
  79.     protected void onStop() {
  80.         // TODO Auto-generated method stub
  81.         super.onStop();
  82.         YSDKApi.onStop(activity);
  83.     }

  84.     @Override
  85.     protected void onNewIntent(Intent intent) {
  86.         // TODO Auto-generated method stub
  87.         super.onNewIntent(intent);
  88.         YSDKApi.handleIntent(intent);
  89.     }

  90.     @Override
  91.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  92.         super.onActivityResult(requestCode, resultCode, data);
  93.         YSDKApi.onActivityResult(requestCode, resultCode,data);
  94.     }

  95.     /*--------------------------------监听回调-----------------------------------------*/
  96.     UserListener ysdkUser = new UserListener() {

  97.         @Override
  98.         public void OnWakeupNotify(WakeupRet ret) {
  99.             // TODO Auto-generated method stub
  100.             Log.d(TAG, "called");
  101.             Log.d(TAG, "flag:" + ret.flag);
  102.             Log.d(TAG, "msg:" + ret.msg);
  103.             Log.d(TAG, "platform:" + ret.platform);
  104.             int platform = ret.platform;
  105.             // TODO GAME 游戏需要在这里增加处理异账号的逻辑
  106.             if (eFlag.Wakeup_YSDKLogining == ret.flag) {
  107.                 // 用拉起的账号登录,登录结果在OnLoginNotify()中回调
  108.             } else if (ret.flag == eFlag.Wakeup_NeedUserSelectAccount) {
  109.                 // 异账号时,游戏需要弹出提示框让用户选择需要登录的账号
  110.                 Log.d(TAG, "diff account");
  111.             } else if (ret.flag == eFlag.Wakeup_NeedUserLogin) {
  112.                 // 没有有效的票据,登出游戏让用户重新登录
  113.                 Log.d(TAG, "need login");
  114.             } else {
  115.                 Log.d(TAG, "logout");
  116.             }
  117.         }

  118.         @Override
  119.         public void OnRelationNotify(UserRelationRet ret) {
  120.             // TODO Auto-generated method stub
  121.             String result = "";
  122.             result = result + "flag:" + ret.flag + "\n";
  123.             result = result + "msg:" + ret.msg + "\n";
  124.             result = result + "platform:" + ret.platform + "\n";
  125.             if (ret.persons != null && ret.persons.size() > 0) {
  126.                 PersonInfo personInfo = (PersonInfo) ret.persons.firstElement();
  127.                 StringBuilder builder = new StringBuilder();
  128.                 builder.append("UserInfoResponse json: \n");
  129.                 builder.append("nick_name: " + personInfo.nickName + "\n");
  130.                 builder.append("open_id: " + personInfo.openId + "\n");
  131.                 builder.append("userId: " + personInfo.userId + "\n");
  132.                 builder.append("gender: " + personInfo.gender + "\n");
  133.                 builder.append("picture_small: " + personInfo.pictureSmall
  134.                         + "\n");
  135.                 builder.append("picture_middle: " + personInfo.pictureMiddle
  136.                         + "\n");
  137.                 builder.append("picture_large: " + personInfo.pictureLarge
  138.                         + "\n");
  139.                 builder.append("provice: " + personInfo.province + "\n");
  140.                 builder.append("city: " + personInfo.city + "\n");
  141.                 builder.append("country: " + personInfo.country + "\n");
  142.                 result = result + builder.toString();
  143.             } else {
  144.                 result = result + "relationRet.persons is bad";
  145.             }
  146.             Log.d(TAG, "OnRelationNotify" + result);

  147.             // 发送结果到结果展示界面
  148.         }

  149.         @Override
  150.         public void OnLoginNotify(UserLoginRet ret) {
  151.             // TODO Auto-generated method stub
  152.             switch (ret.flag) {
  153.             case eFlag.Succ:
  154.                 Log.e(TAG, "Login success:" + ret.toString());
  155.                 openId = ret.open_id;
  156.                 payToken = ret.getPayToken();
  157.                 pf = ret.pf;
  158.                 pfKey = ret.pf_key;
  159.                 accessToken = ret.getAccessToken();
  160.                 Log.i(TAG, "platform:" + ret.platform);
  161.                 logintype = ret.platform;
  162.                 // token和uid
  163.                 Log.d(TAG, "uid:" + openId + "token:" + accessToken);

  164.                 new NetHelper().execute();

  165.                 break;
  166.             // 游戏逻辑,对登录失败情况分别进行处理
  167.             case eFlag.QQ_UserCancel:
  168.                 Log.e(TAG, "用户取消授权,请重试");
  169.                 // showTips("用户取消授权,请重试",localContext);
  170.                 break;
  171.             case eFlag.QQ_LoginFail:
  172.                 Log.e(TAG, "QQ登录失败,请重试");
  173.                 // showTips("QQ登录失败,请重试",localContext);
  174.                 break;
  175.             case eFlag.QQ_NetworkErr:
  176.                 Log.e(TAG, "QQ登录失败,请重试");
  177.                 // showTips("QQ登录失败,请重试",localContext);
  178.                 break;
  179.             case eFlag.QQ_NotInstall:
  180.                 Log.e(TAG, "手机未安装手Q,请安装后重试");
  181.                 // showTips("手机未安装手Q,请安装后重试", localContext);
  182.                 break;
  183.             case eFlag.QQ_NotSupportApi:
  184.                 Log.e(TAG, "手机手Q版本太低,请升级后重试");
  185.                 // showTips("手机手Q版本太低,请升级后重试", localContext);
  186.                 break;
  187.             case eFlag.WX_NotInstall:
  188.                 Log.e(TAG, "手机未安装微信,请安装后重试");
  189.                 // showTips("手机未安装微信,请安装后重试", localContext);
  190.                 break;
  191.             case eFlag.WX_NotSupportApi:
  192.                 Log.e(TAG, "手机微信版本太低,请升级后重试");
  193.                 // showTips("手机微信版本太低,请升级后重试", localContext);
  194.                 break;
  195.             case eFlag.WX_UserCancel:
  196.                 Log.e(TAG, "用户取消授权,请重试");
  197.                 // showTips("用户取消授权,请重试",localContext);
  198.                 break;
  199.             case eFlag.WX_UserDeny:
  200.                 Log.e(TAG, "用户拒绝了授权,请重试");
  201.                 // showTips("用户拒绝了授权,请重试",localContext);
  202.                 break;
  203.             case eFlag.WX_LoginFail:
  204.                 Log.e(TAG, "微信登录失败,请重试");
  205.                 // showTips("微信登录失败,请重试",localContext);
  206.                 break;
  207.             case eFlag.Login_TokenInvalid:
  208.                 Log.e(TAG, "您尚未登录或者之前的登录已过期,请重试");
  209.                 // showTips("您尚未登录或者之前的登录已过期,请重试",localContext);
  210.                 break;
  211.             case eFlag.Login_NotRegisterRealName:
  212.                 // 显示登录界面
  213.                 Log.e(TAG, "您的账号没有进行实名认证,请实名认证后重试");
  214.                 // showTips("您的账号没有进行实名认证,请实名认证后重试", localContext);
  215.                 break;
  216.             default:
  217.                 // 显示登录界面
  218.                 break;
  219.             }
  220.         }
  221.     };

  222.     // 客户端收到登录成功回调(正常登录,票据刷新),发送SDK服务端刷新票据
  223.     private class NetHelper extends AsyncTask<Integer, Void, Integer> {

  224.         @Override
  225.         protected Integer doInBackground(Integer... params) {
  226.             // TODO Auto-generated method stub
  227.             try {
  228.                 Map<String, Object> map = new HashMap<String, Object>();
  229.                 map.put("openid", openId);
  230.                 map.put("openkey", accessToken);
  231.                 map.put("pf", pf);
  232.                 map.put("pfkey", pfKey);
  233.                 map.put("payToken", payToken);
  234.                 String beforeSignString = "openid=" + openId + "&openkey="
  235.                         + accessToken + "&payToken=" + payToken + "&pf=" + pf
  236.                         + "&pfkey=" + pfKey + "clientKey";
  237.                 Log.e(TAG, "login beforeSignString:" + beforeSignString);
  238.                 String sign = md5(beforeSignString);
  239.                 map.put("sign", sign);
  240.                 // 以下为把map post到服务端
  241.                 // refreshResult = HttpUtil._post(TypeSDKBonjour_Tencent
  242.                 // .Instance().platform.GetData("tokenUrl"), map);
  243.                 // Log.i(TAG, "tokenUrl reuslt:" + refreshResult);
  244.             } catch (Exception e) {

  245.                 e.printStackTrace();
  246.             }
  247.             return null;
  248.         }

  249.         @Override
  250.         protected void onPostExecute(Integer result) {
  251.             // TODO Auto-generated method stub
  252.             super.onPostExecute(result);
  253.         }

  254.     }

  255.     public static String md5(String string) {

  256.         byte[] hash;

  257.         try {

  258.             hash = MessageDigest.getInstance("MD5").digest(
  259.                     string.getBytes("UTF-8"));

  260.         } catch (NoSuchAlgorithmException e) {

  261.             throw new RuntimeException("Huh, MD5 should be supported?", e);

  262.         } catch (UnsupportedEncodingException e) {

  263.             throw new RuntimeException("Huh, UTF-8 should be supported?", e);

  264.         }

  265.         StringBuilder hex = new StringBuilder(hash.length * 2);

  266.         for (byte b : hash) {

  267.             if ((b & 0xFF) < 0x10)
  268.                 hex.append("0");

  269.             hex.append(Integer.toHexString(b & 0xFF));

  270.         }

  271.         return hex.toString();

  272.     }

  273. }

至此我们就把所有应用宝上线所需的登录接口完成了,添加UI并添加相应事件调用相应接口后,就是一个最简单的demo

 

 

如果想要了解具体的接口作用,和相应参数的说明,查看代码中的注释或查看官方文档

如果想了解更多,请联系我们或关注官网


了解更多:
问题解答:1771930259
联系邮箱:qianyuzhou@typesdk.com
项目地址:




阅读(3326) | 评论(0) | 转发(0) |
0

上一篇: 游戏sdk接入教程:360渠道SDK接入

下一篇:没有了

给主人留下些什么吧!~~