你好,游客 登录 注册 搜索
背景:
阅读新闻

Android应用程序变量(Application级别)

[日期:2012-06-02] 来源:Linux社区  作者:Linux [字体:大 中 小]

通过这种方式,DataStoreClass类可能会像Activity一样在某些时候被Android系统强行销毁。所以在应用程序运行期间,DataStoreClass的值可能会丢失,或得到一些您不想要的值。 FROM: helloandroid
///////////////////////////////////////////////////////////////////////////////
//使用不罗嗦了,注意红色的地方就可以了
首先:

  1. import android.app.Application;   
  2.   
  3. /**  
  4.  * 全局变量类,不需要用Bundle传来传去了  
  5.  * @author admin  
  6.  *  
  7.  */  
  8.   
  9. public class BaseApplication extends Application {   
  10.   
  11.     private String text = "My world";   
  12.          
  13.     public String getText() {   
  14.         return text;     
  15.     }     
  16.      
  17.     public void setText(String text) {   
  18.         this.text = text;     
  19.     }     
  20.        
  21. }  

其次:
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".BaseApplication">
...
</application>
再次:在activity中
BaseApplication app=null;
app = ((BaseApplication) getApplicationContext());
app.getText();

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款