Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 购买并不总是记录_Android_In App Purchase - Fatal编程技术网

Android 购买并不总是记录

Android 购买并不总是记录,android,in-app-purchase,Android,In App Purchase,有时,我的用户报告说,应用内购买没有被记录(即使它通过并被授权)。我使用了驱动器示例,并具有以下代码: public class FragmentGoPro extends Fragment{ private static final String TAG = AppConstants.Keys.TAG; private final String SKU_PREMIUM = "001"; private IabHelper mHelper; private static final int R

有时,我的用户报告说,应用内购买没有被记录(即使它通过并被授权)。我使用了驱动器示例,并具有以下代码:

public class FragmentGoPro extends Fragment{

private static final String TAG = AppConstants.Keys.TAG;
private final String SKU_PREMIUM = "001";
private IabHelper mHelper;
private static final int RC_REQUEST = 546731;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View rootView = inflater.inflate(R.layout.fragment_billing, container, false);
    getActivity().getActionBar().setTitle(getActivity().getString(R.string.app_name));
    setupHelper();

    TextView descriptionView = (TextView)rootView.findViewById(R.id.pro_description);
    String proDescription = getString(R.string.pro_description);
    descriptionView.setText(proDescription);

    Button goPro = (Button)rootView.findViewById(R.id.purchase);
    goPro.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            makePurchase();

        }
    });
    return rootView;
}
private void setupHelper() {

    mHelper = new IabHelper(getActivity(), AppConstants.Keys.PRO);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {

            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.

            mHelper.queryInventoryAsync(mGotInventoryListener);
        }
    });

}
private void makePurchase() {
    Log.i(TAG, "MakePurchase requested.");

    mHelper.launchPurchaseFlow(getActivity(), SKU_PREMIUM, RC_REQUEST,
            mPurchaseFinishedListener, null);
}
// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        Log.i(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
        if (result.isFailure()) {
            complain("Error purchasing: " + result);
            return;
        }
        Log.i(TAG, "Purchase of " + purchase.getSku() + " Sku confirmed.");

        if (purchase.getSku().equals(SKU_PREMIUM)) {
            // bought the premium upgrade!
            alert("Thank you for upgrading to premium!");

            Log.i(TAG, "Premium features unlocked!");

            setPreference("mIsPremium", true);
            setPreference("Purchased", true);
            updateMetrics();
            updateNav();
        }
    }
};
protected IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        if (result.isFailure()) {
            complain("Failed to query inventory: " + result);
            return;
        }

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

        // Do we have the premium upgrade?
        Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
        boolean mIsPremium = (premiumPurchase != null);
        if (mIsPremium) {
            Log.i(TAG, "Premium purchased previously, restoring transactions.");
            setPreference("mIsPremium", mIsPremium);
            setPreference("Purchased", true);
            updateNav();
        }

    }
};
private void updateMetrics() {
    EasyTracker.getInstance().setContext(getActivity());

    flurryEvent("state", "purchase_state", "purchased");

    if (!checkIfTesting()) {
        Log.i(TAG, "Purchase Logged");
        Transaction myTrans = new Transaction.Builder(
                "0_3164",                                           // (String) Transaction Id, should be unique.
                (long) 2.99 * 1000000)                                // (long) Order total (in micros)
                .setAffiliation("In-App Google Play")                       // (String) Affiliation

                .build();

        myTrans.addItem(new Transaction.Item.Builder(
                "001",                                              // (String) Product SKU
                "Pro License",                                    // (String) Product name
                (long) 2.99 * 1000000,                                // (long) Product price (in micros)
                (long) 1 )                                            // (long) Product quantity
                .build());
        Tracker myTracker = EasyTracker.getTracker(); // Get reference to tracker.
        myTracker.sendTransaction(myTrans); // Track the transaction.

    }
}
protected void updateNav() {
    Activity activity = getActivity();

    if(activity instanceof MainActivity) {
        ((MainActivity) activity).initializeNavigation();
    } else {
        getActivity().finish();
    }

}
@Override
public 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.");
    }
}

我的updateMetrics()和updateNav()似乎没有被调用,但在我看来,它总是应该被调用的。同样,这只发生在一小部分用户身上。

我认为这与我的orderID是静态的有关。我按照指南使用更新的分析。使用System.currentTimeInMillis()应提供唯一的orderID并防止数据冲突

此外,我在一个片段中使用了它,所以没有调用onActivityResult(奇怪的是,它在我的测试设备上工作),所以我需要修改计费代码