Saturday, August 13, 2011

in-app purchase匯入步驟

以下文章從Billing Android- implementing in-app billing取材整理


1.將Billing Sample src裡的com.android.vending.billing複製到你的專案裡
2.在AndroidManifest.xml裡的user-permission添加
com.android.vending.BILLING
註︰如果沒有宣告就請求billing機制時,將會收到
RESULT_DEVELOPER_ERRO反應

3. 建立BroadcastReceiver和Service(見步驟4)

BroadcastReceiver - 用來處理從Android Market傳來的Billing訊息 
Service - 綁剛才從billing sample src複製過來的IMarketBillingService.aidl

哦!對了。AndroidManifest.xml裡的BroadcastReceiver要設 intent filters 

      
        
        
        
      
    
4.攢寫Service
這個Service將要做哪些事?
(1)綁定IMarketBillingService.aidl
(2)向Android Market發佈Billing請求
  • CHECK_BILLING_SUPPORTED requests
  • REQUEST_PURCHASE requests
  • GET_PURCHASE_INFORMATION requests
  • CONFIRM_NOTIFICATIONS requests
  • RESTORE_TRANSACTIONS requests
(3)同步處理以上這些請求的回應


============================
(1)綁定IMarketBillingService.aidl詳細說明
  1. 建立一個Service並實作serviceConnection,然後將底下的Code放進OnCreate()
  2. try {
      boolean bindResult = mContext.bindService(
        new Intent("com.android.vending.billing.MarketBillingService.BIND"), this,
        Context.BIND_AUTO_CREATE);
      if (bindResult) {
        Log.i(TAG, "Service bind successful.");
      } else {
        Log.e(TAG, "Could not bind to the MarketBillingService.");
      }
    } catch (SecurityException e) {
      Log.e(TAG, "Security exception: " + e);
    }
     
  3.  建立callback(將以下程式碼貼進自創的Service裡)
  4. /**
      * The Android system calls this when we are connected to the MarketBillingService.
      */
      public void onServiceConnected(ComponentName name, IBinder service) {
        Log.i(TAG, "MarketBillingService connected.");
        mService = IMarketBillingService.Stub.asInterface(service);
      }
     
  5.  可以開始用mService去 sendBillingRequest()任何需求了!
 記得將developer發佈頁面裡的公開金鑰放進Security.java裡的base64EncodedPublicKey變數中

    No comments: