Android 通过Application 传递数据

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

时间: 2024-12-20 16:06:35

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

Android 使用全局变量传递数据

使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量.申明后,全局调用.只有当内存被清理后,才被销毁.否则一直可以调用. 还是使用点击一个button,传递一个数据到另一个页面中显示. 首先,新建工程,然后,打开mainActivity.xml文件,然后,添加button. <Button android:id="@+id/button" android:layout_width="wrap_content" android:la

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

一.剪切板的使用介绍 1. 剪切板对象的创建 使用剪切板会用到,ClipboardManager对象,这个对像的创建不可以使用构造方法,主要是由于没有提供public的构造函数(单例模式),需要使用Activity.getSystemService(Context.CLIPBOARD_SERVICE)获取该对象.  2. 对象调用的主要方法介绍 在Android-11(Android 3.0)版本之前,利用剪切板传递数据使用setText()和getText()方法,但是在此版本之后,这两个方法

android使用全局变量传递数据

android中Application是用来保存全局变量的,在package创建的时候就存在了,到所有的activity都被destroy掉之后才会被释放掉.所以当我们需要全局变量的时候只要在application中去实现,通过调用Context的getApplicationContext或者Activity的getApplication方法来获得一个Application对象,就可以设置或读取全局变量的值. 启动Application时,系统会创建一个PID,即进程ID,所有的Activity

如何在不同的android应用之间传递数据?

如果你做android的framework的开发,往往会涉及到不同应用之间的配合,需要读取或修改某个共享的数据. 这时你可以用到下面两个类来解决问题,这两个类的使用在framework也有经常出现. SystemProperties,Settings.System 1.SystemProperties的使用. 在framework中,SystemProperties,常用来读取或修改系统属性,使用该方法可以给系统添加属性. 其中读取方法为Systemproperties.get(name),修改

android之bundle传递数据--两个activities之间

登陆页面布局:layout/activity_login.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height=&q

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

android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivity.this,OtherActivity.class); intent.putExtra("name", "使用Intent传递数据"); startActivity(intent); 然后再目标activity中接收数据: Intent intent=getInten

struts2在action中获取request、session、application,并传递数据

如果只是通过request.session.application传递数据,则不需要获取相应的对象也可以传递数据,代码如下: ScopeAction.java: package com.itheima.action; import com.opensymphony.xwork2.ActionContext; public class ScopeAction { public String execute() { /* * 我们这里不通过字段(添加setXXX.getXXX方法)传递数据,我们想着

使用全局变量在Activity之间传递数据

在Activity之间数据传递中还有一种比较实用的方式,就是全局对象,使用J2EE的读者来说都知道Java Web的四个作用域,这四个作用域从小到大分别是:Page.Request.Session和Application,其中Application域在应用程序的任何地方都可以使用和访问,除非是Web服务器停止,Android中的全局对象非常类似于Java Web中的Application域,除非是Android应用程序清除内存,否则全局对象将一直可以访问. 下面通过例子来分享一下它的实现: 建立

Android -- 启动Service并传递数据

本文主要记录Activity传递数据到Service. 1.效果图2.通过以上效果图,可以看出activity页面的数值改变,相应后台service输出的数值也跟着改变.3.核心代码如下,看代码中的38行,使用Intent作为载体,装载activity页面上的数据. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41