继承Application实现Android数据共享

时间:2022-12-09 13:41:23

     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&period;android&period;tools&period;fd&period;runtime&period;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&period;android&period;tools&period;fd&period;runtime&period;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. getLovParameter

    else if (pageContext.isLovEvent()) { StHelper.handleLovEvent(pageContext, webBean); } public static ...

  2. js和jquery获取子元素

    <ul id="nav"> <li></li> <li> <ul> <li></li> < ...

  3. MST&lowbar;prim

    刚刚发了mst 的kruskal,现在再来一发,说一说prim咯. prim适用于稠密图. 与kruskal不同,prim是从一个点开始,不断加入新的点直至连通所有点. 讲讲prim的过程,我们假定有 ...

  4. delphi中的Label控件背景透明

    Label1.Transparent:=true;你在它的属性窗口把它的Transparent属性改成TRUE就行了 来自为知笔记(Wiz)

  5. hdu4491 Windmill Animation &lpar;几何&rpar;

    Windmill Animation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. Java实现敏感词过滤&lpar;转&rpar;

    敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有必要的.前段时间我一个朋友(马上毕业,接触编程不久)要我帮他看一个文字过滤的东西,它说检索效率非常慢.我把它程序拿过来 ...

  7. mac 内置PHP配置多站点

    1.修改/private/etc/hosts 文件,建议用编辑器打开 最后一行加入你的网站名称(自定义),参考如下: 127.0.0.1 www.MyObj.com   2.修改/private/et ...

  8. 大数据开发实战:Hive优化实战1-数据倾斜及join无关的优化

    Hive SQL的各种优化方法基本 都和数据倾斜密切相关. Hive的优化分为join相关的优化和join无关的优化,从项目的实际来说,join相关的优化占了Hive优化的大部分内容,而join相关的 ...

  9. uva-10392-因数分解

    #include<stdio.h> #include<iostream> #include<queue> #include<memory.h> #inc ...

  10. php 下载生成word文件

    方案一 $html2 ='数字'; header("Content-type: application/octet-stream"); header("Accept-Ra ...