标签:application android开发 全局变量 单例
在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,类似于这样:
<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标签中增加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>
等等,是不是局的这样还不是特别的方便,如果写了一些共用的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>
<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>
标签:application android开发 全局变量 单例
原文地址:http://blog.csdn.net/podongfeng_/article/details/42066969