SG Android SDK allows you to connect to SG backend services easily when developing Android App using Java.
It's important for you to read through and understand the document before starting development.
SGSDK requires minimum Android version 4.4 (KitKat, API Level 19) to run. We recommend Android Studio as your main development environment.
1 2 3 4 5 6 | <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="com.android.vending.BILLING" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> |
include ':app', ':SGSDK'
dependencies { compile project(":SGSDK") }
Please add following meta-data
setting in AndroidManifest.xml before initializing SGSDK functions:
1 2 3 4 5 6 7 8 | <application......> ...... ...... <meta-data android:name="SDKChannel" android:value="googleplay" /> <meta-data android:name="Base64PublicKey" android:value="xxxxxxxxxxxxxxxxx" /> </application> |
void init(Context ctx, String gameKey, String appSecret)Description
init
function once before any other functions. init
functions inside Activity onCreate()
event.1 2 3 4 5 6 7 | private SGSDK _sdk = SGSDK.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { _sdk.init(this,GameKey,AppSecret); } |
For certain functions to work properly, SGSDK needs to aware the lifecycle events of an Android app.
Please follow sample codes below to invoke SDK lifecycle functions
onResume
, onBackPressed
,
onActivityResult
, onDestroy
corresponding to the Activity lifecycle events:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
private SGSDK _sdk = SGSDK.getInstance(); @Override protected void onResume() { _sdk.onResume(); super.onResume(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { _sdk.onActivityResult(requestCode,resultCode,data); super.onActivityResult(requestCode, resultCode, data); } @Override protected void onDestroy() { _sdk.onDestroy(); super.onDestroy(); } @Override public void onBackPressed() { if(_sdk.onBackPressed()) { Log.i("[Action] BackPressed is handled by SDK..."); } else { super.onBackPressed(); } } |
onBackPressed()
event, you should check return value of _sdk.onBackPressed()
, and decide if the parent super.onBackPressed()
event should be called.
In order to get the result of many SDK asynchronized functions, you need to register a Listener to handle the response data. Here is the sample:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | private SGSDK _sdk = SGSDK.getInstance(); _sdk.setListener(new SGListener() { @Override public void onCallBack(SGListenerResult result) { Log.i(result.toString()); switch (result.getCode()) { case 101: // init OK break; case 201: // login OK Log.i("getOpenId(): "+_sdk.getOpenId()); Log.i("getSessionId(): "+_sdk.getSessionId()); Log.i("getToken(): "+_sdk.getToken()); break; case 1101: // IAP OK SgSDKPayResponse resp = (SgSDKPayResponse)result.getData(); break; case 1120: // wechat pay result SgSDKPayResponse resp3 = (SgSDKPayResponse)result.getData(); if(resp3!=null) { UILog("SgSDKPayResponse: "+resp3.receipt); } break; //Refer to the full list of response codes } } }); |
Code
,Message
和 Data
.
Based on different situation, Data
might be NULL or an object. Please refer to each function for more detail.