client_Hale
2020-10-20 8c47ada192c22f9bdb1f634abe0ac5046c9a057c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package com.mygame.test;
 
import java.util.UUID;
 
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
import com.quicksdk.QuickSDK;
import com.quicksdk.Sdk;
import com.quicksdk.entity.GameRoleInfo;
import com.quicksdk.entity.OrderInfo;
import com.quicksdk.entity.UserInfo;
import com.quicksdk.notifier.ExitNotifier;
import com.quicksdk.notifier.InitNotifier;
import com.quicksdk.notifier.LoginNotifier;
import com.quicksdk.notifier.LogoutNotifier;
import com.quicksdk.notifier.PayNotifier;
import com.quicksdk.notifier.SwitchAccountNotifier;
 
 
public class MainActivity extends Activity implements OnClickListener {
 
    private Button btnLogin, btnPay, btnLogout, btnFinish, btnUloadUserInfo;
    private TextView infoTv;
 
    private Context mContext;
    boolean isLandscape = false;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getApplicationContext();
        setContentView(getResId("activity_main", "layout"));
 
        initView();
 
        // 设置横竖屏,游戏横屏为true,游戏竖屏为false(必接)
        QuickSDK.getInstance().setIsLandScape(isLandscape);
 
        // 生命周期接口调用(必接)
 
