码迷,mamicode.com
首页 > 移动开发 > 详细

Android 通过Application 传递数据

时间:2014-10-01 12:02:41      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:android   http   io   os   ar   java   数据   art   c   

</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 传递数据

标签:android   http   io   os   ar   java   数据   art   c   

原文地址:http://blog.csdn.net/haifengzhilian/article/details/39717003

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!