`
javajeye
  • 浏览: 62338 次
  • 性别: Icon_minigender_2
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论
  • docong: 这根本不是lazy initial 的问题,而是你没有覆盖Th ...
    ThreadLocal

android static

阅读更多

http://www.pin5i.com/showtopic-android-application-level-variable-global-variable.html
Android应用程序开发中,有的时候我们在应用程序的任何一个地方都需要访问一个全局变量,也就是在任何一个Activity中都可以访问的变量。它不会因为Activity的生命周期结束而消失。要实现应用程序级的变量,我们可以通过Application这个类来实现。

首先,我们新建一个类HelloApplication继承Android.app.Application类,然后在我们的类中增加一些变量。如下所示:

   1. public class HelloApplication extends Application {
   2.         private int globalVariable=1;
   3.

   4.         public int getGlobalVariable() {
   5.                 return globalVariable;
   6.         }
   7.

   8.         public void setGlobalVariable(int globalVariable) {
   9.                 this.globalVariable = globalVariable;
  10.         }
  11. }

复制代码
然后在应用程序配置文件AndroidManifest.xml中进行相应的配置,如下图所示:

附件: 未命名.jpg

完成之后,我们在应用程序的任何一个Activity中可以通过如下方法访问我们的变量:

   1. ((HelloApplication)getApplication()).setGlobalVariable(10);
   2. int valiable=((HelloApplication)getApplication()).getGlobalVariable();

复制代码
怎么样? 是不是很简单?

Application对象只有在应用程序中所有Activity都destroy时才会destrory,所有我们可以在任何一个Activity中访问它。

那么为什么我们不能使用static 变量来存放全局变量呢?如下所示:

   1. public class DataStoreClass {
   2.         private static int globalVariable=1;
   3.

   4.         public static int getGlobalVariable() {
   5.                 return globalVariable;
   6.         }
   7.

   8.         public static void setGlobalVariable(int newGlobalVariable) {
   9.                 globalVariable = newGlobalVariable;
  10.         }
  11. }

复制代码
通过这种方式,DataStoreClass类可能会像Activity一样在某些时候被android系统强行销毁。所以在应用程序运行期间,DataStoreClass的值可能会丢失,或得到一些您不想要的值。

http://www.eoeandroid.com/thread-47981-1-1.html
分享到:
评论

相关推荐

    浅谈Android中关于静态变量(static)的使用问题

    在这里记录下Android中使用static的一些问题。 静态变量的生命周期遵守Java的设计。静态变量在类被load的时候分配内存,并存在于方法区。当类被卸载时,静态变量被销毁。在PC机的客户端程序中,一个类被加载和卸载,...

    Android 7.0 增加Ethernet设置(DHCP与Static ip)

    Android 7.0 增加Ethernet设置(DHCP与Static ip) 静态IP设置选项有: IP 地址 子网掩码 默认网关 首选DNS服务器 备用DNS服务器 参考文章:http://blog.csdn.net/hclydao/article/details/50972932 感谢 hclydao

    android jni使用static变量

    这是我自己写的android jni调用java static属性的例子

    android-ethernet-staticIP-app,安卓-以太网设置-app

    这个是api 21~25(android 5.0.1-7.1.1)能用的app源码,使用eclipse+adt开发(不习惯用AS),android8.0(api26)因时间原因未能测试是否可用。不过android10.0因为api变化被证明肯定不能用。里面除了源码、还有...

    Foxit_Quick_PDF_Library_16.12.rar

    Android Static and Dynamic Lib – Android (API 15, Android 4.0.3 or newer) Static and Dynamic Lib. Android Studio and Visual Studio samples provided. 32-bit and 64-bit. Quick PDF Library for Linux ...

    Foxit Quick PDF Library 16.13 sn

    Quick PDF Library for Android Android Static and Dynamic Lib – Android (API 15, Android 4.0.3 or newer) Static and Dynamic Lib. Android Studio and Visual Studio samples provided. 32-bit and 64-bit. ...

    Android9.0(P)_Ethernet_Settings(DHCP、Static IP).rar

    android 9.0(P),Ethernet Settings(以太网设置-DHCP、Static IP)压缩包中包含 android原生版本、MTK版本

    Android代码-static-maps-api

    Google Static Maps API Fluent and clean Java interface for Google Static Maps API. > DEMO APK Why? Because using Google Play Services to show a Google Map may be overkill, or simply because Google ...

    Android_N_增加Ethernet设置(DHCP与Static ip)

    Android_N_增加Ethernet设置(DHCP与Static ip) Android_N_增加Ethernet设置(DHCP与Static ip)

    Android代码-AndroidPermission6.0

    AndroidPermission6.0 AndroidPermission6.0动态权限 How to use? build.gradle compile 'com.tbruyelle.rxpermissions:rxpermissions:0.8.2@aar' compile 'io.reactivex:rxandroid:1.2.1' PermissionUtils....

    安卓Android源码——jqmDemo_static.zip

    安卓Android源码——jqmDemo_static.zip

    Android代码-MyToast

    MyToast Custom Toast on Android Image Result Usage MyToast.show(this, "MyToast Sample", true); Create toast_layout.xml : ... public static void show(Context context, String text, boolean isLong) {

    Android代码-DebugLog

    DebugLog Create a simple and more understandable Android logs. Why? android.util.Log is the most usable library ...public static final String TAG = "MyApp or MyClass name"; void myFunc(){ android.util

    Android实现获取SERIAL信息的方法

    本文实例讲述了Android实现获取...public static String getDeviceSerial() { String serial = unknown; try { Class clazz = Class.forName(android.os.Build); Class paraTypes = Class.forName(java.lang.String

    Android Menu和ListView 整合设计 源码

    Android Menu和ListView 整合设计 源码 package com.MyMenu; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteCursor; import android.os.Bundle; import ...

    soot-android-static-analysis:使用Soot进行android应用静态分析

    使用Soot对Android应用进行静态分析,目前包括 检测应用中敏感字符串url和email 检测应用中风险代码片段 检测应用可能存在的漏洞 Soot官方Wiki地址 代码结构 src\main\java\...

    Android代码-DBC-Android

    DBC-Android Fluent design by contract assertions for Android Installation Add the JitPack repository to your root build.gradle: allprojects { ...import static com.busybusy.dbc.Dbc.*; @

    android-static-analysis-plugin::linked_paperclips:Android大杂烩静态代码质量检查工具Gradle插件

    android-static-analysis-plugin Android hodgepodge静态代码质量检查工具gradle插件。 (。>﹏<。) Gradle project build.gradle dependencies { com . camnter . gradle . plugin : static - analysis - ...

    Android 7.1 增加Ethernet

    Android 7.0 增加Ethernet设置(DHCP与Static ip) 参考文章:http://blog.csdn.net/hclydao/article/details/50972932

    Android下的dropbear源码

    此包为Android下的dropbear源码,请直接将此包放在Android_src/external目录下,然后设置好编译环境变量之后,进入Android_src目录直接mmm external/dropbear即可。 最终生成的文件包括以下文件: 1.dropbear 2....

Global site tag (gtag.js) - Google Analytics