2013-12-15 38 views
1

嗨我想寫一個代碼持久性/共享首選項。我成功地將數據保存在持久性存儲中。但每次啓動我的應用程序時,都會將我帶到存儲活動。我有兩項活動主要活動和第二活動。在主要活動中,我保存了數據,第二個活動顯示了存儲的數據。我想要的是當數據第一次被存儲時,它應該直接轉到第二個活動。我想實現像watsapp註冊過程這樣的事情,您首次輸入您的號碼和PIN碼,直到您卸載應用程序或手動清除數據爲止,它從不要求它。我怎麼能這樣做。以下是我嘗試過的代碼。堅持/共享存儲在Android

public class MainActivity extends Activity { 

    public static final String PREFS_NAME = "MSISDN and PIN"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     int id = settings.getInt("PREFS_NAME", 0); 
     if (id > 0) { 
      Intent second = new Intent(getApplicationContext(), seond.class); 
      startActivity(second); 
     } 
     final EditText name = (EditText) findViewById(R.id.editText1); 
     final EditText email = (EditText) findViewById(R.id.editText2); 
     Button btn = (Button) findViewById(R.id.btnsaveprefs); 
     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       SharedPreferences.Editor editor = settings.edit(); 
       editor.putString("MSISDN", name.getText().toString()); 
       editor.putString("PIN", email.getText().toString()); 
       editor.commit(); 
       Intent second = new Intent(getApplicationContext(), seond.class); 
       startActivity(second); 
      } 
     }); 
    } 
} 

和第2活動我

public class seond extends Activity { 

    public static final String PREFS_NAME = "MSISDN and PIN"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second); 
     TextView tvname = (TextView) findViewById(R.id.uname); 
     TextView tvemail = (TextView) findViewById(R.id.uemail); 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     tvname.setText(settings.getString("MSISDN", "Unknown")); 
     tvemail.setText(settings.getString("PIN", "Email default")); 
    } 
} 
+0

你在哪裏設置名稱前綴。您正在檢查,如果條件 –

+0

@nitesh戈爾檢查編輯問題 –

+0

你在哪裏設置PREFS_NAME? –

回答

0

我認爲你的問題奠定了這裏:

int id = settings.getInt("PREFS_NAME", 0); 

你永遠不取的「PREFS_NAME」的值,然後檢查它是否是> 0但你存儲密鑰PREFS_NAME的值。我想你想在這裏使用PINMSISDN

+0

不確定是什麼錯誤?我正在嘗試但無法解決它。 –

0

這是正確的方式做到這一點:

public class MainActivity extends Activity { 

    public static final String PREFS_NAME = "MSISDN and PIN"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     int id = settings.getInt("PREFS_NAME", 0); 
     if (id > 0) { 
      Intent second = new Intent(getApplicationContext(), Second.class); 
      startActivity(second); 
     } 
     final EditText name = (EditText) findViewById(R.id.editText1); 
     final EditText email = (EditText) findViewById(R.id.editText2); 
     Button btn = (Button) findViewById(R.id.btnsaveprefs); 
     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       SharedPreferences.Editor editor = settings.edit(); 
       editor.putString("MSISDN", name.getText().toString()); 
       editor.putString("PIN", email.getText().toString()); 
       editors.putInt("PREFS_NAME", 1); // id = 1 
       editor.commit(); 
       Intent second=new Intent(getApplicationContext(), Second.class); 
       startActivity(second); 
      } 
     }); 
    } 
} 

public class Second extends Activity { // classes begin with CAPITAL 

    public static final String PREFS_NAME = "MSISDN and PIN"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second); 
     TextView tvname = (TextView) findViewById(R.id.uname); 
     TextView tvemail = (TextView) findViewById(R.id.uemail); 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     tvname.setText(settings.getString("MSISDN", "Unknown")); 
     tvemail.setText(settings.getString("PIN", "Email default")); 
    } 
} 

通知您沒有設置ID在其他的答案說。你的編碼風格很糟糕:永遠不要用小寫字母來啓動類名,爲什麼id是int?你用它作爲布爾值。爲什麼要在名爲PIN的偏好設置中保存一個電子郵件,並在名稱中設置MSISDN