1

I have a simple app that has an unlock feature in it. When a user purchases this they can unlock more content in the app. When I debug the app I don't get any errors, but I am unable to retrieve a list of products using the google billing services. On my main activity I would like to check if the user has all ready purchased the "upgrade" if they have do something with that information.

I have been following the api documentation but its not really helping.

In App Billing Reference

Implementation

Testing

I have 1 item in my In-app products, I have followed the testing document and added an apk to my Alpha testing. The problem here is that apk cant have debugging enabled, so when I use the debugger in android studio the app may perform differently?? I think my code is OK,but any help or guidance in how to test this would be great.

MainActivity.java

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.android.vending.billing.IInAppBillingService;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

    IInAppBillingService mService;
    ServiceConnection connection;
    Intent serviceIntent;

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



        Button start = (Button) findViewById(R.id.startgame);
        Button settings = (Button) findViewById(R.id.about);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), StartGame.class);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
            }
        });

        settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), Settings.class);
                startActivity(i);
                overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
            }
        });


        /// check for pro unlock
        serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        // first this
        connection = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {
                mService = null;

            }
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d("BILLING", "Connected");
                //purchse info
                mService = IInAppBillingService.Stub.asInterface(service);
                checkPurchaseInfo();
            }
        };
        bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);


    }
    private void checkPurchaseInfo() {
        try {

            Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
            int responseCode = ownedItems.getInt("RESPONSE_CODE");
            Log.d("BILLING","Request Code: " + responseCode);
            if(responseCode == 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_LIST");
                String continuationToken = ownedItems.getString("INAPP_CONTINUATION_TOKEN");

                Log.d("OWNED",ownedSkus.toString());
                Log.d("purcahseList",purchaseDataList.toString());
                Log.d("SIGNLIST",signatureList.toString());
                /////////
                //all of these arrays come back as empty []
                ////////
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (connection != null) {
            unbindService(connection);
        }
    }
}
Paul Ledger
  • 1,125
  • 4
  • 21
  • 46

0 Answers0