Android—SharedPreferences使用

实例:记录应用程序使用的次数。
实现功能:当用户第一次启动该应用程序,系统创建的SharedPreferences来记录使用次数。用户以后启动应用程序时,系统会先读取SharedPreferences中记录的使用次数,然后使用次数加1.
直接MainActivity.java文件中编写代码。
’ public class MainActivity extends Activity {

@SuppressLint("ShowToast")@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    @SuppressWarnings("deprecation")    SharedPreferences preferneces=this.getSharedPreferences("count",MODE_WORLD_READABLE);    //读取SharedPreferences的count数据    int count=preferneces.getInt("count", 0);    if(count==0){        Toast.makeText(this, "恭喜你第一次使用本软件", 1000).show();    }else{    Toast.makeText(this, "你已使用过本程序"+count+"次", 1000).show();    }    Editor editor=preferneces.edit();    //存入数据    editor.putInt("count", ++count);//使用次数加1    //提交修改    editor.commit();}

} ’
运行结果如下:

更多相关文章

  1. Android(安卓)退出整个程序代码
  2. ios, android 应用程序不允许锁屏
  3. Android(安卓)获取系统和应用程序
  4. [学习记录]旋转屏幕禁止重新调用oncreat
  5. Android存储(二):openFileInput和openFileOutput私有目录下文件
  6. JNI综合实验一:LED点亮+IO电平读取
  7. android应用程序---短信发送器
  8. Android应用程序结构及开发流程
  9. Android的应用程序的异常处理2

随机推荐

  1. android Seekbar双滑块滑动
  2. Android使用OpenGL(GLSurfaceView)视频画
  3. Android(安卓)简介及基础知识
  4. Android studio DrawerLayout
  5. 【Android(安卓)Basic Training - 02】基
  6. Android - 获取控件(包括状态栏与标题栏)宽
  7. http://www.jianshu.com/p/7e46fe7485bb
  8. Android 关于build.gradle的一些配置问题
  9. Android中隐藏标题栏和状态栏
  10. flutter 混合开发