Android---8---Intent及使用Intent傳遞資料

來源:互聯網
上載者:User

標籤:android   intent   資料   布局   介面   

Intent 是 Android 程式中各組件之間進行互動的一種重要方式,它不僅可以指明當前組

件想要執行的動作,還可以在不同組件之間傳遞資料。Intent 一般可被用於啟動活動、啟動

服務、以及發送廣播等情境

Intent是一種運行時綁定機制,它能在程式啟動並執行過程中串連兩個不同的組件。通過intent 程式可以向Android表達某種請求或者意願,Android會根據意願的內容選擇適當的組件來請求。

在這些組件之間的通訊中,主要是由Intent協助完成的。Intent負責對應用中一次操作的動作、動作涉及資料、附加資料進行描述,Android則根據此Intent的描述,負責找到對應的組件,將Intent傳遞給調用的組件,並完成組件的調用。

因此,Intent在這裡起著一個媒體中介的作用,專門提供自檢互相調用的相關資訊,實現調用者與被調用者之間的解耦。

例如:在一個連絡人維護的應用中,當我們在一個連絡人清單螢幕上,點擊某個連絡人後,希望能夠跳出此連絡人的詳細資料螢幕。

 

為了實現這個目的,listActivity需要構造一個Intent,這個Intent用於告訴系統,我們要做查看的動作,此動作對應的查看對象是“某連絡人”,然後調用startActivity(Intent intent),將構造的Intent傳入,系統會根據此Intent中的描述,到ManiFest中找到滿足此Intent要求的Activity,系統會調用找到的Activity,即為detailActivity,最終傳入Intent,detailActivity則會根據此Intent中的描述,執行相應的操作。

 

首先,是布局介面,這裡只有一個按鈕,當點擊按鈕的時候,會蹦到另一個介面來顯示連絡人的詳細資料。

先建立MainActivity:


package com.example.intentdemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * 通過Intent來傳遞資料 * 例如:手機連絡人,當點中某人的時候,會顯示這個人的詳細資料 * 當然是通過另一個介面來完成的 * @author Caesar * */public class MainActivity extends Activity implements OnClickListener{private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button  = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//建立一個Intent,表示會從MainActivity跳轉到OtherActivity活動//第一個參數表示啟動活動的上下文,第二個參數表示將要跳轉的目標活動,接收.class類型Intent intent = new Intent(MainActivity.this, OtherActivity.class);//為Intent傳入資料intent.putExtra("name", "張三");intent.putExtra("age", 15);intent.putExtra("address", "北京");intent.putExtra("tel", "121212121212");//啟動活動startActivity(intent);}}

因為程式要跳轉到OtherActivity,所以應該在建立一個OtherActivity的類,讓它繼承Activity,複寫其中的onCreate方法,並載入other布局,現在還沒有other布局,需要先去建立。

因為OtherActivity主要用於顯示資訊,所以other布局中只有一個TextVIew用於顯示資訊。

Other.xml


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

OtherActivity.java


package com.example.intentdemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class OtherActivity extends Activity {private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other);textView = (TextView) findViewById(R.id.msg);//得到意圖Intent intent = getIntent();//擷取intent中的資訊int age = intent.getIntExtra("age", 0);String name = intent.getStringExtra("name");String address = intent.getStringExtra("address");String tel = intent.getStringExtra("tel");String Info = "name-->" + name + "\n" + "age-->" + age + "\n"+ "address-->" + address + "\n" + "TEL:" + tel;//顯示該資訊textView.setText(Info);}}


我們需要在註冊表中為OtherActivity添加許可權:

在application 中,第一個活動的下面添加:<activity android:name=".OtherActivity" >   

 

OK了 ,運行一下:







OK,成功。

Android---8---Intent及使用Intent傳遞資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.