shareperference使用
来源:互联网 发布:数据监控系统 编辑:程序博客网 时间:2024/05/24 07:43
activity1:
package com.example.test;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Properties;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private SharedPreferences sharedPreferences, shareSession;private SharedPreferences.Editor editor, sEditor;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 读取shareperference文件// 根节点sharedPreferences = getSharedPreferences("AA", Context.MODE_PRIVATE);shareSession = getSharedPreferences("AA", Context.MODE_PRIVATE);// 加入子节点editor = sharedPreferences.edit();editor.putString("a1", "a11");editor.commit();sEditor = shareSession.edit();sEditor.putString("a1", "a11");sEditor.commit();// 点击按钮跳转Button btn = (Button) findViewById(R.id.btn_show);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, Activity2.class);startActivity(intent);}});}/** * @description 读取 文件 * @param context * @param file * @return */public Properties loadConfig(Context context, String file) {Properties properties = new Properties();try {FileInputStream s = new FileInputStream(file);properties.load(s);} catch (Exception e) {e.printStackTrace();}return properties;}/** * @description 写入文件 * @param context * @param file * @param properties */public void saveConfig(Context context, String file, Properties properties) {try {FileOutputStream s = new FileOutputStream(file, false);properties.store(s, "");} catch (Exception e) {e.printStackTrace();}}}
activity2:
package com.example.test;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;public class Activity2 extends Activity {private SharedPreferences sp1, sp2;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);// 获取sharepreference数据sp1 = getSharedPreferences("AA", Context.MODE_PRIVATE);sp2 = getSharedPreferences("AA", Context.MODE_PRIVATE);String str1 = sp1.getString("a1", "");String str2 = sp2.getString("a1", "");System.out.println("str1=" + str1 + ",str2=" + str2);}}
结果:
str1=a11,str2=a11
- shareperference使用
- 使用shareperference实现缓存cache功能
- Android 使用SharePerference实现判断是否为第一次登陆
- SharePerference 自动登录的实现
- 文件流,shareperference,应用分级,handler
- android 模拟器上查看shareperference文件内容
- 使用
- 使用
- 使用
- 使用
- 使用
- 使用
- 使用++,--
- 使用$@ $!
- 使用
- SoftICE使用(指令使用)
- 使用GraphEdit使用
- 使用HtmlParser使用心得
- linux route add
- 无光驱无U盘 硬盘安装操作系统 NT6
- java 将远程共享的文件复制到本地
- tomcat在win7下面启动的问题。 郁闷了半天。 多亏了这位大哥。
- BouncyCastle JCE 的安装配置及在java中的使用
- shareperference使用
- 如何结合CoreData给UITableView的section填充数据
- HierarchicalStateMachine(HandlerStateMachine)分析
- 什么是极性反转 polarity reversal
- hbase 结点之间时间不一致造成regionserver启动失败
- 手动修改/etc/shadow和/etc/passwd中的用户密码
- 【转载】会议是浪费工作时间的最佳去处
- jQuery数组处理详解
- 20130324