在android开发过程中,我们可能存储一些全局的变量,最好在正在app的任何一个activity或者service中都可以访问到,这时我们可以使用application。

我们的一个应用就叫application,那么应该很好理解一个应用里面只会存在一个单例的application,也不难想到用这个在存储全局变量,那么到底是怎么存储呢?

首先,我们创建一个Application,继承android.app.Application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<span style="font-size:18px;">package com.podongfeng.firstapplication.app;
 
import android.app.Application;
 
public class MyApplication extends Application {
     
    private Integer allViewInteger;
 
    public Integer getAllViewInteger() {
        return allViewInteger;
    }
 
    public void setAllViewInteger(Integer allViewInteger) {
        this.allViewInteger = allViewInteger;
    }
 
}
</span>

然后,在AndroidManifest.xml去声明这个Application,有点类似于声明Activity。

其实,在AndroidManifest.xml中肯定会存在一个系统声明的Application,类似于这样:

1
2
3
4
5
6
7
8
9
10
11
<span style="font-size:18px;">
         
            <intent-filter>
                 
 
                <category android:name="android.intent.category.LAUNCHER">
            </category></action></intent-filter>
        </activity>
         
        </activity>
    </application></span>

那么,怎么替换成为我们自己的application呢?

其实,只要在application标签中增加android:name属性指向我们自定义的application就可以了:

1
2
3
4
5
6
7
8
9
10
11
<span style="font-size:18px;">
         
            <intent-filter>
                 
 
                <category android:name="android.intent.category.LAUNCHER">
            </category></action></intent-filter>
        </activity>
         
        </activity>
    </application></span>

OK,这样的话,我们就可以在activity中使用getApplicationContext()来获取这个我们自定义的Application了。

等等,是不是局的这样还不是特别的方便,如果写了一些共用的java方法,为了代码的良好复用,没有放在activity里面呢?

通过一个参数把context传过去,然后再用context去获取Application?

这样做当然可以,不过,既然Application是单例的,我们很容联想到在单例的设计模式中使用getInstance方法来得到单例的对象。事实上,我们的MyApplication集成了Application,可以直接覆写onCreate方法,在Application被创建时把对象赋值给一个静态成员变量,这样,就可以任何地方通过MyApplication的静态方法去获取这个单例了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<span style="font-size:18px;">package com.podongfeng.firstapplication.app;
 
import android.app.Application;
 
public class MyApplication extends Application {
     
    private static MyApplication myApplication = null;
     
    public static MyApplication getMyApplication() {
        return myApplication;
    }
     
    private Integer allViewInteger;
 
    public Integer getAllViewInteger() {
        return allViewInteger;
    }
 
    public void setAllViewInteger(Integer allViewInteger) {
        this.allViewInteger = allViewInteger;
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        myApplication = this;
    }
 
}
</span>

OK,我们目前只在里面写了一个可用的全局变量allViewInteger,这仅仅用来说明问题就足够了,想存什么就存什么,获取起来也很方便,最后附上在2个activity中set和get的一个全局变量的样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<span style="font-size:18px;">package com.podongfeng.firstapplication;
 
import com.podongfeng.firstapplication.app.MyApplication;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity implements OnClickListener {
     
    private Button nextBtn;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApplication myApplication = MyApplication.getMyApplication();
        myApplication.setAllViewInteger(100);
        nextBtn = (Button) findViewById(R.id.btn_next);
        nextBtn.setOnClickListener(this);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), SecActivity.class);
        startActivity(intent);
    }
}
</span>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<span style="font-size:18px;">package com.podongfeng.firstapplication;
 
import com.podongfeng.firstapplication.app.MyApplication;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class SecActivity extends Activity {
     
    private TextView textView = null;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activiry_sec);
        textView = (TextView) findViewById(R.id.tv_sec);
        textView.setText(String.valueOf(MyApplication.getMyApplication().getAllViewInteger()));
    }
 
}
</span>
 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

最新文章

  1. 百度MIP移动页面加速——不只是CDN
  2. wp8开发笔记之应用程序真机发布调试
  3. Android之自定义控件-下拉刷新
  4. clean之后R文件消失
  5. winscp私钥如何生成
  6. UITableViewCell 设置圆角
  7. CAD出图
  8. NFS安装过程
  9. golang作为server向android提供数据服务
  10. ACCESS-如何多数据库查询(跨库查询)
  11. UNIX环境高级编程——进程管理和通信(总结)
  12. 调试webpack配置文件
  13. TypeError: &#39;_io.TextIOWrapper&#39; object does not support item assignment
  14. 图片懒加载 echo.js
  15. 机器学习课程-第8周-聚类(Clustering)—K-Mean算法
  16. 在Win32程序中显示Dos调试窗口
  17. PHP swoole process的使用
  18. OneZero第三周——预完成功能点统计
  19. oop思维意识,类 模块命名空间,类扩展之继承 、组合、mixin三种模式
  20. tomcat启动报错,找不到相应的 queue,从而引发内存泄漏

热门文章

  1. jeecg项目将workbook 的Excel流添加到zip压缩包里导出
  2. django groupby 用法
  3. Java本周总结1
  4. 第十四周总结 Io之文件流
  5. 004 gcc 编译 C/C++ 默认使用哪个标准
  6. 将从model中获得的数据传到js函数中
  7. Oracle Replace函数的简单使用
  8. vue点击除了某组件本身的其它地方, 隐藏该组件的方法
  9. uboot常用命令
  10. 【IO流】FileInputStream FileOutputStream BufferInputStream BufferOutputStream