android中使用Application

来源:互联网 发布:买家申请退款淘宝介入 编辑:程序博客网 时间:2024/05/22 02:15

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

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

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

<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,类似于这样:

<span style="font-size:18px;"><application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".SplashActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".MainActivity"            android:label="@string/app_name" >        </activity>    </application></span>

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

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

<span style="font-size:18px;"><application        android:name="com.podongfeng.firstapplication.app.MyApplication"        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".SecActivity"            android:label="@string/app_name" >        </activity>    </application></span>

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

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

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

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

<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;}@Overridepublic void onCreate() {super.onCreate();myApplication = this;}}</span>

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

<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;@Overrideprotected 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);}@Overridepublic 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;}@Overridepublic 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);}@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(getApplicationContext(), SecActivity.class);startActivity(intent);}}</span>

<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;@Overrideprotected 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>


0 0