Android App全局变量Application

来源:互联网 发布:php最好的语言 梗 编辑:程序博客网 时间:2024/05/29 19:26

多个Activity都是一个APP进程的不同UI线程,既然线程之间数据都是共享的,为何不用全局变量来实现变量、对象之间的共享,非要用Serializable和Parcel这种内存复制的方式呢?I like the former!


在Android中程序的开发都知道。在Activity、等件之间传递数据(尤其是复杂类型的数据)很不方便。一般可以使用Intent来传递可序列化或简单类型的数据。看下面的代

  Intent intent = new Intent(this, Test.class);
  intent.putExtra("param1""data1");
  intent.putExtra("intParam1"20);
  startActivity(intent);

  这样ok了。在当前Activity将两个值传到了Test中。但如果遇到不可序列化的数据,如Bitmap、InputStream等,intent就无能力了。因此,我很自然地会想到另外一方法,静态变量。如下面的代所示:
 

 public class Product extends Activity
  {
  public static Bitmap mBitmap;
  }

  于上面的代,其他任何可以直接使用Product中的mBitmap量。这么做很easy、也很cool,但却very very wrong。我千万不要以Davlik虚机的垃圾回收器会帮助我回收不需要的内存垃圾。事上,回收器并不可靠,尤其是手机上,是更加的不可靠。 因此,除非我要使自己的程序得越来越糟糕,否尽量static。
  注:如果常使用static的Bitmap、Drawable等量。可能就会抛出一个在Android系中非常著名的异常(以前budget单词一直不住什意思,自从常抛出个异常后,单词终熟于心了,)
  ERROR/AndroidRuntime(4958): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  如果不使用static,得有方法来代替它(尽管我很喜public static,我相信很多人也喜它,但了我的程序,建议还是忍痛割吧),那么这个新的解决方案就是本文的主就是Application Context,相当于Web程序的Application,它的生命周期和用程序一样长个我喜)。
  那么现在来看看如何使用Application Context。我可以通Context.getApplicationContext或Context.getApplication方法 Application Context。但要注意,我们获得的只是Context象,而更理想的方法是得一个象。ok,干就干,下面来定一个
 

 package net.blogjava.mobile1;
  import android.app.Application;
  import android.graphics.Bitmap;
  public class MyApp extends Application
  {
  private Bitmap mBitmap;
  public Bitmap getBitmap()
  {
  return mBitmap;
  }
  public void setBitmap(Bitmap bitmap)
  {
  this.mBitmap = bitmap;
  }
  }

  上面和普通的没什的不同。但该类Application的子了,就是使用Application Context的第一,定一个承自Application的。然后呢,就在中定任何我想使其全局存在的量了,如本例中的 Bitmap。下面需要一个重要的步骤,就是在<application>标签中使用android:name属性来指定,代 下:
  <application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name">
  </application?
  接下来的最后一就是向MyApp象中存入Bitmap象,或从MyApp象中取出Bitmap象了,存入Bitmap象的代如下:
 

 MyApp myApp = (MyApp)getApplication();
  Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
  myApp.setBitmap(bitmap);

  Bitmap象的代
 

 ImageView imageview = (ImageView)findViewById(R.id.ivImageView);
  MyApp myApp = (MyApp)getApplication();
  imageview.setImageBitmap(myApp.getBitmap());

  上面两段代可以在任何的、Activity中使用。

Android中在不同Activity中传递变量,通常使用Intent中Bundle添加量的操作方法。

   Intent intent = new Intent();
  intent.setClass(A.this, B.class);
  Bundle bundle = new Bundle();
  bundle.putString("Info""Information");
  intent.putExtras(bundle);
  startActivity(intent);
 
  不在多个Activity中常使用同一,使用BundleActivity都需要置一次。

  如想在整个用中使用,在java中一般是使用静态变量,而在android中有个更雅的方式是使用Application context。

  新建一个承自Application

  class MyApp extends
  Application {
  private String myState;
  public String getState() {
  return myState;
  }
  public void setState(String s) {
  myState = s;
  }
  }

  在AndroidManifest.xml的application加个name属性就可以了,如下面所示:

   android:name=".MyApp" android:icon="@drawable/icon"
  android:label="@string/app_name">
  使用时:

  class Blah extends

  Activity {

  @Override

  public void onCreate(Bundle b){

  ...

  MyApp appState = ((MyApp)getApplicationContext());

  String state = appState.getState();

  ...

  }

  }

  英文引用:http://stackoverflow.com/questions/708012/android-how-to-declare- global-variables

  The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context.

  --如想在整个用中使用,在java中一般是使用静态变量,而在android中有个更雅的方式是使用Application context。

  As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application.

  --Activity 都是Context,其包含了其运行的一些状android保了其是single instance的。

  The way to do this is to create your own subclass of android.app.Application,and then specify that class in the application tag in your manifest.Now Android will automatically create an instance of that class andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect):

  --方法是建一个属于你自己的android.app.Application的子,然后在manifest中申明一下 android就此建立一个全局可用的例,你可以在其他任何地方使用Context.getApplicationContext()方法例,取其中的状()。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大母指腱鞘炎好了之后又犯了怎么办 被蚊子咬了好大一个包怎么办 欠我钱的人跑了怎么办 下面旁边破了沙的疼怎么办 协助民警执法时质疑辅警身份怎么办 跑步每次腿落地小腿骨头疼怎么办 两年义务兵完了想继续当兵怎么办 保研联系老师说名额已满怎么办 在部队报函授不发毕业证怎么办 济南自考信息修改后原成绩怎么办 自考本科档案一直在自己手里怎么办 想考警校可身高差两公分怎么办 没读完初中现在想继续夜大怎么办 美国签证确认页姓和名写错了怎么办 单位不承认非法辞退说我旷工怎么办 工作了和同事在一起住宿舍怎么办 教会的事情商量是起冲突怎么办 转转购买的产品是坏的怎么办 二年级的学生反应太迟钝怎么办? 特别胖的人运动一半体力不支怎么办 怀孕了在胸透门口站了很久怎么办 自己觉的色弱但高考体检正常怎么办 高考体检不合格怎么办会影响录取吗 老婆起诉我离婚我不想离怎么办 中考结束了成绩差的学生怎么办 我儿了眼角模不好了怎么办 打了2次催产针没反应怎么办 高中体检学生隐私被同学看到怎么办 要出去旅游刚好遇上月经期怎么办 兵检的时候还在高中怎么办 人流后带上环20天白带很黄怎么办 武警义务兵训练的时候没合格怎么办 小孩考试考的不好·家长怎么办 怀孕了不小心碰了屁股疼怎么办 新密职教中心开学军训有点慌怎么办 房产证是士兵证办的退伍后怎么办 士兵证办的银行卡退伍了怎么办 看左上牙后引发上颌窦炎怎么办 老板克扣进件加班工资应该怎么办 医生给婴儿按嘴巴碰到喉咙痛怎么办 事故逃逸人死对方要钱太多怎么办