1

我有 2 个活动 1.Register.java 2.SecureXActivity.java 在注册活动中,我正在使用共享首选项注册我的应用程序。我使用共享首选项仅显示一次注册活动。现在我的要求是从注册活动中获取用户名,该用户名在注册时仅显示一次并将其保存在 SecureXActivity 活动中,注册后只有此活动对用户可见。所以请在这方面帮助我?我是安卓新手。

我的代码如下

注册.java

public class Register extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
public static final String USER_NAME = "USER";

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    /*
     * Check if we successfully logged in before.
     * If we did, redirect to home page
     */
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    if (settings.getString("logged", "").toString().equals("logged")) 
    {
        Intent intent = new Intent(Register.this, SecureXActivity.class);
        startActivity(intent);
    }

    Button b = (Button) findViewById(R.id.btnRegister);
    b.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) {
            EditText username = (EditText) findViewById(R.id.reg_employeeid);
            EditText password = (EditText) findViewById(R.id.reg_password);
            String welcm ="Welcome";
            String space = " ";
            EditText firstname = (EditText)findViewById(R.id.reg_firstname);
            EditText lastname = (EditText)findViewById(R.id.reg_lastname);
            String getfirstname = firstname.getText().toString();
            String getlasttname = lastname.getText().toString();
            String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname);





            if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) 
            {
                if(username.getText().toString().equals("test") && password.getText().toString().equals("test")) 
                {
                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("logged", "logged");
                    editor.commit();    
                    Intent intent = new Intent(Register.this, SecureXActivity.class);
                    intent.putExtra("name", welcome);
                    startActivity(intent);
                }
            }
        }
    });
}
} 

SecureXActivity.java

public void onCreate(Bundle savedInstanceState)
{
    Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this));
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
        Bundle extras =getIntent().getExtras();

    if (extras != null)
    {
        String value = extras.getString("name");
        TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm);
        txtwelcm.setText(value);
    }
}

我想要从一个活动到另一个活动的值,并且应该将值保存到另一个活动。在我上面的代码中,我使用意图完成了此操作。但问题是值在第二个活动中只显示一次。当我按下返回按钮或主页键时,值不可见?如何做到这一点。

为响应@Picarus 添加:

问题是我使用相同的共享首选项只显示一次注册活动。但它没有在 SecureX 活动上显示名称。是否可以使用相同的共享偏好将值从注册活动传递到 Securex 活动

整个代码在下面再次给出 Register.java

    public class Register extends Activity 
    { 
public static final String PREFS_NAME = "LoginPrefs"; 


@Override 
public void onCreate(Bundle savedInstanceState)  
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.register); 

    /* 
     * Check if we successfully logged in before. 
     * If we did, redirect to home page 
     */ 
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
    if (settings.getString("logged", "").toString().equals("logged"))  
    { 
        Intent intent = new Intent(Register.this, SecureXActivity.class); 
        startActivity(intent); 
    } 

    Button b = (Button) findViewById(R.id.btnRegister); 
    b.setOnClickListener(new OnClickListener()  
    { 
        public void onClick(View v) { 
            EditText username = (EditText) findViewById(R.id.reg_employeeid); 
            EditText password = (EditText) findViewById(R.id.reg_password); 
            String welcm ="Welcome"; 
            String space = " "; 
            EditText firstname = (EditText)findViewById(R.id.reg_firstname); 
            EditText lastname = (EditText)findViewById(R.id.reg_lastname); 
            String getfirstname = firstname.getText().toString(); 
            String getlasttname = lastname.getText().toString(); 
            String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname); 





            if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 )  
            { 
                if(username.getText().toString().equals("test") && password.getText().toString().equals("test"))  
                { 
                    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
                    SharedPreferences.Editor editor = settings.edit(); 
                    editor.putString("name", welcome); 
                    editor.commit();     
                    Intent intent = new Intent(Register.this, SecureXActivity.class); 
                    //intent.putExtra("name", welcome); 
                    startActivity(intent); 
                } 
            } 
        } 
    }); 
} 
} 

