android中使用Application

在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

转载于:https://www.cnblogs.com/rabbit-bunny/p/4182691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值