アプリに Google を実装しました。アプリを公開した後、正常に動作します。しかし、私は問題に直面しています。ユーザーがアプリをインストールしてアイテムを購入し、アプリをアンインストールして再インストールし、購入したアイテムに移動すると、アプリは購入したアイテムをロックして購入リクエストを求めます。OnRestoreTransaction に問題はありますか? Google In app Purchse サイトのコードを使用しました。コードは次のとおりです。
private class DungeonsPurchaseObserver extends PurchaseObserver {
public DungeonsPurchaseObserver(Handler handler) {
super(in_app.this, handler);
}
@Override
public void onBillingSupported(boolean supported) {
if (Consts.DEBUG) {
Log.i(TAG, "supported: " + supported);
}
if (supported) {
restoreDatabase();
} else {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
@Override
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
int quantity, long purchaseTime, String developerPayload) {
if (Consts.DEBUG) {
Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
}
if (purchaseState == PurchaseState.PURCHASED ) {
finish();
}
}
@Override
public void onRequestPurchaseResponse(RequestPurchase request,
ResponseCode responseCode) {
if (Consts.DEBUG) {
Log.d(TAG, request.mProductId + ": " + responseCode);
}
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.i(TAG, "purchase was successfully sent to server");
}
} else if (responseCode == ResponseCode.RESULT_USER_CANCELED) {
if (Consts.DEBUG) {
Log.i(TAG, "user canceled purchase");
}
} else {
if (Consts.DEBUG) {
Log.i(TAG, "purchase failed");
}
}
}
@Override
public void onRestoreTransactionsResponse(RestoreTransactions request,
ResponseCode responseCode) {
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.d(TAG, "completed RestoreTransactions request");
}
// Update the shared preferences so that we don't perform
// a RestoreTransactions again.
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(DB_INITIALIZED, true);
edit.commit();
} else {
if (Consts.DEBUG) {
Log.d(TAG, "RestoreTransactions error: " + responseCode);
}
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.in_app);
mHandler = new Handler();
mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler);
mBillingService = new BillingService();
mBillingService.setContext(this);
mPurchaseDatabase = new PurchaseDatabase(this);
//setupWidgets();
// Check if billing is supported.
ResponseHandler.register(mDungeonsPurchaseObserver);
if (!mBillingService.checkBillingSupported()) {
showDialog(DIALOG_CANNOT_CONNECT_ID);
}
purchase=(Button) findViewById(R.id.purchase);
cancel=(Button) findViewById(R.id.cancel);
purchase.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(!ownedItems.contains("android.test.refunded")){
if (Consts.DEBUG) {
Log.d(TAG, "buying: " + " product" + " Product Name: " + "Product");
}
if (!mBillingService.requestPurchase("android.test.refunded", mPayloadContents)) {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
}
});
編集:
アプリ内 Java ファイルの onCrearte でこれを呼び出します。これは、現在のユーザーが要求しているアイテムを購入したかどうかを最初に確認し、そうでない場合は購入のリクエストを開始します。onPurchsestateChanged では、状態が購入済みかどうかのみを確認しました。
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.in_app);
mHandler = new Handler();
mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler);
mBillingService = new BillingService();
mBillingService.setContext(this);
mPurchaseDatabase = new PurchaseDatabase(this);
//setupWidgets();
// Check if billing is supported.
ResponseHandler.register(mDungeonsPurchaseObserver);
if (!mBillingService.checkBillingSupported()) {
showDialog(DIALOG_CANNOT_CONNECT_ID);
}
System.out.println("for check"+getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false));
if (getPreferences(Context.MODE_PRIVATE).getBoolean(DB_INITIALIZED, false)) {
System.out.println(mBillingService.restoreTransactions());
}
else{
purchase=(Button) findViewById(R.id.purchase);
cancel=(Button) findViewById(R.id.cancel);
purchase.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(!ownedItems.contains("android.test.refunded")){
if (Consts.DEBUG) {
Log.d(TAG, "buying: " + " product" + " Product Name: " + "i");
}
if (!mBillingService.requestPurchase("android.test.refunded", mPayloadContents)) {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
startActivity(new Intent(in_app.this, Main.class));
overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
}
});
}
}
OnPurchaseStateChanged:
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
int quantity, long purchaseTime, String developerPayload) {
if (Consts.DEBUG) {
Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
}
System.out.println("here for new fun");
if (purchaseState == PurchaseState.PURCHASED ) {
finish();
Intent intent1 = new Intent(context,BodyParts1.class);
context.startActivity(intent1);
}
}