private Preferences(Context context) { // this.sharedPreferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE); this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); this.context = context; this.activity = (AppCompatActivity) context; this.permissionCallback = (PermissionCallback) context; // check the permissions on startup if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Log.d("Preferences", "Device is running on SDK +23 ! Checking permissions..."); int storagePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (storagePermission == PackageManager.PERMISSION_GRANTED) canAccessStorage = true; int internetPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.INTERNET); if (internetPermission == PackageManager.PERMISSION_GRANTED) canAccessInternet = true; int networkPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_NETWORK_STATE); if (networkPermission == PackageManager.PERMISSION_GRANTED) canAccessNetwork = true; } else { canAccessStorage = true; canAccessInternet = true; canAccessNetwork = true; } // register Paper data storage Paper.init(context); // get the connectivity manager if (canAccessNetwork) connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); this.sharedPreferences.registerOnSharedPreferenceChangeListener(this); }
@Override protected void onCreate(Bundle savedInstaceState) { super.onCreate(savedInstaceState); setContentView(R.layout.activity_main); // connect variables to interface item btnFalse = (Button) findViewById(R.id.btnFalse); btnTrue = (Button) findViewById(R.id.btnTrue); lblQuestion = (TextView) findViewById(R.id.lblQuestion); imgPicture = (ImageView) findViewById(R.id.imgPicture); // set Questionaire Text lblQuestion.setText("is my name ...."); // set image picture imgPicture.setImageResource(R.drawable.face); // set Score Show index = 0; score = 0; // onclick listeners btnFalse.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { determineButtonPress(false); } }); btnTrue.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { determineButtonPress(true); } }); generateQuestions(); setUpQuestions(); Paper.init(this); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_end_game_screen); // Retrieve values passed from previous activity Bundle extras = getIntent().getExtras(); final int finalScore = extras.getInt("playerScore"); final int questionCount = extras.getInt("questionCount"); final String difficultySetting = extras.getString("difficultySetting"); // Initialise PaperDB Paper.init(this); // Assign layout elements to objects (playerNameFld = (EditText) findViewById(R.id.playerNameFld)) .setInputType( InputType.TYPE_CLASS_TEXT | InputType .TYPE_TEXT_FLAG_CAP_WORDS); // Set input type to 'Text' and auto-capitalise endMessageLbl = (TextView) findViewById(R.id.endMessageLbl); finalScoreLbl = (TextView) findViewById(R.id.finalScoreLbl); submitScoreBtn = (Button) findViewById(R.id.submitScoreBtn); scoreScreenBtn = (Button) findViewById(R.id.scoreScreenBtn); menuScreenBtn = (Button) findViewById(R.id.menuScreenBtn); // Initialise final score counter finalScoreLbl.setText("Final Score: " + finalScore + " / " + questionCount); // Listen for button click submitScoreBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (playerNameFld.length() > 0) { // Submit values to PaperDB SubmitScore( playerNameFld.getText().toString(), finalScore, new Date().getTime(), difficultySetting); finish(); startActivity( new Intent(EndGameScreen.this, LeaderboardScreen.class).putExtra("btnID", 0)); } else { // Output lack of name error message Toast.makeText( EndGameScreen.this, "You must enter a name to submit a score", Toast.LENGTH_SHORT) .show(); } } }); // Listen for button click scoreScreenBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // Go to leaderboard screen startActivity( new Intent(EndGameScreen.this, LeaderboardScreen.class).putExtra("btnID", 1)); } }); // Listen for button click menuScreenBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // Close activity finish(); // Go to leaderboard screen startActivity(new Intent(EndGameScreen.this, MenuScreen.class)); } }); }
public static void setApplicationContext(Context context) { sContext = context.getApplicationContext(); Paper.init(sContext); // Realm.setDefaultConfiguration(new RealmConfiguration.Builder(sContext).build()); }