Android Application 创建全局变量

2024-06-18 02:38

本文主要是介绍Android Application 创建全局变量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/anbo724/article/details/7287776

以前都是建立一个ConstData的类来保存全局用的变量,但是有时候确实是有点小问题。

所以研究了一下使用Application来建立全局变量,下面就是代码,主要分为四个文件:

(1)是MyApplication类,保存全局变量以及变量的查询和修改

(2)TestAndroid 类 也是主类

(3)otherActivity 另外一个类调用全局变量试试是不是被主类改变了

(4)manifest.xml文件 

MyApplication

[java]  view plain copy
  1. package an.test.android;  
  2.   
  3. import android.app.Application;   
  4.   
  5. public class MyApp extends Application{   
  6.   
  7.     private String mylabel ;       
  8.     public String getLabel(){   
  9.         return mylabel;   
  10.     }      
  11.     public void setLabel(String s){   
  12.         this.mylabel = s;   
  13.     }   
  14.   
  15.     @Override   
  16.     public void onCreate() {   
  17.         // TODO Auto-generated method stub   
  18.         super.onCreate();   
  19.         setLabel("Welcome!"); //初始化全局变量          
  20.     }      
  21. }   

TestAndroid

[java]  view plain copy
  1. package an.test.android;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7.   
  8. public class TestAndroid extends Activity {  
  9.     /** Called when the activity is first created. */  
  10.   
  11.       
  12.     private MyApp myApp;   
  13.       
  14.       
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         myApp = (MyApp) getApplication(); //获得自定义的应用程序MyApp   
  20.         Log.i("guoll""InitLabel:"+myApp.getLabel());   //将我们放到进程中的全局变量拿出来,看是不是我们曾经设置的值   
  21.   
  22.         myApp.setLabel("Changing!");  //修改一下   
  23.         Log.i("guoll""ChangeLabel:"+myApp.getLabel()); //看下,这个值改变了没有   
  24.   
  25.         Intent intent = new Intent();  //再看一下在另一个Activity中是取到初始化的值,还是取到修改后的值   
  26.         intent.setClass(this, otherActivity.class);   
  27.         startActivity(intent);   
  28.           
  29.           
  30.           
  31.     }  
  32. }  

otherActivity

[java]  view plain copy
  1. package an.test.android;  
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.util.Log;   
  6.   
  7. public class otherActivity extends Activity{   
  8.       
  9.     private MyApp myApp;   
  10.       
  11.     @Override   
  12.     protected void onCreate(Bundle savedInstanceState) {   
  13.   
  14.             super.onCreate(savedInstanceState);   
  15.             setContentView(R.layout.main);   
  16.               
  17.             myApp = (MyApp) getApplication();  //获得自定义的应用程序MyApp   
  18.             Log.i("guoll""OhterActivity receive the Label:"+myApp.getLabel()); //查看变量值是否修改了   
  19.   
  20.     }          
  21. }   

manifest.xml


[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="an.test.android"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.   
  7.       
  8.   
  9.     <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="MyApp"  >  
  10.       
  11.         <activity android:name=".TestAndroid"  
  12.                   android:label="@string/app_name">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.                 <category android:name="android.intent.category.LAUNCHER" />  
  16.             </intent-filter>  
  17.         </activity>  
  18.           
  19.         <activity android:name=".otherActivity">   
  20.         </activity>   
  21.   
  22.     </application>  
  23. </manifest>  

这里尤其要注意的一点就是:MyApp这个类要在manifest中进行注册,其实就是加上了一行代码

android:name="MyApp"

但是这个是必须的!


这篇关于Android Application 创建全局变量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1071076

相关文章

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

MySQL 临时表创建与使用详细说明

《MySQL临时表创建与使用详细说明》MySQL临时表是存储在内存或磁盘的临时数据表,会话结束时自动销毁,适合存储中间计算结果或临时数据集,其名称以#开头(如#TempTable),本文给大家介绍M... 目录mysql 临时表详细说明1.定义2.核心特性3.创建与使用4.典型应用场景5.生命周期管理6.注

Spring Boot项目如何使用外部application.yml配置文件启动JAR包

《SpringBoot项目如何使用外部application.yml配置文件启动JAR包》文章介绍了SpringBoot项目通过指定外部application.yml配置文件启动JAR包的方法,包括... 目录Spring Boot项目中使用外部application.yml配置文件启动JAR包一、基本原理

MySQL的触发器全解析(创建、查看触发器)

《MySQL的触发器全解析(创建、查看触发器)》MySQL触发器是与表关联的存储程序,当INSERT/UPDATE/DELETE事件发生时自动执行,用于维护数据一致性、日志记录和校验,优点包括自动执行... 目录触发器的概念:创建触www.chinasem.cn发器:查看触发器:查看当前数据库的所有触发器的定

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域