如何在android中卸载应用程序时删除共享首选项 [英] how to remove shared preference while application uninstall in android

查看:34
本文介绍了如何在android中卸载应用程序时删除共享首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 android 应用程序可以通过 SharedPreferences 保存登录详细信息,例如用户名和密码,这很好用,但是我需要删除所有使用过的 SharedPreferences 而我的应用程序卸载.怎么做?

I have an android application to save the login details such as user name and password via SharedPreferences thats works fine, but i need to remove all my used SharedPreferences while my application uninstall. How to do it?

SavePreferences("one ", "");
SavePreferences("two", "");
LoadPreferences();

 private void SavePreferences(String key, String value){
    sharedPreferences = getSharedPreferences("TEST", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
   }

 private void LoadPreferences(){
    sharedPreferences = getSharedPreferences("TEST", MODE_PRIVATE);
    String strSavedMem1 = sharedPreferences.getString("MEM1", "");
    String strSavedMem2 = sharedPreferences.getString("MEM2", "");   
   } 

我想在卸载应用程序时删除此 SharedPreferences.

I want to remove this SharedPreferences when my application uninstall.

推荐答案

SharedPreferences 始终与应用程序卸载一起删除.

SharedPreferences is always deleted along with the app uninstall.

当您卸载任何应用程序时,该应用程序在您的内存中所做的所有更改都将被撤销,这意味着您的 SharedPreference 文件、其他数据文件、数据库文件、应用程序会被 Android 操作系统自动删除.

When you uninstall any application all the changes the application have made in your internal memory are revoked, that means your SharedPreference files, Other data files, Database file, Application gets removed automatically by the Android OS.

29/04/15:对于 >= 21 API,请参考 @Maher Abuthraa 的回答

这篇关于如何在android中卸载应用程序时删除共享首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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