在Activity之间传递数据,有一种比较实用的技术,就是通过全局变量。熟悉iOS开发的童鞋都知道,iOS可以采用[UIApplication shareApplication],[NSUserdefault standerUserDefault],[NSNotificationCenter defaultCenter] 等等去存储和读取全局的变量。在Android当中,我们也可以通过Application对象实现相同的功能。
  1. 新建一个Andrdoid 工程,名字为:application
  2. 在生成的main_acitivity.xml文件中添加一个Button,用于点击后跳转到另外一个Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >    <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="使用application传递数据"/></LinearLayout>

3、在Main.java当中获取这个按钮,并且声明一个意图,然后获取全局对象的值,启动此意图后将全局变量的值传递到另一个Acitivity。

package com.example.application;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.os.Build;public class Main extends ActionBarActivity {    private Button button;    private MyApp myApp;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                /**获取全局的应用**/                myApp = (MyApp)getApplication();                // 修改之后的名称                myApp.setName("new value");                // 新建意图                Intent intent = new Intent(Main.this,other.class);                // 启动意图                startActivity(intent);            }        });    }}

4、 新建myApp继承自Application,添加一个public属性name,并为name添加setter和getter方法。并且添加onCreate()方法,使这个全局对象创建的时候设置name的初始值。API文档中,对于onCreate()方法是这样描述的:
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.

package com.example.application;import android.app.Application;public class MyApp extends Application {    public String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public void onCreate() {        // TODO Auto-generated method stub        super.onCreate();        setName("zhangsan");    }}

5、新建一个Other.xml布局文件, 添加TextView用来接收从main Activity中传递过来的值

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" ><TextView android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>

6、新建Other.java,获取Other.xml中的TextView对象,获取MyApp全局对象,从此对象中通过getName()方法获取到name的值。

package com.example.application;import android.app.Activity;import android.app.Application;import android.os.Bundle;import android.widget.TextView;public class other extends Activity {    private MyApp myApp;    private TextView textView;    public other() {        // TODO Auto-generated constructor stub    }    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.other);        textView = (TextView)this.findViewById(com.example.application.R.id.msg);        /**获取全局的应用**/        myApp = (MyApp)getApplication();        textView.setText("------>msg value = " + myApp.getName());    }}

7、注意,这个步骤非常重要,首先要在AndroidManifest.xml文件中,配置Activity,其次,在Application节点中新增name属性:name = “.MyApp”。注意前面的点别忘了写。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.application" android:versionCode="1" android:versionName="1.0" >    <uses-sdk  android:minSdkVersion="8" android:targetSdkVersion="19" />    <!-- 在application标签添加全局应用的名称 -->    <application  android:name=".MyApp" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >        <activity  android:name="com.example.application.Main" 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 -->        <activity android:name=".other"></activity>    </application></manifest>

更多相关文章

  1. Android(安卓)orm框架Sugar1.4源码(映射篇)
  2. pandaboard ES学习之旅——1 制作烧写SD卡
  3. Android(安卓)UI开发第三十篇――使用Fragment构建灵活的桌面
  4. Android:如何给 ScrollView 添加滑块滚动条
  5. Android(安卓)4.4 打印探究
  6. Android中“android:elevation”设置阴影,在真机上没有效果的问题
  7. 获取或同步MeeGo和Android项目源码的脚本[修改稿]
  8. Android(安卓)获取短信会话列表
  9. Android官方开发文档Training系列课程中文版:使用Fragment构建动

随机推荐

  1. android常用开源库volly下载
  2. 解决android8.0无法启动activity报错问题
  3. android studio 2.0Stable (正式版下载)
  4. Android中PopupWindow的用法(位置、动画
  5. Android(安卓)使用 adb命令 远程安装apk
  6. android 客户端无法解析php服务端返回的j
  7. android studio 运行程序提示"adb not re
  8. android摄像头采集和预览-第一种方法
  9. 【Android】解决sdcard not found 问题
  10. Android bluetooth介绍(二): android 蓝牙代