miércoles, 2 de enero de 2013
viernes, 16 de noviembre de 2012
Data Storage - Using Shared Preferences
Using Shared Preferences
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 aSharedPreferences.Editor. - Add values with methods such as
putBoolean()andputString(). - Commit the new values with
commit()
Here is an example that saves a preference for silent keypress mode in a calculator:
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, 0); 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, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Commit the edits! editor.commit(); } }
source: Android.developer
sábado, 10 de noviembre de 2012
How to pass data from activity to service
Pass data from activity to service
In activity:
1
2
3
4
5
6
7
8
| Intent myIntent = new Intent(MainActivity.this, MyService.class); Bundle bundle = new Bundle();bundle.putCharSequence("extraData", data);myIntent.putExtras(bundle); pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0); |
Basically, it's similar to that in "Pass data between activity".
In service side, there are no getIntent().getExtras() in Service class. We can handle it in onStart() call-back method, intent will be passed as parameter.
In service side, there are no getIntent().getExtras() in Service class. We can handle it in onStart() call-back method, intent will be passed as parameter.
1
2
3
4
5
6
7
8
9
10
| public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Bundle bundle = intent.getExtras(); data = (String) bundle.getCharSequence("extraData"); smsTextToSend = (String) bundle.getCharSequence("extraSmsText"); ...} |
source: Android Coding
Suscribirse a:
Entradas (Atom)
.jpg)
