java - 在设备的本地存储上创建 Android 保存文件?

标签 java android libgdx google-play

我正在 Android 上使用 Libgdx 玩游戏,我想在用户手机的本地存储上创建一个保存文件,我想知道如何做到这一点。我读过的一些文件如下:https://developer.android.com/guide/topics/data/data-storage.html#filesInternal一直很模糊,我真的不明白发生了什么,或者可以让这个例子在我自己的游戏中工作。 Libgdx 本身有这个功能吗?

最佳答案

在 libgdx 中,我们使用“首选项”来存储数据。这有助于将数据存储在设备本身中。这里我将给你一个在设备内存中存储数据的例子。

           myGame extends ApplicationAdapter{
           public static int count;
           public static bitmapFont font;
           public SpriteBatch batch;
           public static Preferences prefs;  
           public void create()
           {
           batch=new SpriteBatch();
           font=new font(Gdx.files.internla("myFont.png"));
           public static Preferences prefs;
        // this is for setting up the storage for the current project 
           prefs = Gdx.app.getPreferences("myGame");
            }
           public void incrementCount()
          {
          count++;
          if(count>100){
         // here "countValue" is the key(which is the reference to the data)  and "count" is the vlaue(the data). the value is stored in key value method.

          prefs.putInteger("countValue", count);

         // is used to flush the data in to the storage
           prefs.flush();
             }
           public void render()
            {
         // getting the stored value from the preference
            pref.getInteger("countValue",countValue);
            batch.begin();
            batch.draw(font,countValue+"stored value",0,0)
            batch.end()
               }

         // this is the simple way to store the data into the device memory . it will work in both the android and the IOS

关于java - 在设备的本地存储上创建 Android 保存文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39682423/

相关文章:

java - Android中strings.xml字符串中的粗体字

java - Libgdx - 触摸按钮时如何退出应用程序?

Java, libgdx, box2d, Creating a Weld Joint 当两个物体碰撞时

java - libgdx 非连续渲染

java - 亚马逊 Corretto 8 32 位

android - 还有其他方法可以为 Android 测试模拟 SharedPreferences 吗?

java - 在不同的类层次结构中重用相同的类

android - 在 Chrome 上播放视频与在 Android 7+ 上播放 Android WebView 有何不同?

java - eclipse 刷新

java - SOLR 5 中是否有任何解决方法支持数字字段的模式替换过滤器?