存储应用的全球数据 [英] Store application global data

查看:82
本文介绍了存储应用的全球数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜      什么是通过存储全局对象/实例了应用程序的最佳方法是什么?

Hi What is the best way to store global objects/instances through out the application ?

推荐答案

下面是下面的code,我用它来存储应用程序上下文字符串

Here is the following code which I use to store String in Application Context.

我使课堂 GlobalVariable.java

import android.app.Application;

public class GlobalVariable extends Application 
{
      private String myState;

      public String getState()
      {
        return myState;
      }//End method

      public void setState(String s)
      {
        myState = s;
      }//End method
}//End Class

在我名为.manifest添加以下code

In .Manifest I add the following code

<application  android:icon="@drawable/icon" android:label="@string/app_name" android:name="GlobalVariable">

在哪里我想设置字符串我用下面的code的值

Where I want to set the value of string I use the following code

GlobalVariable appState = ((GlobalVariable)getApplicationContext());
appState.setState("Testing");

&放大器;在这里我想以检索我使用的数据。

& where I want to Retrive the data I use

GlobalVariable appState = ((GlobalVariable)getApplicationContext());
appState.getState();

这篇关于存储应用的全球数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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