我有一个应用内结算服务,其中有两个消耗品和一个非消耗品。
对于非消耗品,我正在检查购买清单,如果该物品已经拥有,我希望用户不再购买,而是提供优惠。我有两个类:Guess.java(Launcher文件)和Purchase.java(应用内结算设置文件)
Guess.java内部
Intent intent = new Intent(Guess.this,Purchase.class);
intent.putExtra("buy_2x", "buy_2x");
startActivity(intent);
buy_2x是非消耗品(购买选择在下面的代码中是2,而消费品是购买选择0和1的消费品),我指的是。在Purchase.java中,代码如下
public class Purchase extends Activity {
IInAppBillingService mService;
private Bundle extras;
private int add=0;//add coins to the kitty
private int buying_choice;
private String ITEM_SKU_SELECTION;
private static final String TAG = "com.apps.guess.inappbilling";
IabHelper mHelper;
private ServiceConnection mServiceConn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String base64EncodedPublicKey = "<64-char> long";
Intent intent = getIntent();
String buy2x_recd =intent.getExtras().getString("buy_2x");
//this is a class that contains static variables and I am using choices
//0,1,2...choice 2 is for the non-consumable buy_2x
if(UserData.getBuying_choice()!=2) ITEM_SKU_SELECTION = selectbuyingChoice(UserData.getBuying_choice());
//callService() method referred below
if(buy2x_recd.equalsIgnoreCase("buy_2x"))callService();
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new
IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Toast.makeText(getApplicationContext(), "In-app Billing setup failed: = ", Toast.LENGTH_LONG).show();
Log.d(TAG, "In-app Billing setup failed: ");
} else {
Log.d(TAG, "In-app Billing is set up OK");
mHelper.launchPurchaseFlow(Purchase.this, ITEM_SKU_SELECTION, 10001,mPurchaseFinishedListener, "");
mHelper.enableDebugLogging(true, TAG);
}
}
});
}//onCreate() ends
public void callService(){
mServiceConn = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
Log.d("TEST", "mService ready to go!");
checkownedItems();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
};
bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
}
private void checkownedItems()
{
try {
Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
int response = ownedItems.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList<String> ownedSkus =
ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
ArrayList<String> purchaseDataList =
ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
ArrayList<String> signatureList =
ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
String continuationToken =
ownedItems.getString("INAPP_CONTINUATION_TOKEN");
for (int i = 0; i < purchaseDataList.size(); ++i) {
String purchaseData = purchaseDataList.get(i);
String signature = signatureList.get(i);
String sku = ownedSkus.get(i);
if(sku.equalsIgnoreCase("buy_2x"))
{
AlertDialog.Builder builder = new AlertDialog.Builder(Purchase.this);
builder.setTitle("Purchased Already");
builder.setMessage("You have already purchased the 2X option. Do you want to enable it?");
builder.setPositiveButton("Yes!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int which) {
Toast.makeText(getApplicationContext(), "You have decided to double your gains!.Please wait...", Toast.LENGTH_LONG).show();
int double_2x_value=2*getResources().getIntArray(R.array.start_parameters)[2];
UserData.writeData(""+double_2x_value, Purchase.this, UserData.get2XFile());
UserData.setWinningValue(""+double_2x_value);
UserData.writeData("true", Purchase.this, UserData.getDoubleXPurchaseFile());
UserData.setBuy2X(true);
UserData.setBuying_choice(0);
dialog1.dismiss();
Intent intent = new Intent(Purchase.this, com.glambleapps.guesstheword.Guess.class);
startActivity(intent);
}});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int which) {
dialog1.dismiss();
}});
AlertDialog alert = builder.create();
alert.show();
break;
}
// do something with this purchase information
// e.g. display the updated list of products owned by user
}
// if continuationToken != null, call getPurchases again
// and pass in the token to retrieve more items
}
else {
Toast.makeText(Purchase.this, "No 2X purchase available on your device",Toast.LENGTH_LONG).show();
ITEM_SKU_SELECTION=selectbuyingChoice(2);
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String selectbuyingChoice(int buying_choice2) {
String ITEM_SKU_SELECTION=null;
switch(buying_choice2){
case 0:
ITEM_SKU_SELECTION = "buy_200_coins";
add = 200;
Toast.makeText(getApplicationContext(), "You have decided to purchase 200 Coins.Please wait...", Toast.LENGTH_LONG).show();
break;
case 1:
ITEM_SKU_SELECTION = "buy_1200_coins";
add = 1200;
Toast.makeText(getApplicationContext(), "You have decided to purchase 1200 Coins.Please wait...", Toast.LENGTH_LONG).show();
break;
case 2:
ITEM_SKU_SELECTION = "buy_2x";
add = 0;
Toast.makeText(getApplicationContext(), "You have decided to double your winnings!.Please wait...", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "NO BUYING CHOICE", Toast.LENGTH_SHORT).show();
break;
}
//Toast.makeText(getApplicationContext(), "ITEM_SKU_SELECTION = "+ITEM_SKU_SELECTION, Toast.LENGTH_SHORT).show();
return ITEM_SKU_SELECTION;
}
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase,
IabResult result) {
if (result.isSuccess()) {
int total =add+Integer.parseInt(UserData.getCoins_remaining());
if(!(ITEM_SKU_SELECTION.equalsIgnoreCase("buy_2x")))
Toast.makeText(getApplicationContext(), "Successful purchase done of "+add+" Guess-Coins!", Toast.LENGTH_LONG).show();
if(ITEM_SKU_SELECTION.equalsIgnoreCase("buy_2x")){
int double_2x_value=2*getResources().getIntArray(R.array.start_parameters)[2];
Toast.makeText(getApplicationContext(), "2X - Double your winnings! purchase is successful.", Toast.LENGTH_LONG).show();
UserData.setWinningValue(""+double_2x_value);
}
add=0;
Intent intent = new Intent(Purchase.this, Guess.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "NO PURCHASE DONE", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Purchase.this, Guess.class);
startActivity(intent);
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
if(mServiceConn != null)
{
unbindService(mServiceConn);
mServiceConn=null;
}
}
public void consumeItem() {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
Intent intent = new Intent(Purchase.this, Guess.class);
startActivity(intent);
} else {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU_SELECTION), mConsumeFinishedListener);
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(PurchaseActivity.this,"Sorry, you have canceled your coins purchase.",Toast.LENGTH_SHORT).show();
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase)
{
if (result.isFailure()) {
Intent intent = new Intent(Purchase.this, Guess.class);
startActivity(intent);
return;
}
else if (purchase.getSku().equals(ITEM_SKU_SELECTION)) {consumeItem();
}
}
};
}//class ends
我收到以下警告:
Implicit intents with startService are not safe: Intent { act=com.android.vending.billing.InAppBillingService.BIND } android.content.ContextWrapper.bindService:517 Purchase.callService:107 Purchase.onCreate:68
错误如下:
IabHelper(25782): In-app billing error: Unable to buy item, Error response: 5:Developer Error
UserData.java存储一些值并写入数据文本文件。他们没有问题。
这是什么问题?请帮忙。谢谢
答案 0 :(得分:0)
您是否尝试使用GooglePlay开发者帐户购买设备上记录的商品?
谷歌不允许这样做。你应该使用另一个。
在开发者控制台中,转到“帐户设置”并添加一些帐户进行测试。然后,使用其中一个登录,尝试购买该项目。你应该能够实现它。
编辑:
检查文档,我发现您必须使用有效签名的apk http://developer.android.com/google/play/billing/billing_reference.html进行测试
答案 1 :(得分:0)
您只需要在清单上给PAY_THROUGH _...(您在其中应用的evrey商店): 例如,cafebazar商店: 或用于Play商店:...。