        // 当targetVer大于23时 需要动态申请读写等权限 具体权限 具体而定
        try {
            // check权限
            if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
                    || (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
                // 没有 , 申请权限 权限数组
                ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
            } else {
                // 有 则执行初始化
                // 设置通知,用于监听初始化,登录,注销,支付及退出功能的返回值(必接)
                initQkNotifiers();
                // 请将下面语句中的第二与第三个参数,替换成QuickSDK后台申请的productCode和productKey值,目前的值仅作为示例
                Sdk.getInstance().init(this, "88049844578484520615487574815873", "82414864");
            }
        } catch (Exception e) {
            // 异常 继续申请
            ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
        }
        com.quicksdk.Sdk.getInstance().onCreate(this);
    }
    //申请权限的回调(结果)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    //申请成功
            initQkNotifiers();
            Sdk.getInstance().init(this, "88049844578484520615487574815873", "82414864");
        } else {
            //失败  这里逻辑以游戏为准 这里只是模拟申请失败 退出游戏    cp方可改为继续申请 或者其他逻辑
            System.exit(0);
            finish();
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
        // 生命周期接口调用(必接)
        com.quicksdk.Sdk.getInstance().onStart(this);
    }
 
    @Override
    protected void onRestart() {
        super.onRestart();
        // 生命周期接口调用(必接)
        com.quicksdk.Sdk.getInstance().onRestart(this);
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        // 生命周期接口调用(必接)
        com.quicksdk.Sdk.getInstance().onPause(this);
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        // 生命周期接口调用(必接)
        com.quicksdk.Sdk.getInstance().onResume(this);
    }
 
    @Override
    protected void onStop() {
        super.onStop();
        // 生命周期接口调用(必接)
        com.quicksdk.Sdk.getInstance().onStop(this);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 生命周期接口调用(必接)
        com.quicksdk.Sdk.getInstance().onDestroy(this);
    }
 
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 生命周期接口调用(必接)
        com.quicksdk.Sdk.getInstance().onNewIntent(intent);
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 生命周期接口调用(必接)
        com.quicksdk.Sdk.getInstance().onActivityResult(this, requestCode, resultCode, data);
    }
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        // 登录
        if (id == getResId("btn_login", "id")) {
            Log.d("quicksdk",""+com.quicksdk.Extend.getInstance().getDeviceID(MainActivity.this));
            com.quicksdk.User.getInstance().login(MainActivity.this);
        }
 
        // 提交角色信息
        if (id == getResId("btn_uploadUserInfo", "id")) {
            setUserInfo();
        }
 
        // 注销
        if (id == getResId("btn_logout", "id")) {
            com.quicksdk.User.getInstance().logout(MainActivity.this);
        }
 
        // 支付
        if (id == getResId("btn_pay", "id")) {
            pay();
        }
 
        // 退出
        if (id == getResId("btn_finish", "id")) {
            exit();
        }
    }
 
    /**
     * 支付
     */
    private void pay() {
        GameRoleInfo roleInfo = new GameRoleInfo();
        roleInfo.setServerID("1");// 服务器ID,其值必须为数字字符串
        roleInfo.setServerName("火星服务器");// 服务器名称
        roleInfo.setGameRoleName("裁决之剑");// 角色名称
        roleInfo.setGameRoleID("1121121");// 角色ID
        roleInfo.setGameUserLevel("12");// 等级
        roleInfo.setVipLevel("Vip4");// VIP等级
        roleInfo.setGameBalance("5000");// 角色现有金额
        roleInfo.setPartyName("");// 公会名字
 
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCpOrderID(UUID.randomUUID().toString().replace("-", ""));// 游戏订单号
        orderInfo.setGoodsName("元宝");// 产品名称
        // orderInfo.setGoodsName("月卡");
        orderInfo.setCount(10);// 购买数量,如购买"10元宝"则传10
        // orderInfo.setCount(1);// 购买数量,如购买"月卡"则传1
        orderInfo.setAmount(10); // 总金额(单位为元)
        orderInfo.setGoodsID("101"); // 产品ID,用来识别购买的产品
        orderInfo.setExtrasParams("透传参数"); // 透传参数
 
        com.quicksdk.Payment.getInstance().pay(MainActivity.this, orderInfo, roleInfo);
    }
 
    /**
     * 退出
     */
    private void exit() {
        // 先判断渠道是否有退出框,如果有则直接调用quick的exit接口
        if (QuickSDK.getInstance().isShowExitDialog()) {
            Sdk.getInstance().exit(MainActivity.this);
        } else {
            // 游戏调用自身的退出对话框,点击确定后,调用quick的exit接口
            new AlertDialog.Builder(MainActivity.this).setTitle("退出").setMessage("是否退出游戏?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Sdk.getInstance().exit(MainActivity.this);
                }
            }).setNegativeButton("取消", null).show();
        }
    }
 
    /**
     * 向渠道提交用户信息。 在创建游戏角色、进入游戏和角色升级3个地方调用此接口,当创建角色时最后一个参数值为true,其他两种情况为false。
     * GameRoleInfo所有字段均不能传null,游戏没有的字段请传一个默认值或空字符串。
     */
    public void setUserInfo() {
        GameRoleInfo roleInfo = new GameRoleInfo();
        roleInfo.setServerID("1");// 服务器ID
        roleInfo.setServerName("火星服务器");// 服务器名称
        roleInfo.setGameRoleName("裁决之剑");// 角色名称
        roleInfo.setGameRoleID("1121121");// 角色ID
        roleInfo.setGameUserLevel("12");// 等级
        roleInfo.setVipLevel("9"); // 设置当前用户vip等级,必须为整型字符串
        roleInfo.setGameBalance("5000"); // 角色现有金额
        roleInfo.setGameUserLevel("12"); // 设置游戏角色等级
        roleInfo.setPartyName("无敌联盟"); // 设置帮派,公会名称
        roleInfo.setRoleCreateTime("1473141432"); // UC与1881渠道必传,值为10位数时间戳
        roleInfo.setPartyId("1100"); // 360渠道参数,设置帮派id,必须为整型字符串
        roleInfo.setGameRoleGender("男"); // 360渠道参数
        roleInfo.setGameRolePower("38"); // 360渠道参数,设置角色战力,必须为整型字符串
        roleInfo.setPartyRoleId("11"); // 360渠道参数,设置角色在帮派中的id
        roleInfo.setPartyRoleName("帮主"); // 360渠道参数,设置角色在帮派中的名称
        roleInfo.setProfessionId("38"); // 360渠道参数,设置角色职业id,必须为整型字符串
        roleInfo.setProfession("法师"); // 360渠道参数,设置角色职业名称
        roleInfo.setFriendlist("无"); // 360渠道参数,设置好友关系列表,格式请参考:http://open.quicksdk.net/help/detail/aid/190
        com.quicksdk.User.getInstance().setGameRoleInfo(this, roleInfo, true);
    }
 
    /**
     * 初始化界面元素变量及设置点击监听
     */
    private void initView() {
        btnLogin = (Button) findViewById(getResId("btn_login", "id"));
        btnLogin.setOnClickListener(this);
 
        btnPay = (Button) findViewById(getResId("btn_pay", "id"));
        btnPay.setOnClickListener(this);
 
        btnLogout = (Button) findViewById(getResId("btn_logout", "id"));
        btnLogout.setOnClickListener(this);
 
        btnFinish = (Button) findViewById(getResId("btn_finish", "id"));
        btnFinish.setOnClickListener(this);
 
        btnUloadUserInfo = (Button) findViewById(getResId("btn_uploadUserInfo", "id"));
        btnUloadUserInfo.setOnClickListener(this);
 
        infoTv = (TextView) findViewById(getResId("tv_userInfo", "id"));
    }
 
    /**
     * 返回资源id
     */
    public int getResId(String name, String defType) {
        return mContext.getResources().getIdentifier(name, defType, mContext.getPackageName());
    }
 
    /**
     * 设置通知,用于监听初始化,登录,注销,支付及退出功能的返回值
     */
    private void initQkNotifiers() {
        QuickSDK.getInstance()
        // 1.设置初始化通知(必接)
                .setInitNotifier(new InitNotifier() {
 
                    @Override
                    public void onSuccess() {
                        infoTv.setText("初始化成功");
                    }
 
                    @Override
                    public void onFailed(String message, String trace) {
                        infoTv.setText("初始化失败:" + message);
                    }
                })
                // 2.设置登录通知(必接)
                .setLoginNotifier(new LoginNotifier() {
 
                    @Override
                    public void onSuccess(UserInfo userInfo) {
                        if (userInfo != null) {
                            infoTv.setText("登陆成功" + "\n\r" + "UserID:  " + userInfo.getUID() + "\n\r" + "UserName:  " + userInfo.getUserName() + "\n\r"
                                    + "Token:  " + userInfo.getToken());
 
                            // 登录成功之后,进入游戏时,需要向渠道提交用户信息
                            setUserInfo();
                        }
                    }
 
                    @Override
                    public void onCancel() {
                        infoTv.setText("取消登陆");
                    }
 
                    @Override
                    public void onFailed(final String message, String trace) {
                        infoTv.setText("登陆失败:" + message);
                    }
 
                })
                // 3.设置注销通知(必接)
                .setLogoutNotifier(new LogoutNotifier() {
 
                    @Override
                    public void onSuccess() {
                        infoTv.setText("注销成功");
                    }
 
                    @Override
                    public void onFailed(String message, String trace) {
                        infoTv.setText("注销失败:" + message);
                    }
                })
                // 4.设置切换账号通知(必接)
                .setSwitchAccountNotifier(new SwitchAccountNotifier() {
 
                    @Override
                    public void onSuccess(UserInfo userInfo) {
                        if (userInfo != null) {
                            infoTv.setText("切换账号成功" + "\n\r" + "UserID:  " + userInfo.getUID() + "\n\r" + "UserName:  " + userInfo.getUserName() + "\n\r"
                                    + "Token:  " + userInfo.getToken());
                        }
                    }
 
                    @Override
                    public void onFailed(String message, String trace) {
                        infoTv.setText("切换账号失败:" + message);
                    }
 
                    @Override
                    public void onCancel() {
                        infoTv.setText("取消切换账号");
                    }
                })
                // 5.设置支付通知(必接)
                .setPayNotifier(new PayNotifier() {
 
                    @Override
                    public void onSuccess(String sdkOrderID, String cpOrderID, String extrasParams) {
                        infoTv.setText("支付成功,sdkOrderID:" + sdkOrderID + ",cpOrderID:" + cpOrderID);
                    }
 
                    @Override
                    public void onCancel(String cpOrderID) {
                        infoTv.setText("支付取消,cpOrderID:" + cpOrderID);
                    }
 
                    @Override
                    public void onFailed(String cpOrderID, String message, String trace) {
                        infoTv.setText("支付失败:" + "pay failed,cpOrderID:" + cpOrderID + ",message:" + message);
                    }
                })
                // 6.设置退出通知(必接)
                .setExitNotifier(new ExitNotifier() {
 
                    @Override
                    public void onSuccess() {
                        // 进行游戏本身的退出操作,下面的finish()只是示例
                        finish();
                    }
 
                    @Override
                    public void onFailed(String message, String trace) {
                        infoTv.setText("退出失败:" + message);
                    }
                });
    }
 
}