我仍然无法围绕这个应用内结算。我的想法是,在高级升级之前,我无法在EditText中输入任何内容。没有错误(应用程序运行正常),但我必须在某个地方犯了一个逻辑错误,因为在购买高级版之后,变量mPremium应该等于1(如果没有溢价,mPremium == 0)。任何帮助将不胜感激。
public class MainActivity extends Activity {
private static final String TAG = "com.dt.tryout4";
EditText txtIncome;
KeyListener mKeyListener;
IabHelper mHelper;
int mPremium;
static final String ITEM_SKU = "android.test.purchased";
Button upgrade;
TextView txtTest;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp = getPreferences(MODE_PRIVATE);
mPremium = sp.getInt("premium", 0);
txtTest = (TextView) findViewById(R.id.textTest);
txtTest.setText(Integer.toString(mPremium));
txtIncome = (EditText) findViewById(R.id.textIncome);
mKeyListener = txtIncome.getKeyListener();
if (mPremium==0)
{
txtIncome.setKeyListener(null);
}
else if(mPremium==1)
{
txtIncome.setKeyListener(mKeyListener);
}
upgrade = (Button) findViewById(R.id.btnUpgrade);
String base64EncodedPublicKey =
"<your license key here>";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed: " +
result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
}
}
});
}
public void premiumUpgrade (View view)
{
try {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001, mPurchaseFinishedListener, "mypurchasetoken");
} catch (IabHelper.IabAsyncInProgressException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase)
{
if (result.isFailure()) {
return;
}
else if (purchase.getSku().equals(ITEM_SKU)) {
upgrade.setEnabled(false);
mPremium=1;
saveData();
}
}
};
public void saveData()
{
SharedPreferences.Editor spe = getPreferences(MODE_PRIVATE).edit();
spe.putInt("premium", mPremium);
spe.apply();
}
public static boolean verifyPurchase(String base64PublicKey,
String signedData, String signature) {
if (TextUtils.isEmpty(signedData) ||
TextUtils.isEmpty(base64PublicKey) ||
TextUtils.isEmpty(signature)) {
Log.e(TAG, "Purchase verification failed: missing data.");
if (BuildConfig.DEBUG) {
return true;
}
return false;
}
PublicKey key = Security.generatePublicKey(base64PublicKey);
return Security.verify(key, signedData, signature);
}
答案 0 :(得分:1)
这是我更新的答案。显然,您必须延迟Runnable r,以便您的手机/模拟器可以适当地处理购买。因此,如果您的购买成功,您应该添加此代码:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
txtIncome.setEnabled(true);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("isPurchase",true);
editor.commit();
}
}, 4000);
然后,您可以在onCreate中添加:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isPurchase = preferences.getBoolean("isPurchase", false);
if(isPurchase==true)
{
txtIncome.setEnabled(true);
}
else
{
txtIncome.setEnabled(false);
}