降价的反义词是什么:Android--全局变量 很好很强大

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 00:15:52
Android--全局变量 很好很强大
    博客分类:
  • Android——技术细节
AndroidAccess

 

As you know, each Activity is also a Context, which is information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance across your application.

The way to do this is to create your own subclass of android.app.Application, and then specify that class in the application tag in your manifest. Now Android will automatically create an instance of that class and make it available for your entire application. You can access it from any context using the Context.getApplicationContext() method (Activity also provides a method getApplication() which has the exact same effect):

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

 

This has essentially the same effect as using a static variable or singleton,

but integrates quite well into the existing Android framework.

Note that this will not work across processes (should your app be one of the rare ones that has multiple processes).

 

 

然后再manifest中添加应用:

Xml代码
  1. android:label="@string/app_name">

 

说明:

  1. 需添加的内容:android:name=".your_App_Name"

  2. 位置:当前activity所在的位置,(我刚开始以为需要新建一个

Android--全局变量 很好很强大 绿豆写的感谢信!很好很强大 某人的娘很好很强大! 化学元素周期表口诀。。。(很好很强大) 教你怎么从菜鸟变成电脑高手,很好很强大 教你一眼认出英语单词的意思,很好很强大。2。 教你一眼认出英语单词的意思,很好很强大…拿来分享啦~~~ - 日志 - zihan0926... 怎样去维护系统和优化系统资源!很好很强大 - Qzone日志 教你一眼认出英语单词的意思,很好很强大。。33 穿越小说最全排行榜 很好很强大 以后无聊的时候有的看了... 仅用U盘就可以去除 XP管理员密码(很好很强大) 很好很强大 不得不看风水说中堪称完美的户型(图附说明) 很好、很强大:心有多大,解放军的舞台就有多大绝对帅 ! 2011年非常创意的婚礼开场视频。。。(很好,很强大哦) 【经典】这四个故事写得太犀利了,很好很强大,我喜欢最后一个 【经典】这四个故事写得太犀利了,很好很强大,我喜欢最后一个....... 俄军公开中国核潜:远超西方的想像,很好很强大!! 学习英语小秘招——教你一眼认出英语单词的意思,很好很强大 山大威海分校学长考研沥血总结,很好很强大,特别适合威海考生 ps:转自山魂海 【毛坯房装修步骤/过程】很好很强大,怕丢了存下来~~~太有用了!赞! 关于gcc全局变量初始化 Android android Android