How to quickly and efficiently access the SDK-game Access SDK (only accessing the abstract framework) and sdk framework
Question: Many people who develop games probably take over the Channel SDK, UC, dangle, 91, Xiaomi, 360 ...... according to statistics, there are currently no more than 100 channels in the domestic market, and there are also some small channels without sdks. The SDK access methods for each channel are mostly the same. However, these differences allow SDK access to produce endless variables. Therefore, if you have no experience or have never been pitted by the SDK before accessing the SDK, You will be lucky when you see this series of articles. You can avoid this. If you have been pitted before and are still being pitted, now is the time for your relief.
In the previous article, we talked about the design abstracted by the U8 SDK. In this article, we will verify whether it is as simple and flexible as we expect.
As mentioned previously, for each game, you only need to access the abstraction layer, and the access of each channel SDK is an implementation of this abstraction layer. Finally, a one-click packaging tool is used to complete the final output. Let's take a look at how the game calls this abstraction layer.
We assume that we have developed a game that is going online. We need to access sdks, UC, dangle, 91,360, Xiaomi, and other channels. Because the unified access framework of U8 SDK is adopted, the game project itself does not coupling the SDK of each specific channel, but only calls the U8 SDK abstraction layer. Then, based on the abstraction layer mentioned in the previous article, let's look at how to call it.
First, we use eclipse to create an android project as our simulated game project. It should be noted that if you do not directly use a game developed by Android, but use game engines such as Unity3D and Cocos2D, the access is the same. Only a layer of JNI calls are added. Create a main running Activity:
Package com. u8.sdk. demo; import com. u8.sdk. IU8SDKListener; import com. u8.sdk. loginResult; import com. u8.sdk. payParams; import com. u8.sdk. u8SDK; import com. u8.sdk. components. u8Pay; import com. u8.sdk. components. u8User; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. toast; import android. annotation. suppressLint; import android. app. activity; import android. content. intent; public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); U8SDK. getInstance (). setSDKListener (new IU8SDKListener () {@ SuppressLint ("ShowToast") @ Overridepublic void onResult (int arg0, String msg) {Toast. makeText (MainActivity. this, msg, Toast. LENGTH_SHORT);} @ SuppressLint ("ShowToast") @ Overridepublic void onLoginResult (LoginResult loginResult) {Toast. makeText (MainActivity. this, "Logon successful, sid:" + loginResult. getSid (), Toast. LENGTH_SHORT) ;}}); Button btn = (Button) findViewById (R. id. button1); btn. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {pay () ;}}); Button btnLogin = (Button) findViewById (R. id. button2); btnLogin. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {login () ;}}); U8SDK. getInstance (). init (MainActivity. this);} private void login () {U8SDK. getInstance (). runOnMainThread (new Runnable () {@ Overridepublic void run () {U8User. getInstance (). login () ;}}) ;}private void pay () {U8SDK. getInstance (). runOnMainThread (new Runnable () {@ Overridepublic void run () {PayParams params = new PayParams (); params. setPrice (100); params. setServerId ("1"); params. setRoleId ("1"); U8Pay. getInstance (). pay (params) ;}});} public void onActivityResult (int requestCode, int resultCode, Intent data) {U8SDK. getInstance (). onActivityResult (requestCode, resultCode, data); super. onActivityResult (requestCode, resultCode, data);} public void onPause () {U8SDK. getInstance (). onPause (); super. onPause ();} public void onResume () {U8SDK. getInstance (). onResume (); super. onResume ();} public void onNewIntent (Intent newIntent) {U8SDK. getInstance (). onNewIntent (newIntent); super. onNewIntent (newIntent);} public void onStop () {U8SDK. getInstance (). onStop (); super. onStop ();} public void onDestroy () {U8SDK. getInstance (). onDestroy (); super. onDestroy ();} public void onRestart () {U8SDK. getInstance (). onRestart (); super. onRestart ();} public void onBackPressed () {U8SDK. getInstance (). onBackPressed (); super. onBackPressed ();}}
As you can see, in our simulated game, how Easy it is to access the SDK abstraction layer, that is, to initialize the SDK in onCreate:
U8SDK.getInstance().init(MainActivity.this);
At the same time, as we mentioned earlier, we are designing based on the listener mode. We need to set a listener. When a specific SDK has a status change, success, or failure, we can get specific information:
U8SDK. getInstance (). setSDKListener (new IU8SDKListener () {@ SuppressLint ("ShowToast") @ Overridepublic void onResult (int arg0, String msg) {Toast. makeText (MainActivity. this, msg, Toast. LENGTH_SHORT);} @ SuppressLint ("ShowToast") @ Overridepublic void onLoginResult (LoginResult loginResult) {Toast. makeText (MainActivity. this, "Logon successful, sid:" + loginResult. getSid (), Toast. LENGTH_SHORT );}});
Here, onResult can be used as a Toast output during debugging. OnLoginResult is the callback when the channel SDK is successfully logged on. Here, we need to go through step 4 (u8appid, sid) in the previous login process to access U8 Server to obtain a token used for the next login authentication.
In this way, the initialization is complete. Then, you can call:
U8User.getInstance().login();
To call the payment interface:
U8Pay.getInstance().pay(params);
Note that these interfaces need to be called in the UI thread. Therefore, we need to use U8SDK. getInstance (). runOnMainThread ():
U8SDK.getInstance().runOnMainThread(new Runnable() {@Overridepublic void run() {U8User.getInstance().login();}});
Finally, we need to rewrite the System Event method related to the Activity, and add the call of the method corresponding to the U8 SDK in the corresponding method. For example:
public void onPause(){U8SDK.getInstance().onPause();super.onPause();}
In this way, the SDK access is completed for our "game. As you can see, access is a simple task for games. It does not need to consider how to package, how to generate channel packages, how to manage configurations, how to upgrade ,... For TA, this is the end. Next, let's take a look. For a channel, such as UC, how can we access this framework?
Author: Xiao Hei