簡體   English   中英

Android應用內購買

[英]Android In App Purchases

我創建了一個Android應用,該應用有兩個活動,一個是無廣告,另一個是有廣告。 我想在應用內購買中創建廣告來刪除廣告,因此,當用戶購買該應用時,它會向他顯示無廣告活動,如果沒有,則向他顯示廣告活動。 有人可以告訴我該怎么做。 這是我的代碼。

import cf.droiddev.androidtutorials.util.IabHelper;
import cf.droiddev.androidtutorials.util.IabResult;
import cf.droiddev.androidtutorials.util.Inventory;
import cf.droiddev.androidtutorials.util.Purchase;

public class RemoveAdsActivity extends Activity {

static final String TAG = "Android Tutorials";

static final String SKU_INAPPITEM = "android.test.purchased"; //"change to your in app item"; // "android.test.cancelled";

String base64EncodedPublicKey = "REPLACE WITH YOUR PUBLIC KEY";

// The helper object
IabHelper mHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.remove_ads_activity);

    mHelper = new IabHelper(this, base64EncodedPublicKey);

    // enable debug logging (for a production application, you should set
    // this to false).
    mHelper.enableDebugLogging(true);

    // Start setup. This is asynchronous and the specified listener
    // will be called once setup completes.
    Log.d(TAG, "Starting setup.");
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        @Override
        public void onIabSetupFinished(IabResult result) {
            Log.d(TAG, "Setup finished.");

            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                complain("Problem setting up in-app billing: " + result);
                return;
            }

            // Hooray, IAB is fully set up. Now, let's get an inventory of
            // stuff we own.
            Log.d(TAG, "Setup successful. Querying inventory.");
            mHelper.queryInventoryAsync(mGotInventoryListener);
        }
    });

}

// Listener that's called when we finish querying the items and
// subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    @Override
    public void onQueryInventoryFinished(IabResult result,
            Inventory inventory) {
        Log.d(TAG, "Query inventory finished.");
        if (result.isFailure()) {
            complain("Failed to query inventory: " + result);
            return;
        }

        Log.d(TAG, "Query inventory was successful.");

        /*
         * Check for items we own. Notice that for each purchase, we check
         * the developer payload to see if it's correct! See
         * verifyDeveloperPayload().
         */

        // Check for gas delivery -- if we own gas, we should fill up the
        // tank immediately
        Purchase removeAdsPurchase = inventory.getPurchase(SKU_INAPPITEM);
        if (removeAdsPurchase != null
                && verifyDeveloperPayload(removeAdsPurchase)) {
            Log.d(TAG, "User has already purchased this item for removing ads. Write the Logic for removign Ads.");
            mHelper.consumeAsync(inventory.getPurchase(SKU_INAPPITEM),
                    mConsumeFinishedListener);
            return;
        }

        Log.d(TAG, "Initial inventory query finished; enabling main UI.");
    }

};

// User clicked the "Buy Gas" button
public void onBuyGasButtonClicked(View arg0) {
    Log.d(TAG, "Buy gas button clicked.");

    /*
     * TODO: for security, generate your payload here for verification. See
     * the comments on verifyDeveloperPayload() for more info. Since this is
     * a SAMPLE, we just use an empty string, but on a production app you
     * should carefully generate this.
     */
    String payload = "";

    mHelper.launchPurchaseFlow(this, SKU_INAPPITEM, 10000,
            mPurchaseFinishedListener, payload);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + ","
            + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        // not handled, so handle it ourselves (here's where you'd
        // perform any handling of activity results not related to in-app
        // billing...
        super.onActivityResult(requestCode, resultCode, data);
    } else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
    }
}

// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    @Override
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        Log.d(TAG, "Purchase finished: " + result + ", purchase: "
                + purchase);
        if (result.isFailure()) {
            complain("Error purchasing: " + result);
            return;
        }
        if (!verifyDeveloperPayload(purchase)) {
            complain("Error purchasing. Authenticity verification failed.");
            return;
        }

        Log.d(TAG, "Purchase successful.");

        if (purchase.getSku().equals(SKU_INAPPITEM)) {
            // bought 1/4 tank of gas. So consume it.
            Log.d(TAG,
                    "removeAdsPurchase was succesful.. starting consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);
        }
    }
};

// Called when consumption is complete
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
    @Override
    public void onConsumeFinished(Purchase purchase, IabResult result) {
        Log.d(TAG, "Consumption finished. Purchase: " + purchase
                + ", result: " + result);

        // We know this is the "gas" sku because it's the only one we
        // consume,
        // so we don't check which sku was consumed. If you have more than
        // one
        // sku, you probably should check...
        if (result.isSuccess()) {
            // successfully consumed, so we apply the effects of the item in
            // our
            // game world's logic, which in our case means filling the gas
            // tank a bit
            Log.d(TAG, "Consumption successful. Provisioning.");
            alert("You have purchased for removing ads from your app.");
        } else {
            complain("Error while consuming: " + result);
        }
        Log.d(TAG, "End consumption flow.");
    }
};

/** Verifies the developer payload of a purchase. */
boolean verifyDeveloperPayload(Purchase p) {
    String payload = p.getDeveloperPayload();

    /*
     * TODO: verify that the developer payload of the purchase is correct.
     * It will be the same one that you sent when initiating the purchase.
     * 
     * WARNING: Locally generating a random string when starting a purchase
     * and verifying it here might seem like a good approach, but this will
     * fail in the case where the user purchases an item on one device and
     * then uses your app on a different device, because on the other device
     * you will not have access to the random string you originally
     * generated.
     * 
     * So a good developer payload has these characteristics:
     * 
     * 1. If two different users purchase an item, the payload is different
     * between them, so that one user's purchase can't be replayed to
     * another user.
     * 
     * 2. The payload must be such that you can verify it even when the app
     * wasn't the one who initiated the purchase flow (so that items
     * purchased by the user on one device work on other devices owned by
     * the user).
     * 
     * Using your own server to store and verify developer payloads across
     * app installations is recommended.
     */

    return true;
}

void complain(String message) {
    Log.e(TAG, "**** IN APP Purchase Error: " + message);
    alert(message);
}

void alert(String message) {
    Log.d(TAG, "Showing alert dialog: " + message);
    TextView resultTv = (TextView) findViewById(R.id.textView_result);
    resultTv.setText("Result : " + message);
}

}

任何人都可以告訴我如何創建開發人員有效負載,我們將不勝感激。

我可以在此處回答主要問題^^如果尚未完成,則將帶有廣告的活動設置為啟動活動。 在該活動的onCreate()方法中,您將在onCreate()的末尾添加以下內容:

if([Purchased?]
{
Intent adfreeactivity = new Intent(NameOfCurrentActivity.this, NameOfTargetActivity.class);
startActivity(adfreeactivity);
}

之后,您的應用應在啟動時啟動正確的活動。 此外,如果用戶離線,最好將結果保存在SharedPreferences中,並且僅當用戶在線時再次檢查,並僅在最后一次檢查返回成功購買時才切換“活動”。

希望我能為您提供幫助,馬克斯·穆勒

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM