public class MainActivity extends Activity implements OnClickListener { Button btn1; private Context mContext=this; private static final String TAG = "Android BillingService"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(this); startService(new Intent(mContext, BillingService.class)); BillingHelper.setCompletedHandler(mTransactionHandler); } public Handler mTransactionHandler = new Handler(){ public void handleMessage(android.os.Message msg) { Log.i(TAG, "Transaction complete"); Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState); Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId); if(BillingHelper.latestPurchase.isPurchased()){ showItem(); } }; }; @Override public void onClick(View v) { if (v == btn1) { if(BillingHelper.isBillingSupported()){ BillingHelper.requestPurchase(mContext, "android.test.purchased"); } else { Log.i(TAG,"Can't purchase on this device"); btn1.setEnabled(false); // XXX press button before service started will disable when it shouldnt } Toast.makeText(this, "Purchase Books", Toast.LENGTH_SHORT).show(); } } private void showItem() { //purchaseableItem.setVisibility(View.VISIBLE); } @Override protected void onPause() { Log.i(TAG, "onPause())"); super.onPause(); } @Override protected void onDestroy() { BillingHelper.stopService(); super.onDestroy(); } }
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.BILLING"/> <uses-permission android:name="com.android.vending.BILLING" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)