2014-09-28 2 views
1

У меня есть приложение для Android, в котором вы можете просто ввести promocode, чтобы получить полную версию. Но как сделать в других действиях проверить, введено это или нет? Это мой код:Как проверить, введен ли пользователь promocode?

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; 

это код для второго вида деятельности. Я думаю, что он просто не обновил 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(); 
    } 
} 

}

ответ

1

Использование 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; 

Тогда в другой деятельности, получить ваши предпочтения:

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 
Смежные вопросы