Android SharedPreferences本地缓存
来源:互联网 发布:怎么选基金 知乎 编辑:程序博客网 时间:2024/05/16 00:54
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取数据" /> <Button android:id="@+id/write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写入数据" /> </LinearLayout>
SharedPreferencesTest.java
package com.example.sharedpreferences;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class SharedPreferencesTest extends Activity {SharedPreferences preferences;SharedPreferences.Editor editor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);preferences = getSharedPreferences("crazyit", MODE_WORLD_READABLE);editor = preferences.edit();Button read = (Button)findViewById(R.id.read);Button write = (Button)findViewById(R.id.write);read.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString time = preferences.getString("time", null);int randNum = preferences.getInt("random", 0);String result = time == null? "您暂时还未写入数据":"写入时间为:"+ time + "\n上次生成的随机数为:" + randNum;Toast.makeText(SharedPreferencesTest.this, result, 5000).show();}});write.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubSimpleDateFormat time = new SimpleDateFormat("yyyy年MM月dd日 "+ "hh:mm:ss a", Locale.ENGLISH);editor.putString("time", time.format(new Date()));editor.putInt("random", (int)(Math.random() * 100));editor.commit();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.shared_preferences_test, menu);return true;}}
0 0
- Android SharedPreferences本地缓存
- Android 本地缓存开源库 Reservoir 替代SharedPreferences
- 用Gson和SharedPreferences轻松构建android本地数据缓存
- Gson和SharedPreferences轻松构建android本地数据缓存
- 用Gson和SharedPreferences轻松构建android本地数据缓存
- Android中实现本地缓存,存储(SharedPreferences存储)
- Android SharedPreferences本地储存
- Android本地存储方案 SharedPreferences
- 用Gson和SharedPreferences构建本地数据缓存
- Android图片本地缓存
- Android -- ImageLoader本地缓存
- Android -- ImageLoader本地缓存
- Android -- ImageLoader本地缓存
- Android -- ImageLoader本地缓存
- Android -- ImageLoader本地缓存
- Android 之本地缓存
- android本地缓存ACache
- Android ImageLoader 本地缓存
- RevitAPI: 如何实现族编辑器里的连接几何图形
- ArcEngine动态追踪
- 开发phonegap应用问题汇总
- 倒计时效果demo
- Android内存分析和调优(上)
- Android SharedPreferences本地缓存
- Awk编程模型及元字符集
- 第十三周 项目4.1 数组的排序
- ContentProvider 一个error问题
- Head First HTML与CSS 读书笔记(一)
- tar.xz文件一步解压
- HTTPS WinHttpSendRequest的时候报错
- Android内存分析和调优(中)
- c++ 重载 new delete