获取所有 SharedPreferences 名称及其所有键?
我正在制作备份程序,将手机 SharedPreferences
数据保存到我自己结构的文件中。但我不知道如何列出我需要的全部内容:
例如,2 个程序使用名称 "Program A"
和 "Program B"
保存了它们的 SharedPreferences。我需要获取包含这两个名称的字符串数组。然后,我将 getSharedPreferences
与“程序 A”一起使用,我需要获取该程序已保存的所有密钥。
真的可能吗?
编辑1:我不知道手机上有哪些程序/活动。我想获取每个程序保存的所有密钥
。就像您备份所有手机数据一样,但仅备份 SharedPreferences 值。
例如:您的手机有 10 个程序,每个程序都会创建一个名为 Program 1
到 Program 10
的 SharedPreferences(当然,无论他们想要什么名称)。我想获取所有这些Program 1
到Program 10
字符串。然后,如果Program 1
有5个按键,分别为Key 1
到Key 5
,我想获取这些按键名称。
编辑2:根据 NikolaMKD
指南,这是我到目前为止所做的,但列表返回所有程序,所有行都带有“无首选项”,即使在第一行,我也保存了 SharedPreferences我的活动:
public class Test extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("Test", 1);
// No matter what name is: Test or the package name
Editor editor = preferences.edit();
editor.putInt("TestKey", 0);
editor.commit();
List<ResolveInfo> Temp =
getPackageManager().queryIntentActivities(
new Intent(Intent.ACTION_MAIN, null)
.addCategory(Intent.CATEGORY_LAUNCHER), 0);
String[] App = new String[Temp.size()];
for (int i = 0; i < Temp.size(); i++) {
App[i] = Temp.get(i).activityInfo.name;
FileReader reader = null;
try {
reader = new FileReader("/data/data/"
+ App[i] + "/shared_prefs/" + App[i] + "_preferences.xml");
} catch (FileNotFoundException e) {
reader = null;
}
if (reader != null)
App[i] += " (Have Prefereces)";
else
App[i] += " (No Prefereces)";
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, App));
}
}
I'm making backup program, that saved the phone SharedPreferences
data into a file of my own structure. But I don't know how to list them all, which I need:
For example, 2 program saved their SharedPreferences with names "Program A"
and "Program B"
. I need to obtains thay String array contain these 2 names. Then, I use getSharedPreferences
with "Program A", I need to get all keys that program has saved.
Is it really possible?
EDIT1: I DON'T know what programs/activities on the phone. I want to get ALL the keys
that every programs saved. It just like you back up all your phone data, but only SharedPreferences values.
For example: Your phone has 10 programs, each creates a SharedPreferences named Program 1
to Program 10
(but of course, whatever name they want). And I would like to obtain all these Program 1
to Program 10
String. Then, if Program 1
has 5 keys called Key 1
to Key 5
, I want to obtain these keys name.
EDIT2: According to NikolaMKD
guide, this is what I've done so far, but the list return all programs, with "No Preferences" at all line, even at the first, I saved the SharedPreferences with my activity:
public class Test extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("Test", 1);
// No matter what name is: Test or the package name
Editor editor = preferences.edit();
editor.putInt("TestKey", 0);
editor.commit();
List<ResolveInfo> Temp =
getPackageManager().queryIntentActivities(
new Intent(Intent.ACTION_MAIN, null)
.addCategory(Intent.CATEGORY_LAUNCHER), 0);
String[] App = new String[Temp.size()];
for (int i = 0; i < Temp.size(); i++) {
App[i] = Temp.get(i).activityInfo.name;
FileReader reader = null;
try {
reader = new FileReader("/data/data/"
+ App[i] + "/shared_prefs/" + App[i] + "_preferences.xml");
} catch (FileNotFoundException e) {
reader = null;
}
if (reader != null)
App[i] += " (Have Prefereces)";
else
App[i] += " (No Prefereces)";
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, App));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
获取所有 _preferences.xml
从
/data/data/(package)/shared_prefs/(package)_preferences.xml
解析 XML,在 XML 中每个键都有名称。我可能误解了你的问题
Get all the _preferences.xml from
/data/data/(package)/shared_prefs/(package)_preferences.xml
Parse the XML, in the XML there is name for every key. I might misunderstood your question
试试这个:
Try this:
使用
sharedPreference.getAll();
返回一个Map
。Use the
sharedPreference.getAll();
which returns aMap
.不要认为这是可能的,您无法读取其他应用程序的 /data/data/ 文件夹。在模拟器上,您可以阅读它们,因为您具有 root 访问权限。
Don't think it is possible, You can't read other apps /data/data/ folder. On the emulator you can read them because you have root access.
这是一个很好的问题,但我认为没有一个简单的方法可以做到这一点。最好的方法是将这些名称存储在 DefaultSharedPreferences 或某种全局数组中,或者如果您知道首选项资源 ID,则可以从活动本身检索共享Preferences:
It's a good question, and I don't think there is a straightforward way of doing it. The best way to do it would be to either store these names in a DefaultSharedPreferences or some sort of global array, or you could retrieve the sharedPreferences from the activity itself if you know the preference resource id:
您可以循环访问共享首选项目录中的所有 xml 文件,并获取应用程序中共享首选项名称的所有文件的名称。
You can loop through all the xml files in your shared preference directory and get the name of all the files which are the shared preference names in your app.