android - 如何检查用户是否输入了促销代码?

标签 android in-app-purchase

我有一个 Android 应用程序,您只需在其中输入促销代码即可获得完整版本。但是如何在其他 Activity 中检查它是否已输入?这是我的代码:

AlertDialog.Builder alert = new AlertDialog.Builder(Browser.this);
                            alert.setTitle("Enter your promocode.");
                            final View input = LayoutInflater.from(Browser.this).inflate(R.layout.edittext, null);
                            alert.setView(input);
                            final EditText edit = (EditText) input.findViewById(R.id.edit);
                            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    if (edit.getText().toString().equalsIgnoreCase("TheSecretcode")) {
                                        isPremium = true;
                                    } else {
                                        isPremium = false;
                                    }
                                }
                            });
                            alert.show();
                            break;

这是第二个 Activity 的代码。我认为它只是没有更新 SharedPrefs。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_view_layout);
    Uri uri = getIntent() != null ? getIntent().getData() : null;
    final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
    final boolean isPremium = premiumSettings.getBoolean("isPremium", false);
    if(isPremium) {
        ImageView img = (ImageView) findViewById(R.id.imageView);
        img.setImageURI(uri);
    }
    else {
        AlertDialog.Builder builder = new AlertDialog.Builder(ImageViewer.this);
        builder.setTitle("Error");
        builder.setMessage("You are not premium user. Please enter the promocode or buy the full version");
        builder.setIcon(R.drawable.holo_dark_action_info);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                finish();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

最佳答案

使用SharedPreferences

AlertDialog.Builder alert = new AlertDialog.Builder(Browser.this);
alert.setTitle("Enter your promocode.");
final View input = LayoutInflater.from(Browser.this).inflate(R.layout.edittext, null);
alert.setView(input);
final EditText edit = (EditText) input.findViewById(R.id.edit);
final SharedPreferences premiumSettings= getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    SharedPreferencesEditor.Editor editor= premiumSettings.edit();
    if (edit.getText().toString() == "TheSecretCode") {
      isPremium = true;
    } else {
      isPremium = false;
    }
    editor.putBoolean("isPremium", isPremium);
    editor.commit();
  }
});
alert.show();
break;

然后在另一个 Activity 中,检索您的首选项:

final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE); // The name is the same as previously
final boolean isPremium = premiumSettings.getBoolean("isPremium", false); // false is default value

关于android - 如何检查用户是否输入了促销代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082730/

相关文章:

java - android 中的 'strace'

Android:子类库不会通过 onKeyDown 事件滚动

android - OpenIAB - 如何从库存中获取价格?

android - 如何从 Android Studio 中删除 .aar 文件

android - 使用 waitForDebugger 对 Android 进行 IntelliJ 调试

java - GSON 将特定字段的整数值动态转换为 boolean 值

swift - 无法使 IAP 在模拟器的沙箱 (SWIFT) 中工作

in-app-purchase - iOS 可更新订阅 expires_date 字段

ios - 苹果由于将用户重定向到应用之外而拒绝了iOS APP

ios - 对于应用程序购买中的 Cordova,沙箱 "confirm purchase"屏幕与商店屏幕相同吗?