来自另一个类的 Android 上下文
我想做的是以下内容...
FileInputStream fIn;
try {
fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
BufferedReader br = new BufferedReader(isr);
isTrue = Boolean.parseBoolean(br.readLine());
Log.i("IN", "isTrue = " + isTrue);
}
但这仅适用于扩展 Android 内 Activity 类的类。
我有一个“设置”类,它从文件中写入和读取当前的游戏设置,但该文件有很多我不想操纵的数据。
我最初使用的是 BufferedReader
& BufferedWriter
但我无法将数据设置为 Private,这意味着任何人都可以编辑该文件。使用 OutputStreamWriter
至少更安全一点
如何让我的“Settings”类(具有完全静态方法)访问上下文,以便我可以使用诸如 之类的方法>打开文件输入
What I'm trying to do is the following...
FileInputStream fIn;
try {
fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
BufferedReader br = new BufferedReader(isr);
isTrue = Boolean.parseBoolean(br.readLine());
Log.i("IN", "isTrue = " + isTrue);
}
But this is only going to work in the class that extends the Activity class within Android.
I have a "Settings" class which writes and reads the current games settings from a file, but this file has a lot of data I dont really want manipulated.
I was initially using a BufferedReader
& BufferedWriter
but I cannot set the data to Private which means anyone can just edit the file. With a OutputStreamWriter
it is a little more secure at least
How do I get my excising "Settings" class (which has entirely static methods) to have access to the Context so I may use methods such as openFileInput
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为具有 Context 参数的 Settings 类创建构造函数。然后,当您从该类实例化该对象时,只需传递您的应用程序上下文即可。
构造函数:
在您的活动类中:
Create constructor for your Settings class that has Context argument. Then when you instantiate the object from that class, just pass the your application context, thats it.
Constructor:
In your activity class: