来自另一个类的 Android 上下文

发布于 12-04 04:47 字数 621 浏览 0 评论 0原文

我想做的是以下内容...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野味少女2024-12-11 04:47:45

为具有 Context 参数的 Settings 类创建构造函数。然后,当您从该类实例化该对象时,只需传递您的应用程序上下文即可。

构造函数:

 public Settings(Context cont)
{
     //do something with the context, e.g assign it to some private variable of type Context

}

在您的活动类中:

Settings settings = new Settings(MyActivity.this);

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:

 public Settings(Context cont)
{
     //do something with the context, e.g assign it to some private variable of type Context

}

In your activity class:

Settings settings = new Settings(MyActivity.this);
无远思近则忧2024-12-11 04:47:45

不传递上下文,使用 File 类会更合适。您应该尽可能避免将上下文传递给其他类。

    File file = new File("sample.txt");
    InputStream is = new FileInputStream(file);

另一种选择是将上下文传递给该方法。

boolean isTrue = Settings.readBoolean(MyClass.this);

Instead of passing context, it would be more appropriate to use Fileclass. You should avoid passing context to other classes whenever possible.

    File file = new File("sample.txt");
    InputStream is = new FileInputStream(file);

Another alternative could be to pass the context to the method.

boolean isTrue = Settings.readBoolean(MyClass.this);
别靠近我心2024-12-11 04:47:45

如果您在多个活动中使用多个片段,那么有一个获取上下文的快捷方法。
创建一个静态类。
定义上下文。
每当你改变活动时,都要改变环境。
使用该静态类获取片段中的上下文。
我知道您可以通过 getactivity() 获取片段中的上下文,但如果您使用某些适配器或非上下文类,这将非常有帮助

If you are using multiple fragments with multiple activities than there is a shortcut method for getting the context.
create a static class.
define the context.
whenever you are changing activity change the context.
get the context in fragment using that static class.
I know you can get the context in a fragment by getactivity() but if you are using some adapter or noncontext class this will be really helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文