2011-12-06 15 views
15

के लिए उच्च स्कोर सहेजने की आवश्यकता है यह बहुत आसान है, मुझे बस इतना करना है कि गेम के लिए उच्च स्कोर (एक पूर्णांक) बचाएं। मुझे लगता है कि ऐसा करने का सबसे आसान तरीका यह है कि इसे टेक्स्ट फ़ाइल में स्टोर करना होगा, लेकिन मुझे वास्तव में यह नहीं पता कि यह करने के बारे में कैसे जाना है।एंड्रॉइड गेम

उत्तर

39

आप सभी की जरूरत एक पूर्णांक स्टोर करने के लिए है, तो SharedPreferences सबसे अच्छा होगा कि आप का उपयोग करने के लिए होगा:

//setting preferences 
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
Editor editor = prefs.edit(); 
editor.putInt("key", score); 
editor.commit(); 

एक वरीयता प्राप्त करने के लिए:

//getting preferences 
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
int score = prefs.getInt("key", 0); //0 is the default value 
बेशक

के लिए कुंजी के साथ "key" की जगह आपकी प्राथमिकता के लिए कुंजी के साथ आपका उच्च स्कोर मान और "myPrefsKey" (ये कुछ भी हो सकता है। उन्हें पहचानने योग्य और अद्वितीय कुछ सेट करना अच्छा है)।

+1

बस पुष्टि करने के लिए, क्या यह डेटा स्टोर करेगा अगली बार जब मैं गेम चलाता हूं तो मैं इसे प्राप्त कर सकता हूं? – Zizo47

+2

हां, यह डेटा आपके ऐप के रनों के बीच बनी हुई है। बस प्रतिबद्ध() को कॉल करना सुनिश्चित करें; उनके लिए संपादक पर सहेजा जा सकता है! – dymmeh

+0

बहुत बहुत धन्यवाद, मैं इसे अब कोशिश करूँगा – Zizo47

2

उपयोग shared preferences:

public class Calc extends Activity { 
    public static final String PREFS_NAME = "MyPrefsFile"; 

    @Override 
    protected void onCreate(Bundle state){ 
     super.onCreate(state); 
     . . . 

     // Restore preferences 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
     boolean silent = settings.getBoolean("silentMode", false); 
     setSilent(silent); 
    } 

    @Override 
    protected void onStop(){ 
     super.onStop(); 

     // We need an Editor object to make preference changes. 
     // All objects are from android.context.Context 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putBoolean("silentMode", mSilentMode); 

     // Commit the edits! 
     editor.commit(); 
    } 
} 

यह इतनी बातें स्टोर करने के लिए सबसे आसान तरीका है।

2

मुझे लगता है कि this link यह में मदद मिलेगी:

The SharedPreferences class provides a general framework that allows you to save and 
retrieve persistent key-value pairs of primitive data types. You can use 
SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. 
This data will persist across user sessions (even if your application is killed). 

User Preferences 

Shared preferences are not strictly for saving "user preferences," such as what ringtone 
a user has chosen. If you're interested in creating user preferences for your 
application, see PreferenceActivity, which provides an Activity framework for you to 
create user preferences, which will be automatically persisted (using shared preferences). 

To get a SharedPreferences object for your application, use one of two methods: 

    getSharedPreferences() - Use this if you need multiple preferences files identified 
by name, which you specify with the first parameter. 
    getPreferences() - Use this if you need only one preferences file for your Activity. 
Because this will be the only preferences file for your Activity, you don't supply a name. 

To write values: 

    Call edit() to get a SharedPreferences.Editor. 
    Add values with methods such as putBoolean() and putString(). 
    Commit the new values with commit() 

To read values, use SharedPreferences methods such as getBoolean() and getString(). 

के रूप में मैं देख रहा हूँ, आप उच्च स्कोर बचाने के लिए सबसे अच्छा तरीका है SharedPreferences है।

0
public class HighScores extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_high); 

     //get text view 
     TextView scoreView = (TextView)findViewById(R.id.high_scores_list); 
     //get shared prefs 
     SharedPreferences scorePrefs = getSharedPreferences(PlayGame.GAME_PREFS, 0); 
     //get scores 
     String[] savedScores = scorePrefs.getString("highScores", "").split("\\|"); 
     //build string 
     StringBuilder scoreBuild = new StringBuilder(""); 
     for(String score : savedScores){ 
      scoreBuild.append(score+"\n"); 
     } 
     //display scores 
     scoreView.setText(scoreBuild.toString()); 
    } 

} 
+2

इस कोड ब्लॉक वास्तव में क्या करता है यह बताने के लिए टेक्स्ट की कुछ पंक्तियों को जोड़ने की आवश्यकता है। – Sufian

संबंधित मुद्दे