1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android 结算权限 如何在Android应用程序中实施应用内结算?

android 结算权限 如何在Android应用程序中实施应用内结算?

时间:2022-06-30 01:34:33

相关推荐

android 结算权限 如何在Android应用程序中实施应用内结算?

小编典典

好吧,我将尝试解释我的经历。我不认为自己是专家,但是几天我都伤透了脑筋。

对于初学者来说,我很难理解示例和应用程序的工作流程。我认为从一个简单的示例开始应该会更好,但是将代码分成小块很难并且不知道是否破坏了任何代码是非常困难的。我将告诉您我所拥有的以及为使示例工作而对示例进行的更改。

我有一个活动,所有购买都来自此活动。叫做Pro。

首先,您应该使用公共Market开发人员密钥更新Security类中的变量base64EncodedPublicKey,否则您将看到一个不错的Exception。

好吧,我将Activity绑定到BillingService上是这样的:

public class Pro extends TrackedActivity implements OnItemClickListener {

private BillingService mBillingService;

private BillingPurchaseObserver mBillingPurchaseObserver;

private Handler mHandler;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.pro);

//Do my stuff

mBillingService = new BillingService();

mBillingService.setContext(getApplicationContext());

mHandler = new Handler();

mBillingPurchaseObserver = new BillingPurchaseObserver(mHandler);

}

}

@Override

protected void onStart() {

//Register the observer to the service

super.onStart();

ResponseHandler.register(mBillingPurchaseObserver);

}

@Override

protected void onStop() {

//Unregister the observer since you dont need anymore

super.onStop();

ResponseHandler.unregister(mBillingPurchaseObserver);

}

@Override

protected void onDestroy() {

//Unbind the service

super.onDestroy();

mBillingService.unbind();

}

这样,所有购买者都与该服务对话,然后,该服务会将JSON请求发送到市场。您可能会认为购买是在同一瞬间完成的,但没有。您发送请求后,购买可能会在几分钟或几小时后到来。我认为这主要是因为服务器超负荷和

信用卡审批。

然后,我有了一个包含商品的ListView,然后在每个商品上打开一个AlertDialog,邀请他们购买商品。当他们单击某个项目时,我会这样做:

private class BuyButton implements DialogInterface.OnClickListener {

private BillingItem item = null;

private String developerPayload;

public BuyButton(BillingItem item, String developerPayload) {

this.item = item;

this.developerPayload = developerPayload;

}

@Override

public void onClick(DialogInterface dialog, int which) {

if (GeneralHelper.isOnline(getApplicationContext())){

//I track the buy here with GA SDK.

mBillingService.requestPurchase(this.item.getSku(), this.developerPayload);

} else {

Toast.makeText(getApplicationContext(), R.string.msg_not_online, Toast.LENGTH_SHORT).show();

}

}

}

好了,您应该看到市场打开,用户完成或取消了购买。

然后重要的是我的PurChaseObserver,它处理市场发送的所有事件。这是一个剥离的版本,但您应该明白这一点

(请参阅代码中的我的评论):

private class BillingPurchaseObserver extends PurchaseObserver {

public BillingPurchaseObserver(Handler handler) {

super(Pro.this, handler);

}

@Override

public void onBillingSupported(boolean supported) {

if (supported) {

//Enable buy functions. Not required, but you can do stuff here. The market first checks if billing is supported. Maybe your country is not supported, for example.

} else {

Toast.makeText(getApplicationContext(), R.string.billing_not_supported, Toast.LENGTH_LONG).show();

}

}

@Override

public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,

int quantity, long purchaseTime, String developerPayload) {

//This is the method that is called when the buy is completed or refunded I believe.

// Here you can do something with the developerPayload. Its basically a Tag you can use to follow your transactions. i dont use it.

BillingItem item = BillingItem.getBySku(getApplicationContext(), itemId);

if (purchaseState == PurchaseState.PURCHASED) {

if (item != null){

//This is my own implementation that sets the item purchased in my database. BillingHelper is a class with methods I use to check if the user bought an option and update the UI. You should also check for refunded. You can see the Consts class to find what you need to check for.

boolean resu = item.makePurchased(getApplicationContext());

if (resu){

Toast.makeText(getApplicationContext(), R.string.billing_item_purchased, Toast.LENGTH_LONG).show();

}

}

}

}

private void trackPurchase(BillingItem item, long purchaseTime) {

//My code to track the purchase in GA

}

@Override

public void onRequestPurchaseResponse(RequestPurchase request,

ResponseCode responseCode) {

//This is the callback that happens when you sent the request. It doesnt mean you bought something. Just that the Market received it.

if (responseCode == ResponseCode.RESULT_OK) {

Toast.makeText(getApplicationContext(), R.string.billing_item_request_sent, Toast.LENGTH_SHORT).show();

} else if (responseCode == ResponseCode.RESULT_USER_CANCELED) {

//The user canceled the item.

} else {

//If it got here, the Market had an unexpected problem.

}

}

@Override

public void onRestoreTransactionsResponse(RestoreTransactions request,

ResponseCode responseCode) {

if (responseCode == ResponseCode.RESULT_OK) {

//Restore transactions should only be run once in the lifecycle of your application unless you reinstalled the app or wipe the data.

SharedPreferences.Editor edit = PreferencesHelper.getInstance().getDefaultSettings(getApplicationContext()).edit();

edit.putBoolean(Consts.DB_INITIALIZED, true);

mit();

} else {

//Something went wrong

}

}

}

而且我相信您不需要编辑其他任何内容。代码的其余部分“有效”。您可以首先在自己的“ android.test.purchased” 项目中尝试使用示例SKU 。到目前为止,我已经对此进行了测试,并且可以正常工作,但是我仍然需要涵盖退款状态之类的所有内容。在这种情况下,我让用户保留这些功能,但我想确保在修改之前可以正常使用。

希望对您和其他人有帮助。

-09-18

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。