我正在以这种方式检查文件是否存在,但我需要超越,我需要知道具体是否有一个,有什么办法吗?
File f = new File("/data/data/com.eventrid.scanner/shared_prefs/Eventrid.xml");
if (f.exists()){
}
else{
}
Run Code Online (Sandbox Code Playgroud) 我有一个Android应用程序,我在xml文件中有我的首选项,工作正常.我现在想要使用代码设置其中一个首选项而不是显示整个首选项屏幕,我将如何进行此操作?
如何在非Activity类中使用SharedPreferences?我尝试制作一个通用的Preferences实用程序类并导入android.content.Context但Eclipse仍然不允许我使用getSharedPreferences().
正如标题所说,我想保存并检索某些字符串.但我的代码不会在检索或存储中通过第一行.我尝试按照以下链接:http://developer.android.com/guide/topics/data/data-storage.html
private void savepath(String pathtilsave, int i) {
String tal = null;
// doesn't go past the line below
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
tal = String.valueOf(i);
editor.putString(tal, pathtilsave);
editor.commit();
}
Run Code Online (Sandbox Code Playgroud)
和我的检索方法:
public void getpaths() {
String tal = null;
// doesn't go past the line below
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
for (int i = 1; i <= lydliste.length - 1; i++) {
tal = String.valueOf(i);
String restoredText = settings.getString(tal, null);
if (restoredText != null) {
lydliste[i] …Run Code Online (Sandbox Code Playgroud) 有没有人在Nexus 6P设备上遇到过这个问题?我只是在Nexus 6P(运行Google Fi)上遇到此问题.
当我安装的应用程序有一个关键的userIsLoggedIn内部SharedPreferences.
这个块:
boolean userIsLoggedIn = SharedPrefs.userIsLoggedIn(this);
// Then in another class...
public static boolean userIsLoggedIn(Context context) {
// For users updating apps, if the previous key-value is a string, convert it to boolean
try {
return context.getSharedPreferences(LOGIN_FILE, Context.MODE_PRIVATE)
.getBoolean(USER_LOGGED_IN, false);
} catch (ClassCastException e) {
Logger.e(TAG, e.getMessage());
context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE)
.edit()
.putBoolean(USER_LOGGED_IN, false)
.commit();
return context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE)
.getBoolean(USER_LOGGED_IN, false);
}
}
Run Code Online (Sandbox Code Playgroud)
现在这应该返回false一个新的卸载,但在全新安装上调试它我在App Startup上得到以下内容.
我也运行Proguard,如果这很重要,当在非proguard启用的APK上运行设备时它运行正常.在任何其他设备上运行proguard运行正常.
我正在开发一个应用程序,用户需要登录才能执行操作...但主要是在Android手机上使用"让我登录"......在这种情况下我将不得不维护用户名和密码的值我的应用程序......应该使用首选项,还是SQLite Db,还是有别的东西,我怎样才能使它安全?Plz帮助...在此先感谢..
android android-preferences sharedpreferences android-sqlite
我正在尝试在没有Dagger的情况下实现MVP(用于学习目的).但我遇到了问题 - 我使用Repository模式从缓存(共享首选项)或网络获取原始数据:
Shared Prefs|
|<->Repository<->Model<->Presenter<->View
Network|
Run Code Online (Sandbox Code Playgroud)
但要把手放在共享首选项上,我必须把它放在某个地方
presenter = new Presenter(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)
我使用onRetainCustomNonConfigurationInstance/ getLastCustomNonConfigurationInstancepair来保持Presenter"保留".
public class MyActivity extends AppCompatActivity implements MvpView {
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
presenter = (MvpPresenter) getLastCustomNonConfigurationInstance();
if(null == presenter){
presenter = new Presenter(getApplicationContext());
}
presenter.attachView(this);
}
@Override
public Object onRetainCustomNonConfigurationInstance() {
return presenter;
}
//...
}
Run Code Online (Sandbox Code Playgroud)
那么如何在没有Dagger的MVP中使用共享首选项并且不会导致Presenter依赖于上下文?
mvp dependencies android dependency-injection sharedpreferences
在我的应用程序中有一个按钮(activity1).当用户点击它时我想在游戏中没有声音.我想我应该通过在按钮的onClick方法中使用activity1中的sharedpreferences来做到这一点:
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("sound","1");
editor.commit();
Run Code Online (Sandbox Code Playgroud)
声音和游戏从另一个活动(activity2)开始.我需要在那里阅读set sharedpreferences,但我不知道该怎么做.
谢谢
编辑
我已经离开了这条线:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Activity1.this);
Run Code Online (Sandbox Code Playgroud)
根据您在Activity2.class中的帮助,我读了这样的首选项:
SharedPreferences myPrefs = getSharedPreferences("Activity1", MODE_PRIVATE); //Activity1.class
String ifsound = myPrefs.getString("sound","");
if (ifsound.equals("1"))
{
Toast.makeText(Activity1.this, "1", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(Activity1.this, "0", Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
我有一个与服务器通信的应用程序.当用户登录到应用程序时,在服务器上创建身份验证令牌并将其存储在SharedPreferences应用程序中,并且每当应用程序从Web服务请求数据时,验证身份验证令牌.
我的问题是,将身份验证令牌存储在SharedPreferences?中是否安全?我问,因为具有root权限的用户可以访问首选项,提取令牌并使用它.
无论如何,在这方面有更多的安全吗?