简体   繁体   English

避免在Android设备上的应用程序设置中单击“清除数据”按钮从数据库中删除数据

[英]Avoid deleting the data from database on clicking 'clear data' button in the app settings on android device

I am using database to store some data . 我正在使用database来存储一些data But when user go to Apps Setting s and press the 'clear data' button in the app settings, data in databases gets deleted how can avoid this. 但是,当用户转到Apps Setting并按Apps Setting'clear data'按钮时,数据库中的数据将被删除,如何避免这种情况。 Can I make 'clear data' BUTTON foR my app in Apps settings as not enabled or any other suggestions.... 我可以在应用程序设置中将我的应用程序中的'clear data'按钮设置为未启用或任何其他建议....

Add android:manageSpaceActivity=".ActivityOfMyChoice" to the application tag of your Manifest like: android:manageSpaceActivity=".ActivityOfMyChoice"添加到您的Manifest的应用程序标记中,如:

<application android:label="MyApp" android:icon="@drawable/icon" android:manageSpaceActivity=".ActivityOfMyChoice" >

Then instead of "Clear Data", there is a button for "Manage Space" which launches ActivityOfMyChoice 然后,有一个“管理空间”按钮,而不是“清除数据”,它启动ActivityOfMyChoice

ActivityOfMyChoice.java ActivityOfMyChoice.java

public class ActivityOfMyChoice extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    finish();
}

So when we click the Mange activity it finish the Activity. 因此,当我们单击Mange活动时,它会完成活动。

There is no direct way to avoid any app data being cleared from what I know. 没有直接的方法可以避免从我所知道的内容中删除任何应用数据。

However, you could store your data on the external storage (SD card) in some file and at every app launch check if that file is there and merge it's contents into the database. 但是,您可以将数据存储在某个文件中的外部存储(SD卡)上,并在每个应用程序启动时检查该文件是否存在并将其内容合并到数据库中。 This will of course not work if the user manually deletes your backup file from the SD, inserts another SD, removes the SD or connects the device as a mounted drive to the computer and then launches the app. 如果用户从SD手动删除备份文件,插入另一个SD,删除SD或将设备作为已安装的驱动器连接到计算机,然后启动应用程序,这当然不起作用。

Android won't ask whether your App like's its data to be deleted or not. Android不会询问您的应用程序是否要删除其数据。 It's a system functionality provided to user. 这是为用户提供的系统功能。 User has power to totally uninstall the App. 用户有权完全卸载App。

So, create/open the database file at another location. 因此,在另一个位置创建/打开数据库文件。

Data durability: 数据持久性:

  1. App cache dir : deleted when internal memory is low. App cache dir:内存不足时删除。
  2. App data dir : deleted when user clears app data , app is uninstalled. 应用数据目录:当用户清除应用数据时删除,应用已卸载。
  3. External storage : user can delete anytime, user may also remove the storage. 外部存储:用户可以随时删除,用户也可以删除存储。
  4. Backup manager : new versions of android offer Apps to backup data on cloud via BackupManager. 备份管理器:Android的新版本提供了通过BackupManager在云上备份数据的应用程序。
  5. Remote server : Stays in server until deleted. 远程服务器:保留在服务器中直到删除。

I think there's a way but haven't tried it yet. 我认为有一种方法,但尚未尝试过。 You can store your DB file in asset folder, check if it available, otherwise just do like this https://stackoverflow.com/a/9109728/265167 (pay attention on DataBaseHelper class). 您可以将您的数据库文件存储在资产文件夹中,检查它是否可用,否则只需执行此操作https://stackoverflow.com/a/9109728/265167 (注意DataBaseHelper类)。 But do this way, you'll lose all "update data". 但这样做,你将失去所有“更新数据”。

最后我找到了一些解决方案,如果你让应用程序系统管理员那么它就无法清除数据并卸载app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM