Introduction

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.

Development Environment

1.Minimum Android version requirement:

SGSDK requires minimum Android version 4.4 (KitKat, API Level 19) to run. We recommend Android Studio as your main development environment.


2.Download SGSDK Android Library File


3.Sample application


4.Android app permissions

  • SGSDK requires following permissions to function. It will request app permission automatically.
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"/>

Using SGSDK

1.Import SDK

  • From Android Studio: File > New Module
  • Select Import .JAR/.AAR Package and then click Next
  • Enter the location of SGSDK.AAR then click Finish.
  • Make sure ':SGSDK' is in settings.gradle file, as shown here:
    include ':app', ':SGSDK'
  • Make sure ':SGSDK' is in build.gradle dependencies, as shown here:
    dependencies { compile project(":SGSDK") }

2.Other reference materials

Initialization

1.Before initialization

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>

2.SDK initialization

Function
void init(Context ctx, String gameKey, String appSecret)
Description
  • Initialize SDK. You have to call init function once before any other functions.
  • You should call init functions inside Activity onCreate() event.
Example
1
2
3
4
5
6
7
private SGSDK _sdk = SGSDK.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState)
{
   _sdk.init(this,GameKey,AppSecret);
}
Parameters
  • Context: Activity object.
  • You can obtain GameKeyAppSecret from SG Developer console.
Response

SDK Lifecycle Events

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();
    }
}
  • Special note on onBackPressed():

    Some SDK functions launch WebView control as part of the process flow. In order to handle those functions properly, it needs to aware the back button click event.

    Inside Activity onBackPressed() event, you should check return value of _sdk.onBackPressed(), and decide if the parent super.onBackPressed() event should be called.

SDK Callback Listener

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
        }
    }
});
  • SDK responses through SGListenerResult object, which contains three members: CodeMessageData. Based on different situation, Data might be NULL or an object. Please refer to each function for more detail.
  • For code and message, please refer to 【Response Code Definition】for more detail.