會遇到有些舊的類別在新版已不推薦使用,
因為新版推出了更好的功能。
產品經理(PM)在此時又要求2種功能都存留,
這時候下面這個判斷就變得很好用了。
if (android.os.Build.VERSION.SDK_INT >= 8){
//在Android 2.2以上要用什麼類別
}else{
//在2.2以下又要怎麼處理同一個需求
)
if (android.os.Build.VERSION.SDK_INT >= 8){
//在Android 2.2以上要用什麼類別
}else{
//在2.2以下又要怎麼處理同一個需求
)
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 requestsREQUEST_PURCHASE requestsGET_PURCHASE_INFORMATION requestsCONFIRM_NOTIFICATIONS requestsRESTORE_TRANSACTIONS requests============================(1)綁定IMarketBillingService.aidl詳細說明建立一個Service並實作serviceConnection,然後將底下的Code放進OnCreate()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);
}
建立callback(將以下程式碼貼進自創的Service裡)/**
* 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);
}
可以開始用mService去 sendBillingRequest()任何需求了! 記得將developer發佈頁面裡的公開金鑰放進Security.java裡的base64EncodedPublicKey變數中