在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;
}
@Override
public 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;
@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>
<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>