public class Dungeons extends Activity implements OnClickListener { private static final String TAG = "Dungeons"; public static boolean chk = false; private static final String LOG_TEXT_KEY = "DUNGEONS_LOG_TEXT"; private static final String DB_INITIALIZED = "db_initialized"; private DungeonsPurchaseObserver mDungeonsPurchaseObserver; private Handler mHandler; private BillingService mBillingService; private Button mBuyButton; private Cursor mOwnedItemsCursor; private static final int DIALOG_CANNOT_CONNECT_ID = 1; private static final int DIALOG_BILLING_NOT_SUPPORTED_ID = 2;
public static String base64EncodedPublicKey = "";
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
mHandler = new Handler(); mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler); mBillingService = new BillingService(); mBillingService.setContext(this);
setupWidgets();
// Check if billing is supported. ResponseHandler.register(mDungeonsPurchaseObserver); if (!mBillingService.checkBillingSupported()) { showDialog(DIALOG_CANNOT_CONNECT_ID); }
}
public void onClick(View v) { if (v == mBuyButton) { mBillingService.requestPurchase("horse_001", null); } }
private class DungeonsPurchaseObserver extends PurchaseObserver { public DungeonsPurchaseObserver(Handler handler) { super(Dungeons.this, handler); }
@Override public void onBillingSupported(boolean supported) { if (Consts.DEBUG) { Log.i(TAG, "supported: " + supported); } if (supported) { restoreDatabase(); mBuyButton.setEnabled(true); } else { showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID); } }
@Override public void onPurchaseStateChange(PurchaseState purchaseState, String itemId, int quantity, long purchaseTime, String developerPayload) { if (Consts.DEBUG) { Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState); } mOwnedItemsCursor.requery(); }
if (Consts.DEBUG) { Log.i(TAG, "purchase failed"); } } }
@Override public void onRestoreTransactionsResponse(RestoreTransactions request, ResponseCode responseCode) { if (responseCode == ResponseCode.RESULT_OK) { if (Consts.DEBUG) { Log.d(TAG, "completed RestoreTransactions request"); } // Update the shared preferences so that we don't perform // a RestoreTransactions again. SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor edit = prefs.edit(); edit.putBoolean(DB_INITIALIZED, true); edit.commit(); } else { if (Consts.DEBUG) { Log.d(TAG, "RestoreTransactions error: " + responseCode); } } } }
/** * Called when this activity becomes visible. */ @Override protected void onStart() { super.onStart(); ResponseHandler.register(mDungeonsPurchaseObserver); }
/** * Called when this activity is no longer visible. */ @Override protected void onStop() { super.onStop(); ResponseHandler.unregister(mDungeonsPurchaseObserver); }
/** * Save the context of the log so simple things like rotation will not * result in the log being cleared. */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); }
/** * Restore the contents of the log if it has previously been saved. */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); }
package dungeons;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.inapptoy.R;
import dungeons.BillingService.RequestPurchase;
import dungeons.BillingService.RestoreTransactions;
import dungeons.Consts.PurchaseState;
import dungeons.Consts.ResponseCode;
public class Dungeons extends Activity implements OnClickListener {
private static final String TAG = "Dungeons";
public static boolean chk = false;
private static final String LOG_TEXT_KEY = "DUNGEONS_LOG_TEXT";
private static final String DB_INITIALIZED = "db_initialized";
private DungeonsPurchaseObserver mDungeonsPurchaseObserver;
private Handler mHandler;
private BillingService mBillingService;
private Button mBuyButton;
private Cursor mOwnedItemsCursor;
private static final int DIALOG_CANNOT_CONNECT_ID = 1;
private static final int DIALOG_BILLING_NOT_SUPPORTED_ID = 2;
public static String base64EncodedPublicKey = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
mDungeonsPurchaseObserver = new DungeonsPurchaseObserver(mHandler);
mBillingService = new BillingService();
mBillingService.setContext(this);
setupWidgets();
// Check if billing is supported.
ResponseHandler.register(mDungeonsPurchaseObserver);
if (!mBillingService.checkBillingSupported()) {
showDialog(DIALOG_CANNOT_CONNECT_ID);
}
}
public void onClick(View v) {
if (v == mBuyButton) {
mBillingService.requestPurchase("horse_001", null);
}
}
private class DungeonsPurchaseObserver extends PurchaseObserver {
public DungeonsPurchaseObserver(Handler handler) {
super(Dungeons.this, handler);
}
@Override
public void onBillingSupported(boolean supported) {
if (Consts.DEBUG) {
Log.i(TAG, "supported: " + supported);
}
if (supported) {
restoreDatabase();
mBuyButton.setEnabled(true);
} else {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
@Override
public void onPurchaseStateChange(PurchaseState purchaseState,
String itemId, int quantity, long purchaseTime,
String developerPayload) {
if (Consts.DEBUG) {
Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " "
+ purchaseState);
}
mOwnedItemsCursor.requery();
}
public void onRequestPurchaseResponse(RequestPurchase request,
ResponseCode responseCode) {
if (Consts.DEBUG) {
Log.d(TAG, request.mProductId + ": " + responseCode);
}
if (responseCode == ResponseCode.RESULT_OK) {
// request.mProductId
Log.e("1", "Id: " + request.mProductId + " request: "
+ responseCode);
if (Consts.DEBUG) {
Log.i(TAG, "purchase was successfully sent to server");
}
} else if (responseCode == ResponseCode.RESULT_USER_CANCELED) {
if (Consts.DEBUG) {
Log.i(TAG, "user canceled purchase");
}
} else {
if (Consts.DEBUG) {
Log.i(TAG, "purchase failed");
}
}
}
@Override
public void onRestoreTransactionsResponse(RestoreTransactions request,
ResponseCode responseCode) {
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.d(TAG, "completed RestoreTransactions request");
}
// Update the shared preferences so that we don't perform
// a RestoreTransactions again.
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(DB_INITIALIZED, true);
edit.commit();
} else {
if (Consts.DEBUG) {
Log.d(TAG, "RestoreTransactions error: " + responseCode);
}
}
}
}
/**
* Called when this activity becomes visible.
*/
@Override
protected void onStart() {
super.onStart();
ResponseHandler.register(mDungeonsPurchaseObserver);
}
/**
* Called when this activity is no longer visible.
*/
@Override
protected void onStop() {
super.onStop();
ResponseHandler.unregister(mDungeonsPurchaseObserver);
}
@Override
protected void onDestroy() {
super.onDestroy();
mBillingService.unbind();
}
/**
* Save the context of the log so simple things like rotation will not
* result in the log being cleared.
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
/**
* Restore the contents of the log if it has previously been saved.
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_CANNOT_CONNECT_ID:
return createDialog(R.string.cannot_connect_title,
R.string.cannot_connect_message);
case DIALOG_BILLING_NOT_SUPPORTED_ID:
return createDialog(R.string.billing_not_supported_title,
R.string.billing_not_supported_message);
default:
return null;
}
}
private Dialog createDialog(int titleId, int messageId) {
String helpUrl = replaceLanguageAndRegion(getString(R.string.help_url));
if (Consts.DEBUG) {
Log.i(TAG, helpUrl);
}
final Uri helpUri = Uri.parse(helpUrl);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(titleId)
.setIcon(android.R.drawable.stat_sys_warning)
.setMessage(messageId)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(R.string.learn_more,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Intent intent = new Intent(Intent.ACTION_VIEW,
helpUri);
startActivity(intent);
}
});
return builder.create();
}
private String replaceLanguageAndRegion(String str) {
// Substitute language and or region if present in string
if (str.contains("%lang%") || str.contains("%region%")) {
Locale locale = Locale.getDefault();
str = str.replace("%lang%", locale.getLanguage().toLowerCase());
str = str.replace("%region%", locale.getCountry().toLowerCase());
}
return str;
}
/**
* Sets up the UI.
*/
private void setupWidgets() {
mBuyButton = (Button) findViewById(R.id.buy_button);
mBuyButton.setEnabled(false);
mBuyButton.setOnClickListener(this);
}
private void restoreDatabase() {
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
boolean initialized = prefs.getBoolean(DB_INITIALIZED, false);
if (!initialized) {
mBillingService.restoreTransactions();
}
}
혹시나 참조 하실수 있게 코드 자료 입니다.
좋은 조언 주시면 정말 감사드립니다.