我正在使用in-app purchase
。
我在特定的Activity
购物,然后存储在sharedpreferences
到目前为止没有问题。现在我想在另一个类中使用这些sharedpreferences`并检查是否付款是否要完成特定任务。
我的代码:
`@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_premium);
btnBuy = (Button) findViewById(R.id.btnBuy);
btnBuy.setEnabled(false);
btnBuy.setOnClickListener(this);
mBillingProcessor = new BillingProcessor(
this,
BAZAAR_RSA_KEY,
NIVAD_APPLICATION_ID,
NIVAD_APPLICATION_SECRET,
MarketName.CAFE_BAZAAR,
this
);
}
@Override
public void onClick(View v) {
mBillingProcessor.purchase(this, FULL_VERSION_SKU);
}
@Override
public void onBillingInitialized() {
btnBuy.setEnabled(true);
btnBuy.setBackgroundColor(getResources().getColor( R.color.colorBoy));
btnBuy.setText(R.string.Buy);
}
@Override
public void onProductPurchased(String sku, TransactionDetails transactionDetails) {
if (FULL_VERSION_SKU.equals(sku)) {
PreferenceManager.getDefaultSharedPreferences(PremiumActivity.this)
.edit().putBoolean("is_full_version", true).apply();
Toast.makeText(PremiumActivity.this, R.string.Lol, Toast.LENGTH_LONG).show();
}
}
@Override
public void onPurchaseHistoryRestored() {
if(mBillingProcessor.isPurchased(FULL_VERSION_SKU)){
PreferenceManager.getDefaultSharedPreferences(PremiumActivity.this)
.edit().putBoolean("is_full_version", true).apply();
} else {
PreferenceManager.getDefaultSharedPreferences(PremiumActivity.this)
.edit().putBoolean("is_full_version", false).apply();
}
}
@Override
public void onBillingError(int errorCode, Throwable throwable){
if (errorCode == 205){
Toast.makeText(PremiumActivity.this, R.string.nivad_protects_this_application, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDestroy() {
if (mBillingProcessor != null) {
mBillingProcessor.release();
}
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mBillingProcessor.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
} `
答案 0 :(得分:1)
getDefaultSharedPreferences
将为仅限内部活动创建共享首选项文件。 你应该得到SharedPreferences
这样的实例:
SharedPreferences preferences = context.getSharedPreferences("NAME",MODE_PRIVATE);
2.在另一项活动中,只需像上一次活动中所做的那样获取数据
SharedPreferences preferences = getSharedPreferences("NAME", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.getBoolean("is_full_version", false);