我正在尝试在应用结算中删除广告。
我有这段代码:
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
// Have we been disposed of in the meantime? If so, quit.
if (mAbHelper == null)
return;
// Is it a failure?
if (result.isFailure()) {
return;
}
// Do we have the premium upgrade?
Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
mIsPremium = premiumPurchase != null;
updateInterface();
}
};
/**
* Updates interface
*/
private void updateInterface() {
Button saveButton = (Button) findViewById(R.id.btUpgrade);
Button btUpgrade = (Button) findViewById(R.id.btUpgrade);
if (mIsPremium) {
final EditText edittext = (EditText) findViewById(R.id.editTextAntiAdsKey);
saveButton = (Button) findViewById(R.id.buttonSaveAntiAdsKey);
if (edittext != null && edittext.getText() != null) {
Constants.ANTI_ADS_VALUE = edittext.getText().toString().trim();
} else {
Constants.ANTI_ADS_VALUE = "ERROR";
}
if ("".equals(Constants.ANTI_ADS_VALUE)
|| Constants.ANTI_ADS_VALUE.equals(this
.getString(R.string.app_name))) {
Constants.ANTI_ADS_VALUE = "";
Toast.makeText(MainActivity.this,
getResources().getString(R.string.app_name),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
MainActivity.this,
getResources().getString(R.string.app_name) + " ("
+ edittext.getText() + ")", Toast.LENGTH_LONG)
.show();
}
SharedPreferences settings = getSharedPreferences(
Constants.PREFERENCES_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(Constants.ANTI_ADS_KEY, Constants.ANTI_ADS_VALUE);
editor.commit();
editor.clear();
} else {
btUpgrade.setEnabled(true);
}
}
问题是我必须按一个按钮才能使此代码正常工作。 我希望此代码适用于活动开始。
我是新手,所以我想知道是否可能?
由于
答案 0 :(得分:0)
在初始化编辑框后直接输入代码。但是如果你想按下按钮并仍然想在启动时执行那么你可以写``/ / p>
saveButton.performClick()
保存按钮OnClickListener代码块之后的。也将它放在活动的onResume()方法块中,这样即使你的活动从暂停开始,它也会被执行。
答案 1 :(得分:0)
如果您不想按下按钮&想让这个代码在Activity上启动,删除按钮的onClickListener
。使用此:
final EditText edittext = (EditText) findViewById(R.id.editTextAntiAdsKey);
saveButton = (Button) findViewById(R.id.buttonSaveAntiAdsKey);
if (edittext != null && edittext.getText() != null) {
Constants.ANTI_ADS_VALUE = edittext.getText().toString()
.trim();
} else {
Constants.ANTI_ADS_VALUE = "ERROR";
}
if ("".equals(Constants.ANTI_ADS_VALUE)
|| Constants.ANTI_ADS_VALUE.equals(activity
.getString(R.string.app_name))) {
Constants.ANTI_ADS_VALUE = "";
Toast.makeText(PromoCode.this,
getResources().getString(R.string.app_name),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
PromoCode.this,
getResources().getString(R.string.app_name) + " ("
+ edittext.getText() + ")",
Toast.LENGTH_LONG).show();
}
SharedPreferences settings = getSharedPreferences(
Constants.PREFERENCES_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(Constants.ANTI_ADS_KEY,
Constants.ANTI_ADS_VALUE);
editor.commit();
editor.clear();