惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
GbyAI
GbyAI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园_首页
D
Docker
S
Security @ Cisco Blogs
K
Kaspersky official blog
爱范儿
爱范儿
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Troy Hunt's Blog
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Hacker News
The Hacker News
美团技术团队
S
SegmentFault 最新的问题
L
Lohrmann on Cybersecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
Google Online Security Blog
Google Online Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
Vercel News
Vercel News
The Cloudflare Blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
AI
AI
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
Scott Helme
Scott Helme
S
Schneier on Security
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
S
Securelist
IT之家
IT之家
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - xin36933

浅析android OCR文字识别 Android中使用代码截图的各种方法总结 Android通过tcpdump抓包 Android 开机自动启动服务 SqlServer表死锁的解决方法 数据库 的事务日志已满。若要查明无法重用日志中的空间的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列。 修改Android工程版本 android利用剪切板来实现数据的传递 ANDROID开发之SQLite详解 浅析SQLite数据库开发常用管理工具 android DDMS 连接真机(己ROOT),用file explore看不到data/data文件夹的解决办法 Android手机获取手机唯一识别号(转) android 程序时时获取logcat信息 Android adb shell启动应用程序的方法 android打电话、发短信实现 安卓4.0/4.1/4.2手机怎么打开USB调试模式 Android中的DDMS进行调试 Runtime.getRuntime().exec执行阻塞问题解决 GC_EXTERNAL_ALLOC freed 与 GC_EXPLICIT freed 是什么?
Android全局变量的定义与使用
xin36933 · 2014-02-28 · via 博客园 - xin36933

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

  1. class MyApp extends Application {  
  2.   
  3.   private String myState;  
  4.   
  5.   public String getState(){  
  6.     return myState;  
  7.   }  
  8.   public void setState(String s){  
  9.     myState = s;  
  10.   }  
  11. }  
  12.   
  13. class Blah extends Activity {  
  14.   
  15.   @Override  
  16.   public void onCreate(Bundle b){  
  17.     ...  
  18.     MyApp appState = ((MyApp)getApplicationContext());  
  19.     String state = appState.getState();  
  20.     ...  
  21.   }  
  22. }  


然后再manifest中添加应用:

  1. <application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name">  
  2.         <activity android:name=".ClickableListItemActivity"  
  3.                   android:label="@string/app_name">  
  4.             <intent-filter>  
  5.                 <action android:name="android.intent.action.MAIN" />  
  6.                 <category android:name="android.intent.category.LAUNCHER" />  
  7.             </intent-filter>  
  8.         </activity>  
  9.   
  10.     </application>  

注意:

不用新建<application />,在原有基础上添加内容:android:name=".your_App_Name"

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

参考文章:http://androidstudy.iteye.com/blog/776127

http://www.eoeandroid.com/thread-163-1-1.html