Android 通過Application 傳遞資料,androidapplication

來源:互聯網
上載者:User

Android 通過Application 傳遞資料,androidapplication

</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怎在Activity與Service之間傳遞資料

1、未經處理資料類型:在Activity/Servier之間傳遞臨時性的未經處理資料,可以使用Intent的putExtras方法來傳遞資料。若傳遞的資料需要長久儲存,則使用SharedPreference類來完成。2、傳遞對象。當在Activity/Servier之間傳遞不需要長久儲存的對象時,可以使用以下幾種途徑:(1)通過Application類,每個Android應用程式都有一個Application類。當你在程式的AndroidManifest.xml中給Application設定一個名字時,你的程式中就必須有一個Application的子類。這個Application子類會被Android自動執行個體化,並且是一個全家性的類,它的生命週期和程式的生命週期相同,你可以把一些全域性的對象儲存在Application類中。Application類可以通過getApplication()獲得。(2 通過HashMap of WeakReferences傳遞對象。當一個Activity需要向另外一個Activity傳遞對象時,可以使用一個關鍵字把對象存在一個HashMap中,並把這個關鍵字通過Internt的Extras發給目標Activity,目標Activity接到該關鍵字後使用該關鍵字把對象沖HashMap中取出。
 
android activity 間傳遞自訂類型資料怎傳輸

可以用全域變數來解決。
1、自訂一個Application的衍生類別MyApplication,然後在Manifest中指定MyApplication為你的application name。
2、在MyApplication中定義你要傳遞資料類型對應的變數。然後增加一個成員函數,用於獲得該變數的引用。
3、當然對MyApplication中對應變數的讀寫要做好同步。
---------------------------------------------------------------------
更多疑問解答,請關注@安卓互助平台 新浪微博
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.