</pre><pre>
package com.example.ApplicationTest;

import android.app.Application;

/**
* Created by chang on 14-10-1.
*/
public class App extends Application {
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String name; @Override
public void onCreate() {
super.onCreate(); setName("张三");
}
}
package com.example.ApplicationTest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MyActivity extends Activity { private Button btnLaunch = null;
private App myApp; /**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); btnLaunch = (Button)this.findViewById(R.id.btnLaunch);
myApp = (App)this.getApplication();
myApp.setName("hello"); btnLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MyActivity.this,OtherActivity.class);
startActivity(intent);
}
}); }
}
package com.example.ApplicationTest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by chang on 14-10-1.
*/
public class OtherActivity extends Activity {
private App myApp;
private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.other);
textView = (TextView)findViewById(R.id.showMsg);
myApp = (App)getApplication(); textView.setText(myApp.getName());
}
}
<?

xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ApplicationTest"
android:versionCode="1"
android:versionName="1.0"> <uses-sdk android:minSdkVersion="19"/>
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:name=".App">
<activity
android:name="MyActivity"
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=".OtherActivity" > </activity>
</application>
</manifest>

Android 通过Application 传递数据的更多相关文章

  1. 通过Application传递数据代码

    使用Application传递数据步骤如下:创建新class,取名MyApp,继承android.app.Application父类,并在MyApp中定义需要保存的属性     在整个Android程 ...

  2. Android 使用全局变量传递数据

    使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量.申明后,全局调用.只有当内存被清理后,才被销毁.否则一直可以调用. 还是使用点击一个button,传递一个数据到另一 ...

  3. Android--通过Application传递数据

    在整个Android程序中,有时需要保存某些全局的数据(如:用户信息),方便在程序的任何地方调用.在Activity之间数据传递中有一种比较使用的方式,就是全局对象,使用过J2EE的都应该知道Java ...

  4. 通过Application传递数据

    1:通过Application传递数据 假如有一个Activity A, 跳转到 Activity B ,并需要推荐一些数据,通常的作法是Intent.putExtra() 让Intent携带,或者有 ...

  5. Android剪切板传递数据传递序列化对象数据

    一.剪切板的使用介绍 1. 剪切板对象的创建 使用剪切板会用到,ClipboardManager对象,这个对像的创建不可以使用构造方法,主要是由于没有提供public的构造函数(单例模式),需要使用A ...

  6. Android 使用意图传递数据

    使用意图传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <But ...

  7. android使用全局变量传递数据

    android中Application是用来保存全局变量的,在package创建的时候就存在了,到所有的activity都被destroy掉之后才会被释放掉.所以当我们需要全局变量的时候只要在appl ...

  8. Android与NativeC传递数据不正确问题

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Android studio 2.3.3 这两天一直在调试一个BUG,具体为通过 NativeC 来处理上层Android ...

  9. android中使用Intent在activity之间传递数据

    android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...

随机推荐

  1. 图论trainning-part-1 G. Stockbroker Grapevine

    G. Stockbroker Grapevine Time Limit: 1000ms Memory Limit: 10000KB 64-bit integer IO format: %lld     ...

  2. zoj 1760 Doubles

    Doubles Time Limit: 2 Seconds      Memory Limit: 65536 KB As part of an arithmetic competency progra ...

  3. ListView虚拟模式封装

    public class ListViewAH : ListViewEx { #region 虚拟模式相关操作 ///<summary> /// 前台行集合 ///</summary ...

  4. 【bzoj1059】[ZJOI2007]矩阵游戏 二分图最大匹配

    题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换 ...

  5. [BZOJ2733] [HNOI2012]永无乡(并查集 + 线段树合并)

    传送门 一看到第k大就肯定要想到什么权值线段树,主席树,平衡树之类的 然后就简单了 用并查集判断连通,每个节点建立一颗权值线段树,连通的时候直接合并即可 查询时再二分递归地查找 时间复杂度好像不是很稳 ...

  6. POJ 1502 MPI Maelstrom [最短路 Dijkstra]

    传送门 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5711   Accepted: 3552 ...

  7. ls 不是内部或外部命令

    在C:\windows目录下新建一个文件 命名为 ls.bat 打开编辑这个文件 输入: @echo off dir 这两句保存即可.

  8. SELinux 服务检查与关闭

    查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 SELinux status:         ...

  9. linux fork()

    一. linux下C语言可以用fork()建立子进程.fork函数返回两个值,对于子进程,返回0; 父进程,返回子进程ID. 所以用if(fork()==0)      {子进程执行的代码段:}els ...

  10. Codeforces D. Iahub and Xors

    题目大意:给定一个N*N的区间,1:对(x0,y0,x1,y1)每个直 都xor v: 2: 求(x0,y0,x1,y1)区间的 sum xor: http://codeforces.com/blog ...