另一个包中的共享首选项

时间:2012-05-14 05:19:27

标签: android

我是Android开发的新手。有没有办法从另一个包扩展类?我试图从另一个包获取共享首选项,虽然我能够访问同一个包中的类之间的共享首选项。但是,在不同的包中,我不知道我应该怎么做editer.commit()或编辑另一个包中的共享首选项。请帮忙。

2 个答案:

答案 0 :(得分:0)

I put example for the sharing data between two Activities using shared preference.
Using this code u get idea of shared preference how it's work.
you can also be shared data between two activites which are in different packages in the same application.
PracSharePrefActivity Class
package com.sharedpref;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PracSharePrefActivity extends Activity {

    private SharedPreferences settings;
    private String name,subject;
    private Button btnSave;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        settings = PreferenceManager.getDefaultSharedPreferences(this);

        name = settings.getString("Name","");
        subject = settings.getString("Subject", "");

        Log.e("Name0==",name);
        Log.e("Subject0==",subject);


        String yourName="Deval";
        String yourSubject="Android";

        SharedPreferences.Editor editor = settings.edit();

        editor.putString("Name",yourName);
        editor.putString("Subject", yourSubject);

        editor.commit();

        Log.e("Name 1==",settings.getString("Name", ""));
        Log.e("Subject 1==",settings.getString("Subject", ""));       


        btnSave=(Button)findViewById(R.id.btnsave);
        btnSave.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i=new Intent(PracSharePrefActivity.this,GetSharedPref.class);
                startActivity(i);
            }
        });

    }



}
"GetSharedPref" Class
package com.sharedpref;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

public class GetSharedPref extends Activity {
    private SharedPreferences preferences;
    private String name,subject;
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.pref);          

            preferences = PreferenceManager.getDefaultSharedPreferences(this);
            name = preferences.getString("Name", "");
            subject=preferences.getString("Subject", "");

            Log.e("Name:  ",preferences.getString("Name", ""));
            Log.e("Subject:  ",preferences.getString("Subject",""));

            preferences = PreferenceManager.getDefaultSharedPreferences(this);    
     }
}

答案 1 :(得分:-2)

试试这段代码

将字符串放入共享首选项

    SharedPreferences USERNAME_and_PASSWORD;
            SharedPreferences.Editor editor;

USERNAME_and_PASSWORD = getSharedPreferences(Shared_preference_key,
                MODE_PRIVATE);
        editor = USERNAME_and_PASSWORD.edit();
                editor.putString(user_name_key, userName);
                editor.commit();

从共享首选项中获取字符串

    SharedPreferences USERNAME_and_PASSWORD;
 USERNAME_and_PASSWORD = getSharedPreferences(Shared_preference_key,
                    MODE_PRIVATE);
     USERNAME_and_PASSWORD = getSharedPreferences(Shared_preference_key,
                        MODE_PRIVATE);
    String Return_Password = USERNAME_and_PASSWORD.getString(pass_word_key,
                    "Guest");