继承Application实现Android数据共享

     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数据共享

时间: 2024-07-30 16:58:51

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

继承Application管理生命周期

继承Application实现Android数据共享 http://www.jianshu.com/p/75a5c24174b2 jessyan提出一个思路,用Application + 接口来管理扩展每个activity的生命周期 这个接口有什么用呢? Application 提供有一个 registerActivityLifecycleCallbacks() 的方法,需要传入的参数就是这个 ActivityLifecycleCallbacks 接口,作用和你猜的没错,就是在你调用这个方法传入

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

Application Fundamentals--应用程序基础知识 Key classes--关键类 Activity Service BroadcastReceiver ContentProvider Intent In this document--在这篇文章中 Application Components--应用程序组件 Activating components: intents--激活组件:意图 Shutting down components--关闭组件 The manifest

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

每个程序运行时会创建一个Application类的对象且仅有一个.在app结束时这个Application才会消失.所以可以利用Application来进行一些数据的处理和储存.在多个activity或fragment之间交换数据比如一个app的设置,主题,用户信息等等.用它来实现全局变量比static更便于数据的集中管理. 例如: public class MyApp extends Application { private Person person; private String cor

Android数据共享

在Android应用程序开发的过程中,借助Bundle类对象来传递数据并不是在所有场景下都适用,就那简单的Intent类对象来说,就不能put进Bundle类对象中.当然不能否认,用Intent类对象来开启另一个Activity或者Service,并利用Bundle类对象来携带数据是个不错的形式,而且用途非常广泛,但不是万能的. 关于在程序中共享数据,方法有很多,下面来看看到底有哪些是比较常用,哪些又是比较符合面向对象和高效.安全的. 1.SharedPerferences 首先来看在同一个Mo

application/vnd.android.package-archive到底是什么

在拜读组里北大研二的安卓代码的时候, 读到登录前检测版本后更新的代码.发现了一个不懂的地方. void update() { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment .getExternalStorageDirectory(), m_appNameStr)), "application/vnd.android.package-ar

android 数据共享

android应用各个组件数据共享最基本的有3种方式: 第一,利用Application的子类来实现数据共享. 如下例子所示: /** * @author YangQuanqing 功能:实现数据共享 */ public class DataApplication extends Application { // private static FFtThread fftThread = null;// 傅立叶变换线程对象申明 private static ImageNum tempResult

Template design pattern application in android

The template method allow separate the generic method from a detail context(class) via inheritance Another advantage: 1.Inheritance implementation means that you will know the actual method implementation class before compilation,it is very big advan

Android实战简易教程-第三十枪(实例解析Application的用法)

一.Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系统的一些信息. Android系统自动会为每个程序运行时创建一个Application类的对象且只创建一个,所以Application可以说是单例(singleton)模式的一个类. 通常我们是不需要指定一个Application的,系统会自动帮我们创建,如果需要创建自己的Application

android Application类的详细介绍

在代码中经常看到application这个类,一直不知道这个是干什么用的,今天刚好有点时间,所以进行了详细的学习. 一.先对它的整体概念解释: 在android源码中对他的描述是; * Base class for those who need to maintain global application state. You can        * provide your own implementation by specifying its name in your        *