2015-02-09 50 views
0

我試圖讓我的應用程序記住我的名字,當我寫它,然後告訴它,但不是告訴我的名字它告訴文件的名稱存儲名稱(「MySharedName」) 。 這是我的代碼:在SharedPreferences中寫入和讀取字符串

nameData = getSharedPreferences(namee, 0); 
     File l = new File("/data/data/cannon.gaming.appname/shared_prefs/MySharedName.xml"); 
     if(l.exists()){ 
     }else{ 

      SharedPreferences.Editor editor = nameData.edit(); 
      editor.putString("user", String.valueOf(namee)); 
      editor.commit(); 
     } 

if(String.valueOf(namee).equals("MySharedName")) 
         { 
          home.speak(abc, TextToSpeech.QUEUE_FLUSH, null); 
          home.speak(abcc, TextToSpeech.QUEUE_ADD, null); 
          home.speak(abccc, TextToSpeech.QUEUE_ADD, null); 
          home.speak(abcccc, TextToSpeech.QUEUE_ADD, null); 
          home.speak(name, TextToSpeech.QUEUE_ADD, null); 
         } 
         else 
         { 
          home.speak(hello, TextToSpeech.QUEUE_FLUSH, null); 
          home.speak(abc, TextToSpeech.QUEUE_ADD, null); 
          home.speak(abcc, TextToSpeech.QUEUE_ADD, null); 
          home.speak(abccc, TextToSpeech.QUEUE_ADD, null); 
          home.speak(abcccc, TextToSpeech.QUEUE_ADD, null); 
         } 

    if(String.valueOf(namee).equals("MySharedName")) 
       { 
        SharedPreferences.Editor editor = nameData.edit(); 
        editor.putString("user", String.valueOf(matches)); 
        editor.commit(); 
        home.speak(meet, TextToSpeech.QUEUE_FLUSH, null); 
        home.speak(now, TextToSpeech.QUEUE_ADD, null); 
       } 
+0

我可以看到更多的代碼? – jb15613 2015-02-09 20:37:58

回答

0

寫一首

SharedPreferences prefs = PreferenceManager 
.getDefaultSharedPreferences(YourActivity.this); 

SharedPreferences.Editor editor = prefs.edit(); 

editor.putString ("key", "value"); 
editor.commit(); 

要檢索偏好

String value = prefs.getString ("key", "defaultValue"); 
+0

是我的if line是否正確? – Cannon 2015-02-09 19:36:09