如何让我在我的Andr​​oid应用程序的全球变化?让我、在我、应用程序、全球

2023-09-05 00:45:58 作者:自律微信 自律励志的

我在我的应用程序,它控制整个应用程序中使用的单位设置菜单 - 公制或英制单位。因此,当用户选择这些在菜单中的选项之一,我希望我的应用程序来使用所选择的单位在整个显示器和计算。

I have a settings menu in my app which controls the units used throughout the app - metric or US units. So when the user selects one of these in the options in the menu, I want my app to use the chosen units throughout in display and calculations.

我打算通过存储在共享preferences一个布尔值,要做到这一点,那么检查活动打开布尔每一次的值,然后进行相应的更改。

I plan to do this by storing a boolean in sharedpreferences, then check the value of the boolean every time an activity is opened, and then make the appropriate changes.

有没有更好的方法去这样做?

Is there a better method to go about doing this?

感谢

推荐答案

是的,你可以extends 应用类并存储你的数据在那里使用getter和setter。

Yes you can extends Applications class and store your data over there using Getter and setter.

所以,您的数据将被保留在整个应用程序。

So that your data will be retained throughout the Application.

public class SocketManager extends Application {
private static SocketManager singleton;
public int mBluetoothState;


public synchronized static SocketManager getInstance(Context context) {
    if (null == singleton) {
        singleton = new SocketManager();
    }
    return singleton;
}

public synchronized void setState(int state) {
    mBluetoothState = state;
}

public synchronized int getState() {
    return mBluetoothState;
}
}

访问它的活动,如:

Access it in Activity like :

  SocketManager socketManager = SocketManager.getInstance(this);
  socketManager.setState(10);
  socketManager.getState();

添加您的应用程序Maanifest这样的文件:

Add your Application to Maanifest file like this :

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

   <activity .... />

 </application>

编辑:

您应该补充的是扩展应用到你的类名的应用程序代码而不是活动标签

You should add your class name that extends Application into Application Tag not on Activity Tag

有关进一步refrence检查此链接

For further refrence check this link