访问变量的Java / Android系统有道变量、系统、Java、Android

2023-09-05 04:09:39 作者:我姓王却不能忘记你的模样

我有多个类:

的主要活动 服务 设置

通过设置,可以改变有一定的延迟(可变),我需要(我需要一个变量的值)的服务。

With settings it is possible to change a certain delay (variable) which I need (I need the value of that variable) in service.

我应该如何实现呢?

我应该开始设置与意图,并使其返回延迟的价值呢? 我应该做一个吸能与像mysettings.getdelay创建的设置的实例后返回值()? 或有没有这样做?的另一个(/更好)的方式 should I start settings with an intent and make it return the value of delay? should I make a getter which can return the value after creating an instance of settings with something like mysettings.getdelay()? or is there another (/better) way of doing this?

感谢 最后我用类似Ridcully的答案。唯一不同的是我没有给一个公开声明,并使用了getter和setter方法​​来代替。人们总是告诉我使用getter和setter。

thanks I ended up using something like Ridcully's answer. Only difference is that I didn't give a public declaration and used a getter and setter instead. People always told me to use getters and setters.

推荐答案

有关的设置,你应该使用Android的标准用于此目的 - 共享preferences 。 阅读这篇 API指南的一个开始。

For your settings you should use Android's standard for this purpose - SharedPreferences. Read this API Guide for a start.

编辑:

有关存储数据只为您的应用程序的生命周期,您还可以使用自己的应用程序类,并使用变量有:

For storing data only for the life-time of your app you can also use your own Application class and use variables there:

自定义应用程序类:

public class MyApplication extends Application {

    public static int delay = 0;

}

在AndroidManifest.xml中:

In the AndroidManifest.xml:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".MyApplication">