卸载应用程序后保留数据

Retaining data after uninstalling the app

有没有办法在卸载应用程序后保留我的一些数据,然后再将其安装到设备上。我需要检查我的应用程序之前是否已安装在某人的设备上。

是的,如果您将应用程序数据保存在设备的外部存储中,这是可能的,因为即使您的应用程序被卸载,该数据也不会被删除。

重新安装应用程序后,您可以读取文件数据以确定该应用程序是以前安装的还是全新安装的,以防文件不存在。

注意:-外部目录中的数据对用户可见,因此用户可能会从文件管理器中删除数据。

您可以使用共享首选项来存储此数据。 使用 MODE_WORLD_READABLE 创建共享引用。 您可以在应用程序 运行 时设置一个标志为真,并且可以在每次安装时提取此值以查明此值是否存在。 如下所示:

当应用程序 运行(将此代码写入您的应用程序的启动代码中 - 首先触发):

SharedPreferences sharedpreferences = context.getSharedPreferences("test", Context.MODE_WORLD_READABLE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString("app_already_installed", "true");
                editor.commit();

检查应用是否已经安装(写在你想检查早期安装的地方):

SharedPreferences sharedpreferences = context.getSharedPreferences("test", Context.MODE_WORLD_READABLE);
String is_app_already_installed= sharedpreferences.getString("app_already_installed", "false");

if(is_app_already_installed.equals("true"){
//do-something
}
else{
//do-something
}