MainActivity.java
4.49 KB
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
package com.gump.cohesivesdk.sample;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.gump.cohesivesdk.CohesiveApi;
import com.gump.cohesivesdk.callback.ExitCallback;
import com.gump.cohesivesdk.callback.InitialCallback;
import com.gump.cohesivesdk.callback.PaymentListener;
import com.gump.cohesivesdk.callback.UserStateListener;
import com.gump.cohesivesdk.model.GPUser;
import com.gump.cohesivesdk.model.Payment;
import com.tencent.tmgp.testgump.R;
public class MainActivity extends Activity implements UserStateListener {
private static final String TAG = "MainActivity";
CohesiveApi cohesiveApi;
private Button btnLogin;
private Button btnPay;
private TextView tvInfo;
private boolean isLogin = false;
private ProgressDialog prog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CohesiveApi.setDebugging(true);
cohesiveApi = CohesiveApi.build(this);
cohesiveApi.onCreate(this, getIntent());
cohesiveApi.init(this, "1002", new InitialCallback() {
@Override
public void onInitComplete(int result) {
Log.d(TAG, "sdk init completed result:" + result);
if (result == 0)
setListeners();
}
});
cohesiveApi.setUserListener(this);
btnLogin = (Button) findViewById(R.id.login);
btnPay = (Button) findViewById(R.id.pay);
tvInfo = (TextView) findViewById(R.id.info);
tvInfo.setText("SDK Version:" + CohesiveApi.getVersion());
}
private void setListeners() {
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prog = ProgressDialog.show(MainActivity.this, "", isLogin ? "正在注销" : "正在登录");
if (isLogin)
cohesiveApi.logout(MainActivity.this);
else
cohesiveApi.login(MainActivity.this);
}
});
btnPay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle payinfo = new Bundle();
payinfo.putString("orderId", System.currentTimeMillis() + "");
payinfo.putString("product", "coin");
payinfo.putString("extra", "buy 10 coins");
payinfo.putLong("amount", 1);
cohesiveApi.goPay(MainActivity.this, payinfo, new PaymentListener() {
@Override
public void onPaySuccess(Payment arg0) {
Log.i(TAG, arg0.toString());
}
@Override
public void onPayError(int code) {
Log.i(TAG, "pay Error:" + code);
}
});
}
});
}
@Override
public void onBackPressed() {
cohesiveApi.exit(this, new ExitCallback() {
@Override
public void onNo3rdExiterProvide() {
// 退出窗口需要游戏自定义,并且实现资源回收,杀死进程等退出逻辑
// TODO 实现退出窗口
cohesiveApi.appDestroy(MainActivity.this);
MainActivity.super.onBackPressed();
}
@Override
public void onExit() {
// 退出窗口由渠道提供,游戏只须实现资源回收,杀死进程等退出逻辑
cohesiveApi.appDestroy(MainActivity.this);
// 调用游戏退出逻辑
MainActivity.super.onBackPressed();
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
cohesiveApi.onNewIntent(this, intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
cohesiveApi.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
cohesiveApi.onPause(this);
}
@Override
protected void onResume() {
super.onResume();
cohesiveApi.onResume(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
cohesiveApi.onDestroy(this);
}
@Override
protected void onRestart() {
super.onRestart();
cohesiveApi.onRestart(this);
}
@Override
protected void onStop() {
super.onStop();
cohesiveApi.onStop(this);
}
@Override
public void onLoginFailed(String arg0) {
Log.i(TAG, "Login Failed:" + arg0);
if (prog != null)
prog.cancel();
}
@Override
public void onLoginSuccess(GPUser arg0) {
Log.i(TAG, "Login success:" + arg0.toString());
btnLogin.setText("logout");
isLogin = true;
if (prog != null)
prog.cancel();
}
@Override
public void onLogout() {
Log.i(TAG, "logout");
btnLogin.setText("login");
isLogin = false;
if (prog != null)
prog.cancel();
}
}