SecureXActivity.java

public class SecureXActivity extends Activity 
{ 
public static final String PREFS_NAME = "LoginPrefs"; 

private ListView m_listview; 
Button btnSendlogs; 
Button btnMailadmin; 
Button btnContactSupport; 
Button btnSettings; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this)); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //Here we are setting the installation date of a application 
    ContextWrapper context = this; 
    PackageManager packageManager = context.getPackageManager(); 
    ApplicationInfo appInfo; 
    try 
    { 
        appInfo = packageManager.getApplicationInfo("com.simsys.securex", 0); 
        String sourceDir= appInfo.sourceDir; 
        long dateTimeSync= new File(sourceDir).lastModified(); 
        String DateTimeStamp=getDate(dateTimeSync, "dd/MM/yyyy hh:mm:ss"); 
        String dateTimeSync2="Installed_Date:"; 
        String dateTimeSync3=dateTimeSync2.concat(DateTimeStamp); 
        TextView txtCurrentTime= (TextView)findViewById(R.id.textViewdatetime); 
        txtCurrentTime.setText(dateTimeSync3); 
    }  
    catch (NameNotFoundException e)  
    { 
        e.printStackTrace(); 
    } 
    //Here We Welcome the user 

    //List View 
    m_listview = (ListView) findViewById(R.id.list_view); 
    ArrayList<String> listItems=new ArrayList<String>(); 
    listItems.add("CAMERA"); 
    listItems.add("RECORDER"); 
    ArrayAdapter<String> adapter = 
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems); 

    m_listview.setAdapter(adapter); 
    if(isBluetoothServiceRunning()) 
    { 
        listItems.add("BLUETOOTH"); 
    } 
    if(isGprsServiceRunning()) 
    { 
        listItems.add("GPRS"); 
    } 
    if(isWifiServiceRunning()) 
    { 
        listItems.add("WIFI"); 
    } 
    adapter.notifyDataSetChanged(); 
    //Call to Buttons Functionality 
    btnSendlogs = (Button) findViewById(R.id.button_sendlogs); 
    btnSendlogs.setOnClickListener(myhandler1); 
    btnMailadmin=(Button) findViewById(R.id.button_mailtoadmin); 
    btnMailadmin.setOnClickListener(MailadminHandler); 
    btnContactSupport = (Button) findViewById(R.id.button_contactsupport); 
    btnContactSupport.setOnClickListener(ContactSupportHandler); 

} 

//Navigation from Registration to Main page 
public boolean onCreateOptionsMenu(Menu menu)  
{ 
    MenuInflater Inflater = getMenuInflater(); 
    Inflater.inflate(R.menu.menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    if (item.getItemId() == R.id.logout) 
    { 
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 

        String data2=settings.getString("name", ""); 
        TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm); 
        txtwelcm.setText(data2); 
        SharedPreferences.Editor editor = settings.edit(); 
        editor.remove("name"); 
        editor.commit(); 
        finish(); 
    } 
    return super.onOptionsItemSelected(item); 
} 
4

2 回答 2

1

为了实现 sharedPreferences 使用以下代码

 Sharedpreferences myPref = this.getSharedPreferneces("your_file_name",MODE);
 SharedPreferences.Editor editor = myPref.edit();
 editor.putString("String_Name",value);
 editor.putInt("Int_Name","value");

从 SharedPreferences 中检索

   Sharedpreferences myPref = this.getSharedPreferneces("your_file_name",MODE);  
   String stringValue = myPref.getStirng("String_Name","DEFAULT_VALUE");
于 2012-08-01T11:57:21.753 回答
0

您可以从任何活动(SecureXActivity)中检索您的 SharedPreferences,就像在 Register.java 中一样

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
settings.getString("logged", "");

你有没有尝试过?你发现什么问题?

编辑:也许是因为您尝试从 onCreate.load 加载它。刷新或单击主页时不会调用此方法。您应该在您的应用程序中找到更合适的点来执行此操作。

于 2012-08-01T10:53:52.137 回答