android 应用程序 数据,如何在Android应用程序中保存数据

dfe6f3a343de1edc3a46997b6291c233.png

狐的传说

你有两个选择,我会把选择留给你。共享首选项这是Android独有的框架,允许您在键值框架中存储原始值(例如int,boolean,并且String严格来说,String这不是原始的)。这意味着您为值指定一个名称,例如“homeScore”并将值存储到此键。SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);SharedPreferences.Editor editor = settings.edit();editor.putInt("homeScore", YOUR_HOME_SCORE);// Apply the edits!editor.apply();// Get from the SharedPreferencesSharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);int homeScore = settings.getInt("homeScore", 0);内部存储器在我看来,这是你可能正在寻找的。您可以将任何想要的内容存储到文件中,这样可以提供更大的灵活性。但是,该过程可能比较棘手,因为所有内容都将存储为字节,这意味着您必须小心保持读写过程协同工作。int homeScore;byte[] homeScoreBytes;homeScoreBytes[0] = (byte) homeScore;homeScoreBytes[1] = (byte) (homeScore >> 8);  //you can probably skip these two homeScoreBytes[2] = (byte) (homeScore >> 16); //lines, because I've never seen a                                                                 //basketball score above 128, it's                                              //such a rare occurance.FileOutputStream outputStream = getApplicationContext().openFileOutput(FILENAME, Context.MODE_PRIVATE);outputStream.write(homeScoreBytes);outputStream.close();现在,您也可以查看外部存储,但我不建议在这种特殊情况下,因为外部存储可能不会在以后存在。(注意,如果你选择它,它需要一个权限)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值