Android---SharedPreferences使用
16lz
2021-01-26
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();}
} ’
运行结果如下:
更多相关文章
- Android(安卓)退出整个程序代码
- ios, android 应用程序不允许锁屏
- Android(安卓)获取系统和应用程序
- [学习记录]旋转屏幕禁止重新调用oncreat
- Android存储(二):openFileInput和openFileOutput私有目录下文件
- JNI综合实验一:LED点亮+IO电平读取
- android应用程序---短信发送器
- Android应用程序结构及开发流程
- Android的应用程序的异常处理2