如何将数据保存在一个Android应用程序 [英] How to save data in an android app

查看:153
本文介绍了如何将数据保存在一个Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近codeD Android应用程序。这只是一个简单的应用程序,可以让你保持得分的篮球比赛用几个简单的计数器间隔。我非常新的编码,我只是有一个快速的问题。我得到的需求来添加保存功能,这样可以节省你的分数,并将它们加载它们回来了。目前,当你停止的应用程序,你的数据丢失。因此,我想知道是什么,我就必须增加有应用程序保存的标签(得分),然后装入它备份。谢谢你们对不起,我不是很了解这个东西。

I recently coded an Android app. It's just a simple app that allows you to keep score of a basketball game with a few simple counter intervals. I'm extremely new to coding, and I just had one quick question. I'm getting demand to add a save feature, so you can save your scores and them load them back up. Currently, when you stop the app, your data is lost. So what I was wondering is what I would have to add to have the app save a label (score) and then load it back up. Thanks guys sorry I don't know much about this stuff.

推荐答案

您有两种选择,我会选择离开了你。

You have two options, and I'll leave selection up to you.

  1. 共享preferences

  1. Shared Preferences

这是唯一的Andr​​oid的一个框架,允许你存储的原始值(如 INT 布尔值,字符串,虽然严格地说字符串不是原始)的键值框架。这意味着,你给某个值的名称,比如homeScore和值保存到该键。

This is a framework unique to Android that allows you to store primitive values (such as int, boolean, and String, although strictly speaking String isn't a primitive) in a key-value framework. This means that you give a value a name, say, "homeScore" and store the value to this key.

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 SharedPreferences
SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
int homeScore = settings.getInt("homeScore", 0);

  • 内部存储

  • Internal Storage

    这在我看来,就是你可能会寻找什么。您可以将想要的文件什么,所以这给你更多的灵活性。但是,这个过程可以棘手,因为一切都将被存储为字节,这意味着你必须要小心,让您的读写一起工作的进程。

    This, in my opinion, is what you might be looking for. You can store anything you want to a file, so this gives you more flexibility. However, the process can be trickier because everything will be stored as bytes, and that means you have to be careful to keep your read and write processes working together.

    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();
    

  • 现在,你也可以考虑外部存储,但我不'吨建议,在这种特殊情况下,因为外部存储可能不是有存货。 (请注意,如果你选择这个,它需要一个许可)

    Now, you can also look into External Storage, but I don't recommend that in this particular case, because the external storage might not be there later. (Note that if you pick this, it requires a permission)

    这篇关于如何将数据保存在一个Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