15 Android 数据存储
来源:互联网 发布:js date gmt 编辑:程序博客网 时间:2024/06/13 16:39
package com.data.r;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class StroeDataActivity extends Activity { /** Called when the activity is first created. */private Button btn1,btn2;private static String FILE_NAME="hello"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); btn1.setOnClickListener(listern1); btn2.setOnClickListener(listern2); } OnClickListener listern1=new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubSharedPreferences prefernces=getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);Editor editor=prefernces.edit();editor.putString("name", "Tom");editor.putString("password", "abc123");editor.commit();}}; OnClickListener listern2=new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubSharedPreferences preferences=getSharedPreferences(FILE_NAME, Context.MODE_WORLD_READABLE);String str=preferences.getString("name", "abc123");Log.d(">>>>>>>>>>>>提示", str);Toast.makeText(StroeDataActivity.this, str+"是", Toast.LENGTH_LONG).show();}};}
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, seePreferenceActivity
, 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()
To read values, use SharedPreferences
methods such asgetBoolean()
andgetString()
.
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(); }}
- 15 Android 数据存储
- Android | Android数据存储
- Android数据存储--网络存储
- Android数据存储:File存储
- Android 数据存储--文件存储
- android 数据存储-文件存储
- Android数据存储----File 存储
- Android的数据存储
- Android数据存储
- Android数据存储
- Android 数据存储
- Android数据存储
- Android数据存储
- android中的数据存储
- Android 数据存储总结
- Android-数据存储
- Android 数据存储
- android数据存储
- LDD3 之Makefile文件的分析
- Linux 常用目录
- iOS开发之Objective-C与JavaScript交互操作
- 页面中哪些元素可以做为容器使用
- Chrome强制使用某种字体采用下面的方式
- 15 Android 数据存储
- android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
- 基于融合特征图像检索的优点
- eclipse4.2 版本插件装配
- Android 和 iOS 孰优孰劣:真实应用开发过程告诉你答案
- SAX解析XML
- jdbc的pl/sql的java调用存储过程
- 实战演示黑客如何利用SQL注入漏洞攻破一个WordPress网站
- 关于编程在线