1.界面的频繁跳转会费时(尤其对于用cocos2d-x写的游戏来说界面跳出跳进延时非常大,会影响游戏性);
2.开启新Activity会涉及到数据的传递,新Activity的维护及销毁,这里力求简洁,频繁的数据传递难免会出错(大家都知道,支付的逻辑是不同意出现丁点错误的),假设单一渠道的付费逻辑上非常easy,假设集合了5、6个甚至很多其它的付费方式的时候以及掺杂游戏逻辑之后,支付逻辑不够简洁会使你头疼。
3.正在尝试的让cocos2d-x的游戏跟Unity3D的游戏使用一套付费接口,这种话Activity多了不会是一个好的选择
4.假设使用Activity那么AndroidManifest里边必须要注意注冊此Activity,这是一个定时炸弹,由于不同支付方式都须要在Manifest里边注冊相关配置,当配置量大了以后难免会漏掉次Activity的声明,SDK的接入是批量的你可能会同一时候接不同的游戏和不同的付费方式,这就须要在最短的时间里准确的接好多种游戏及付费方式,因此注意事项越少越好。
机锋的支付是比較简单的一种支付方式,并且其本身已经集成了支付宝和充值卡支付,如今我们来看下代码:
AndroiManifest.xml
- <activity
- android:name="com.gfan.sdk.payment.PaymentsActivity"
- android:configChanges="orientation|keyboardHidden" />
- <activity
- android:name="com.gfan.sdk.charge.ChargeActivity"
- android:configChanges="orientation|keyboardHidden" />
- <activity
- android:name="com.gfan.sdk.account.LoginActivity"
- android:configChanges="orientation|keyboardHidden"
- android:windowSoftInputMode="adjustUnspecified" />
- <meta-data
- android:name="gfan_pay_appkey"
- android:value="XXX" />
- <meta-data
- android:name="gfan_type"
- android:value="paysdk" />
- <meta-data
- android:name="gfan_cpid"
- android:value="testcpid" />
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Fiap.java
- package com.example.blogforgfan;
- // 机锋应用内支付
- import com.gfan.sdk.payment.PaymentInfo;
- import com.gfan.sdk.payment.PaymentsActivity;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.View;
- @SuppressLint("HandlerLeak")
- public class Fiap extends Activity {
- final int CATCAP_ANDROID_PAY = 0; // 跳转Activity请求码
- int catcap_android_pay_catcap_coin = 0; // 购买的猫币
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViewById(R.id.button1).setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- android_pay(2);
- }
- });
- }
- // ===================================
- // JAVA
- // ===================================
- public void android_pay(int index) {
- if (!is_can_internet(Fiap.this)) {
- fiapHandler.sendEmptyMessage(1);
- return;
- }
- int coin = 0;
- switch (index) {
- case 0:
- coin = 2;
- this.catcap_android_pay_catcap_coin = 50;
- break;
- case 1:
- coin = 4;
- this.catcap_android_pay_catcap_coin = 120;
- break;
- case 2:
- coin = 6;
- this.catcap_android_pay_catcap_coin = 200;
- break;
- case 3:
- coin = 8;
- this.catcap_android_pay_catcap_coin = 300;
- break;
- default:
- break;
- }
- // coin *= 10;此处coin是人民币,换算成机锋券需*10
- // 设置购买信息(购买的道具名,付费描写叙述,支付金额,订单号)
- // PS:这里须要自己组织订单号,所以需保证每次订单号是唯一的,此处传入的金额是机锋券(1元=10机锋券)
- PaymentInfo info = new PaymentInfo("猫币", "购买游戏所用猫币", coin,
- get_order_id());
- // 到购买页面
- Intent intent = new Intent(this, PaymentsActivity.class);
- intent.putExtra(PaymentsActivity.EXTRA_KEY_PAYMENTINFO, info);
- startActivityForResult(intent, CATCAP_ANDROID_PAY);
- }
- // ===================================
- // Android
- // ===================================
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == CATCAP_ANDROID_PAY) { // 保证结果码跟请求码一致
- if (resultCode == RESULT_OK) { // 支付成功,这里填写支付成功后的逻辑
- new AlertDialog.Builder(this).setTitle("感谢购买猫币")
- .setPositiveButton("确定", null).create().show();
- }
- }
- // 清空数据
- this.catcap_android_pay_catcap_coin = 0;
- super.onActivityResult(requestCode, resultCode, data);
- }
- public boolean is_can_internet(final Context context) {
- try {
- ConnectivityManager manger = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = manger.getActiveNetworkInfo();
- return (info != null && info.isConnected());
- } catch (Exception e) {
- return false;
- }
- }
- public String get_order_id() {
- long ran1 = get_round(1111, 9999);
- long ran2 = get_round(1111, 9999);
- // return android_get_umeng_channel () + "_" + ran1 +
- // System.currentTimeMillis () + ran2;
- return "_" + ran1 + System.currentTimeMillis() + ran2;
- }
- public long get_round(int min, int max) {
- return Math.round(Math.random() * (max - min) + min);
- }
- Handler fiapHandler = new Handler() {
- public void handleMessage(android.os.Message msg) {
- if (msg.what == 1) {
- new AlertDialog.Builder(Fiap.this).setTitle("提示")
- .setMessage("连接不到网络。").setPositiveButton("确定", null)
- .create().show();
- }
- };
- };
- }