[Android]使用全局变量传递数据

   在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>

你可能感兴趣的:(ios,全局变量)