是否可以写入放入apk的文件(例如.txt)? 当用户关闭我的应用程序时,我想在文件上写一些内容,然后在重新打开我的应用程序时阅读它。
答案 0 :(得分:0)
您应该在app-private / data文件夹中写入共享首选项或创建文件。如果您在编译时还有其他内容也应该在该文件中,请将其存储到资源中,并在第一次启动时将其写入/ data中的新文件。
答案 1 :(得分:0)
感谢大家的帮助。 也许@Priya建议我做我想做的最好的方式。
我使用了偏好。
但我仍然想知道它是否适合我想做的事情。我想跟踪用户在玩游戏时获得的积分;当用户重新打开我的应用程序时,我必须显示他们安装应用程序后获得的所有积分。我只是想知道偏好是否适合这个,或者我是否应该使用别的东西。
无论如何,我发布我的代码,它可能会帮助某人
public class managePreferences{
Context context;
managePreferences(Context context){
this.context = context;
}
public String readPreference(String fieldName, String defaultValue){
SharedPreferences prefs = context.getSharedPreferences("MY_PREFERENCES", Context.MODE_PRIVATE);
String value = prefs.getString(fieldName, defaultValue);
return value ;
}
public void writePreference(String fieldName, String value){
SharedPreferences prefs = context.getSharedPreferences("MY_PREFERENCES", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(fieldName, value);
editor.commit();
}
}