<p>import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.vending.billing.IInAppBillingService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class InAppTestActivity extends Activity {
IInAppBillingService mService;
IabHelper mHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
bindService(
new
Intent(
"com.android.vending.billing.InAppBillingService.BIND"
),
mServiceConn, Context.BIND_AUTO_CREATE);
String base64EncodiedPushedkey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuCphx6V1V9XXqe"
+
"XGgPMHgEtq64/199fDj0n46H2Gw7Qje+WXJKboCTvydomWUhpCxSknklv/7XhBC4/UI0VbWCVnWSz"
+
"+D2WYS+phunjtluImwAoT33/oV7Z4hempYKdPDaR24txT6GJgzT7ATJKffO6Wi4TgVILNcIQ1/LpXBALFvEFy"
+
"WiLEz5FB0hO4eemZtgnWTvUlsT7mdCo704l2PpfNtlpVwUixUVk21bHKiklVcR7AzI3yxFPa8SFSfNe4D7j5prBxSl"
+
"35wP5s5bRB0lsJ2DeaY8T9bdfZLqb8/ZObf68j8r7mVau9CIqDR/gpPjFUHam1RRPpiZPDxxxxxxx"
;
InAppInit_U(base64EncodiedPushedkey,
true
);
Button innapp1 = (Button) findViewById(R.id.innapp1);
innapp1.setOnClickListener(
new
View.OnClickListener() {
@Override
public void onClick(View v) {
InAppBuyItem_U(
"item01"
);
}
});
}
public void InAppInit_U(String strPublicKey, boolean bDebug) {
Log.d(
"myLog"
,
"Creating IAB helper."
+ bDebug);
mHelper =
new
IabHelper(
this
, strPublicKey);
if
(bDebug ==
true
) {
mHelper.enableDebugLogging(
true
,
"IAB"
);
}
mHelper.startSetup(
new
IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
boolean bInit = result.isSuccess();
Log.d(
"myLog"
,
"IAB Init "
+ bInit + result.getMessage());
if
(bInit ==
true
) {
Log.d(
"myLog"
,
"Querying inventory."
);
mHelper.queryInventoryAsync(mGotInventoryListener);
}
Toast.makeText(InAppTestActivity.
this
,
"InAppInit_U"
,
Toast.LENGTH_SHORT).show();
}
});
}
IabHelper.QueryInventoryFinishedListener mGotInventoryListener =
new
IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if
(result.isFailure()) {
Log.d(
"myLog"
,
"Failed to query inventory: "
+ result);
SendConsumeResult(
null
, result);
return
;
}
List</p><p></p><string> inappList = inventory
.getAllOwnedSkus(IabHelper.ITEM_TYPE_INAPP);
for
(String inappSku : inappList) {
Purchase purchase = inventory.getPurchase(inappSku);
Log.d(
"myLog"
,
"Consumeing ... "
+ inappSku);
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}
Log.d(
"myLog"
,
"Query inventory was successful."
);
}
};
public void InAppBuyItem_U(final String strItemId) {
runOnUiThread(
new
Runnable() {
@Override
public void run() {
String payload =
"user_id"
;
mHelper.launchPurchaseFlow(InAppTestActivity.
this
, strItemId,
1001, mPurchaseFinishedListener, payload);
Log.d(
"myLog"
,
"InAppBuyItem_U "
+ strItemId);
}
});
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener =
new
IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(
"myLog"
,
"Purchase finished: "
+ result +
", purchase: "
+ purchase);
if
(purchase !=
null
) {
if
(!verifyDeveloperPayload(purchase)) {
Log.d(
"myLog"
,
"Error purchasing. Authenticity verification failed."
);
}
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}
else
{
Toast.makeText(InAppTestActivity.
this
,
String.valueOf(result.getResponse()),
Toast.LENGTH_SHORT).show();
}
}
};
boolean verifyDeveloperPayload(Purchase p) {
String payload = p.getDeveloperPayload();
return
true
;
}
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new
IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase, IabResult result) {
Log.d(
"myLog"
,
"Consumption finished. Purchase: "
+ purchase
+
", result: "
+ result);
SendConsumeResult(purchase, result);
}
};
protected void SendConsumeResult(Purchase purchase, IabResult result) {
JSONObject jsonObj =
new
JSONObject();
try
{
jsonObj.put(
"Result"
, result.getResponse());
if
(purchase !=
null
) {
jsonObj.put(
"OrderId"
, purchase.getOrderId());
jsonObj.put(
"Sku"
, purchase.getSku());
jsonObj.put(
"purchaseData"
, purchase.getOriginalJson());
jsonObj.put(
"signature"
, purchase.getSignature());
Log.d(
"myLog"
,
"OrderId"
+ purchase.getOrderId());
Log.d(
"myLog"
,
"Sku"
+ purchase.getSku());
Log.d(
"myLog"
,
"purchaseData"
+ purchase.getOriginalJson());
Log.d(
"myLog"
,
"signature"
+ purchase.getSignature());
}
}
catch
(JSONException e) {
e.printStackTrace();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(
"myLog"
,
"onActivityResult("
+ requestCode +
","
+ resultCode
+
","
+ data);
if
(requestCode == 1001) {
if
(!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super
.onActivityResult(requestCode, resultCode, data);
}
else
{
Log.d(
"myLog"
,
"onActivityResult handled by IABUtil."
);
}
}
}
@Override
protected void onDestroy() {
super
.onDestroy();
if
(mServiceConn !=
null
) {
unbindService(mServiceConn);
}
}
ServiceConnection mServiceConn =
new
ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService =
null
;
}
};
}
</string>