Android学习笔记十六.Android数据存储与IO.SharedPreferences
package com.example.android_sharedprefences_1; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; 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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //1.获取只能被本应用程序读、写的SharedPreferences对象 preferences=getSharedPreferences("jiangdongguo",Context.MODE_PRIVATE); //实例化一个SharedPreferences对象 editor=preferences.edit(); //实例化一个SharedPreferences.Editor对象 Button read=(Button)findViewById(R.id.readBtn); Button write=(Button)findViewById(R.id.writeBtn); //2.读取SharedPreferences中的数据:读取字符串、int类型数据 read.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { String time=preferences.getString("time", null); //获取键值为time的数据 int randNum=preferences.getInt("random", 0); //获取键值为random的数据 String result=(time==null?"您暂时还未写入数据":"写入时间为:"+time+"\n上次生成的随机数为:"+randNum); Toast.makeText(SharedPreferencesTest.this, result, Toast.LENGTH_SHORT).show(); } }); //3.利用SharedPreferences.Editor向Preferences写入数据 write.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss"); editor.putString("time",sdf.format(new Date())); //存入当前时间到time键 editor.putInt("random", (int)(Math.random()*100)); //存入一个随机数 editor.commit(); //提交所有存入的数据 } }); } }
/*实现功能:计算应用程序被打开的次数*/ package com.example.android_sharedprefences_2; import android.support.v7.app.ActionBarActivity; import android.widget.Toast; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; public class Usecount extends ActionBarActivity { SharedPreferences preferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //1.获取本应用程序的SharedPreferences,其文件名为count preferences=getSharedPreferences("count",Context.MODE_WORLD_READABLE); //2.读取SharedPreferences里的count数据(默认值为0),并显示 int count=preferences.getInt("count", 0); Toast.makeText(this, "程序被使用了"+count+"次。",Toast.LENGTH_SHORT).show(); //3.存储count次数到SharedPreferences里(应用每打开依次,count+1) Editor editor=preferences.edit(); editor.putInt("count", ++count); //4.提交数据 editor.commit(); } }
/*实现:读取其他应用程序所保存的SharedPreferences数据*/ package com.example.android_sharedpreferences_3; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.widget.TextView; public class ReadOtherPreferences extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Context useCount=null; try{ //1.获取其他程序所对应的Context useCount=createPackageContext("com.example.android_sharedprefences_2",Context.CONTEXT_IGNORE_SECURITY); } catch(NameNotFoundException e) { e.printStackTrace(); } //2.使用其他程序的Context获取对应的SharedPreferences SharedPreferences prefs=useCount.getSharedPreferences("count", Context.MODE_WORLD_READABLE); //3.读取数据并显示 int count=prefs.getInt("count", 0); TextView show=(TextView)findViewById(R.id.show); show.setText("UseCount应用程序以前被使用了"+count+"次。"); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。