Application类



在Android中,启动一个应用,首先会初始化Application,然后再通过它检查AndroidManifest.xml清单文件,选择须要首先启动的Activity。

在Activity中能够使用getApplication()方法获得该Application的实例,使用它就能够获得当前应用的主题、资源文件里的内容等,而且我们能够通过它来加入自己的全局属性,如User。比如开发一个游戏,须要保存分数,那么我们就能够继承Application。

首先,先写个Application的子类:

import android.app.Application;
public class GameApplication extends Application {
private int score;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}

然后在manifest.xml文件里面改动

<application android:name=".GameApplication" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DemoActivity"
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="ResultActivity"></activity>
</application>

注意到加入了android:name=".GameApplication" 。

改动完了以后,再往下看:

public class DemoActivity extends Activity {
public Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
((GameApplication)getApplication()).setScore(100);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(DemoActivity.this, ResultActivity.class);
startActivity(intent);
}
});
}
}

在这个activity里面设置了分数,我们能够在别的activity里面取出来:

public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
TextView tv=(TextView)findViewById(R.id.tv);
int score=((GameApplication)getApplicationContext()).getScore();
tv.setText("你的成绩是:"+score);
}
}

这仅仅是个简单的样例,当然,想要完毕以上功能,使用intent传值就能够了,这样还显得麻烦,可是,假设有非常多activity,使用这样的方法就会发现非常实用,是不是使用sharepreference也能够完毕类似功能呢,能够,可是,效率方面就要比这个差非常多了,sharepreference主要是用来存储数据,你能够退出程序时把所须要保存的简单数据保存到sharepreference里面,当然复杂的数据,还得使用sqllite。

继承Application实现Android数据共享的更多相关文章

  1. 继承Application管理生命周期

    继承Application实现Android数据共享 http://www.jianshu.com/p/75a5c24174b2 jessyan提出一个思路,用Application + 接口来管理扩 ...

  2. Android数据共享

    Android数据共享 在Android应用程序开发的过程中,借助Bundle类对象来传递数据并不是在所有场景下都适用,就那简单的Intent类对象来说,就不能put进Bundle类对象中.当然不能否 ...

  3. Android Application Fundamentals——Android应用程序基础知识

    Application Fundamentals--应用程序基础知识 Key classes--关键类 Activity Service BroadcastReceiver ContentProvid ...

  4. android 数据共享

    android数据共享的各种部件中的应用是最重要的3途径: 第一.使用Application子类来实现数据共享. 例如,请看下面的例子: /**  * @author YangQuanqing 特征: ...

  5. 继承Application以实现全局资源共享

    原文地址:http://www.cnblogs.com/Dentist/p/Mr_Dentist_.html 每个程序运行时会创建一个Application类的对象且仅有一个.在app结束时这个App ...

  6. Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication 解决办法

    相信很多人都遇到过这个问题,用Android Studio正在运行程序的时候,突然不知道什么原因,报一个找不到application或者找不到activity的错误(java.lang.ClassNo ...

  7. javafx 继承Application打开

    前段时间需要用到javafx的Application来写一些图形界面之类的东西,但是run了之后eclipese不会去运行它,很纳闷,然后看了一下run as发现是没有main入口 其实加上一个mai ...

  8. 小米手机无法打开程序报错Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication的解决办法

    打开studio的setting 然后 Preferences -> Build, Execution, Deployment -> Instant Run -> Enable In ...

  9. Template design pattern application in android

    The template method allow separate the generic method from a detail context(class) via inheritance A ...

随机推荐

  1. 用JS判断用户使用的是手机端还是pc端访问

    最近项目中用到一个应用,当访问同一个网站地址的时候,例如:www.xxx.com的时候,如果当前客户端是pc则跳转到专注于pc的部分,如果当前客户机是手机,则跳转到专注于手机的部分,秉承一贯的习惯,b ...

  2. 一維條碼編碼規則(1D Barcode)

    1.Code 39 條碼:又分 標準型Code 39 條碼(Standard Code 39):資料內容包含有0~9數字,A~Z英文字母,”+”,”-“,”*”,”/”,”%”,”$”,”.”以及sp ...

  3. Nginx 开启 debug 日志的办法

    译序:一般来讲,Nginx 的错误日志级别是 error,作为 Nginx 用户来讲,你设置成 info 就足够用了.         但有时有些难以挖掘的 bug,需要看到更详细的 debug 级别 ...

  4. 循环之while

  5. HDU 5091 线段树扫描线

    给出N个点.和一个w*h的矩形 给出N个点的坐标,求该矩形最多能够覆盖多少个点 对每一个点point(x.y)右边生成相应的点(x+w,y)值为-1: 纵向建立线段树,从左到右扫描线扫一遍.遇到点则用 ...

  6. 倒计时IE10+

    直接上代码,dome 里边有我做的列表倒计时(多个同时倒计时)下面是我做的例子,颜色可以自己设置的 <p name="daojishi" style="width: ...

  7. sql substring charindex

    substring(s1,int,int) 截取字符串,起始位置,截取个数,返回截取后的字符串 charindex(s1,s2)查找字符s1在s2中的位置.返回int位置 SELECT substri ...

  8. BZOJ 1455: 罗马游戏( 配对堆 + 并查集 )

    可并堆水题 --------------------------------------------------------- #include<bits/stdc++.h>   usin ...

  9. WCF Publisher/Subscriber 订阅-发布模式

    本博后续将陆续整理这些年做的一些预研demo,及一些前沿技术的研究,与大家共研技术,共同进步. 关于发布订阅有很多种实现方式,下面主要介绍WCF中的发布订阅,主要参考书籍<Programming ...

  10. W3C 、HTML 、CSS 发展介绍

    一.W3C W3C 指万维网联盟(World Wide Web Consortium),创建于1994年10月,由 Tim Berners-Lee (他是html的发明人)创建. W3C开始被创建的目 ...