Swa*_* EP 30 android shared preferences
我有一个设置应用程序,我必须从中检索其他应用程序首选项,但我没有其中的键的详细信息,如何检索该首选项中的所有可用键和值?
谢谢,斯瓦蒂
小智 50
好的!在应用程序1中使用此代码(包名称为"com.sharedpref1")以使用共享首选项存储数据.
SharedPreferences prefs = getSharedPreferences("demopref",
Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring", strShareValue);
editor.commit();
Run Code Online (Sandbox Code Playgroud)
并在Application 2中使用此代码从Application 1中的Shared Preferences获取数据.我们可以得到它,因为我们在应用程序1中使用MODE_WORLD_READABLE:
try {
con = createPackageContext("com.sharedpref1", 0);
SharedPreferences pref = con.getSharedPreferences(
"demopref", Context.MODE_PRIVATE);
String data = pref.getString("demostring", "No Value");
displaySharedValue.setText(data);
} catch (NameNotFoundException e) {
Log.e("Not data shared", e.toString());
}
Run Code Online (Sandbox Code Playgroud)
更多信息,请访问以下URL:http: //androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html
jkh*_*uw1 24
假设首选项是WORLD_READABLE,这可能有效:
final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
// where com.example is the owning app containing the preferences
Context myContext = createPackageContext("com.example", Context.MODE_WORLD_WRITEABLE);
SharedPreferences testPrefs = myContext.getSharedPreferences("test_prefs", Context.MODE_WORLD_READABLE);
Map<String, ?> items = testPrefs .getAll();
for(String s : items.keySet()) {
// do something like String value = items.get(s).toString());
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,文档现在甚至没有解释MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE,而是说:
这个常量在API级别17中被折旧.创建世界可读文件非常危险,并且可能在应用程序中造成安全漏洞.强烈劝阻; 相反,....等
由于折旧,在具有共享偏好的应用之间实现文件共享可能风险太大,尽管它很简单.我不太关心游戏应用程序中MODE_WORLD_READABLE模式的安全漏洞,我只想将角色从一个应用程序转移到另一个应用程序.他们贬低两种共享模式太糟糕了.
如果我们想要来自其他app/pkg/process的读取权限值,它可以工作.但是jkhouw1的回答有些不对劲:
Context myContext = createPackageContext("com.example",
Context.MODE_WORLD_WRITEABLE);
Run Code Online (Sandbox Code Playgroud)
它应该是 :
Context myContext = createPackageContext("com.example",
Context.CONTEXT_IGNORE_SECURITY);
Run Code Online (Sandbox Code Playgroud)
虽然,CONTEXT_IGNORE_SECURITY和MODE_WORLD_WRITEABLE具有相同的"int 2"值,完全归功于这个问题和答案.
| 归档时间: |
|
| 查看次数: |
52227 次 |
| 最近记录: |