android - 如何保存数据直到在android中卸载应用程序?

标签 android save

在 Android 中,一旦用户从“管理应用程序”->“应用程序名称”->“清除数据”中清除数据,SharedPreference 和 SQLite 数据库就会被清除。如果在安装应用程序之前应该保留一些信息怎么办。我想存储信息,直到应用程序安装到设备中。

有没有办法真正做到这一点?

有没有办法将数据存储到应用程序的生命周期?我读到 2.2 提供了 getExternalFiles() 选项,但同样依赖于 SDCard。应用程序无法在 Android 设备上永久存储数据吗?

最佳答案

@everyone-who-says-to-use-internal-storage 如果您转到“设置”>“应用程序”>“管理应用程序”>(应用程序)>“清除数据”,您正在谈论的文件将被删除这只是清除应用程序存储的数据的一种方法。此方法将删除您的应用自首次运行以来存储的所有内容。

“永久”存储数据的唯一方法是将其存储在您可以控制的地方,例如服务器上的数据库。

编辑:这是一个简单快速的测试用例,用于存储和显示已保存的内部文件。此测试用例是对@sunil 评论的回应

public class TestFileActivity extends Activity {
    private static final String FILENAME = "hello_file";


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        byte[] data = null;
        String collected = null;

        try {
            FileInputStream fis = openFileInput( FILENAME );

            data = new byte[fis.available()];

            while(fis.read(data) != -1) {
                collected = new String( data );
            }

            fis.close();
        } catch ( FileNotFoundException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch ( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Button b1 = (Button)findViewById( R.id.button1 );

        b1.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick( View v ) {
                putInFile();
            }
        });

        if( collected != null ) {
            TextView tv = (TextView)findViewById( R.id.textView1 );

            tv.setText( collected );
        }

    }

    private void putInFile() {

        String string = "hello world!";

        try {
            FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fos.write(string.getBytes());
            fos.close();
        } catch ( FileNotFoundException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch ( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button
        android:text="Button"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    ></Button>
</LinearLayout>

关于android - 如何保存数据直到在android中卸载应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6730558/

相关文章:

android - Dagger-Hilt:@ViewModelInject 没有注入(inject) MyViewModel 并崩溃?

image - 使用 Processing 保存图像

java - 当 Activity 被破坏时保存切换按钮的值,并在 Activity 再次启动时将它们放回原处

iphone - 保存 UIPicker 中选择的值的最佳方法?

java - getResources() 上的 nullPoint 异常

java - 安卓日历 : Changing the start day of week

android - 覆盖方向更改但不重新启动 Activity 并传递状态数据

android - 应用程序崩溃后清除状态栏中的通知

java - hibernate中的save方法没有立即插入

java - 如何在java中设置和从文件中获取整数