如何让我在我的Andr​​oid应用程序的全球变化? [英] How do I make global changes throughout my app in Android?

查看:118
本文介绍了如何让我在我的Andr​​oid应用程序的全球变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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检查此<一个href="http://trace.adityalesmana.com/2010/08/declare-global-variable-in-android-via-android-app-application/"相对=nofollow>链接

For further refrence check this link

这篇关于如何让我在我的Andr​​oid应用程序的全球变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