Preface: A lot of people do game development, estimates are more or less to take the Channel SDK, what UC, when Le, 91, millet, 360 ... according to statistics the domestic market is currently less than 100 channels, but also includes some small channels without SDK. Each channel SDK access method, much is similar. However, it is these small different, and let the SDK access, creating endless variables. So, before accessing the SDK, if you have no experience, or have not been trapped by the SDK, then when you see this series of articles, you are lucky, you can avoid all this. If you've been in a hole before and you're still in the pit, now is your time to be free.
In the previous article we talked about the entire U8 SDK abstraction into the design, that this article, we will be to verify that he is as we expected, the simple and flexible.
As previously mentioned, for each game, you only need to access the abstraction layer, and each channel SDK access, is an implementation of the abstraction layer. Finally, the final output is done through a one-click Packaging Tool. So, let's take a look at how the game calls this abstraction layer.
We assume that we have developed a game, facing on-line, need to access the SDK, need to access UC, when Le, 91,360, Xiaomi and other channels. Because of the idea of using the U8 SDK Unified Access Framework, the game engineering itself does not couple the specific channel SDK, but only invokes the U8 SDK abstraction layer. So, based on the abstraction layer discussed in the previous article, let's look at how to invoke it.
First, we built a new Android project with Eclipse as our simulation game project. Here's what you need to say, if you're not using an Android-developed game, but instead use a game engine like unity3d,cocos2d, and the same is true for access. Just one more layer of JNI calls. We create a new activity for the main run:
/p>
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 (SA Vedinstancestate); 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, "Login 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.getin Stance (). Login ();}); private void Pay () {u8sdk.getinstance (). Runonmainthread (New Runnable () {@Overridepublic void run () {Payparams params = new Payparams ();p arams.setprice ();p Arams.setserverid ("1");p Arams.setroleid ("1"); U8pay.getinstance (). Pay (params);}); } public void Onactivityresult (int requestcode, int resultcode, Intent data) {u8sdk.getinstance (). Onactivityresult (requ Estcode, 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 simulation game, access to the SDK abstraction layer is a very easy thing to do in the oncreate inside the initialization of the operation:
U8sdk.getinstance (). Init (mainactivity.this);
at the same time, as we said earlier, we are based on the listener mode design, we need to set up a listener, when the 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, "Login successful, sid:" +loginresult.getsid (), toast.length_short);});
among them, Onresult can make a toast output when debugging. Onloginresult is the callback when the Channel SDK landed successfully. This is where we need to do our fourth step in the entire login process (U8APPID,SID) to access U8 server to get a token that will be used for the next login authentication.
In this way, the initialization is complete. Then, in the place where the login interface needs to be called, call:
U8user.getinstance (). Login ();
called where the payment interface needs to be called:
U8pay.getinstance (). Pay (params);
It is important to note that these interfaces need to be called in the UI thread, so we need to use U8sdk.getinstance (). Runonmainthread ():
U8sdk.getinstance (). Runonmainthread (New Runnable () {@Overridepublic void run () {u8user.getinstance (). Login ();});
Finally, we need to rewrite the activity-related system event method, in the corresponding method, plus the U8 SDK corresponding method call. For example:
public void OnPause () {u8sdk.getinstance (). OnPause (); Super.onpause ();}
In this way, for our "game", even if the SDK access is completed. As you can see, access is a very simple thing for a game. It does not consider how to package the back, how to generate a channel package, how to manage the configuration, how to upgrade, ... For TA, it's over here. Next, we'll look at how we can access this framework for a single channel, like UC.
This article small black
Teach you fast and efficient access to the sdk--Game Access SDK (abstract framework only)