Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
?運行有問題或需要源碼請點贊關注收藏后評論區留言~~
顯示Intent和隱式Intent
Intent是各個組件之間的信息溝通的橋梁,既能在Activity之間溝通,又能在Activity與Service溝通,也能在Activtiy與Broadcast之間溝通,總而言之,Intent用于Android各組件之間的通信 它主要處理以下三部分工作
1:表明本次通信請求從哪里來? 到哪里去 要怎么走
2:發起方攜帶本次通信需要的數據內容
3:發起方若想判斷接收方的處理結果 Intent就要負責讓接收方傳回應答的數據內容
?
?1:顯示Intent 直接指定來源活動與目標活動 屬于精確匹配
2:隱式Intent 沒有明確地指定要跳轉的目標活動 只給出一個動作字符串讓系統自動匹配,屬于模糊匹配
下面實現一個簡單的調用系統撥號程序的例子 效果如下
?
?
?ActionUriActivity類代碼如下
package com.example.chapter04;import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View;import androidx.appcompat.app.AppCompatActivity;public class ActionUriActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_action_uri);findViewById(R.id.btn_dial).setOnClickListener(this);findViewById(R.id.btn_sms).setOnClickListener(this);}@Overridepublic void onClick(View v) {String phoneNo = "12345";if (v.getId() == R.id.btn_dial) { // 點擊了“跳到撥號頁面”按鈕Intent intent = new Intent(); // 創建一個新意圖intent.setAction(Intent.ACTION_DIAL); // 設置意圖動作為準備撥號Uri uri = Uri.parse("tel:" + phoneNo); // 聲明一個撥號的Uriintent.setData(uri); // 設置意圖前往的路徑startActivity(intent); // 啟動意圖通往的活動頁面} else if (v.getId() == R.id.btn_sms) { // 點擊了“跳到短信頁面”按鈕Intent intent = new Intent(); // 創建一個新意圖intent.setAction(Intent.ACTION_SENDTO); // 設置意圖動作為發短信Uri uri = Uri.parse("smsto:" + phoneNo); // 聲明一個發送短信的Uriintent.setData(uri); // 設置意圖前往的路徑startActivity(intent); // 啟動意圖通往的活動頁面}}}activity_action_uriXML文件代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:text="點擊以下按鈕將向號碼12345發起請求"android:textColor="@color/black"android:textSize="17sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_dial"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="跳到撥號頁面"android:textColor="@color/black"android:textSize="17sp" /><Buttonandroid:id="@+id/btn_sms"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="跳到短信頁面"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout></LinearLayout>向下一個Activity發送數據
Android引入了Bundle概念,可以把Bundle理解為超市的寄包柜或快遞收件柜,大小包裹由Bundle統一存取。Bundle內部用于存放信息的數據結構是Map映射,既可添加或刪除元素,還可以判斷元素是否存在?
Bundle對象操作各類型數據的讀寫方法說明如下圖
?
?
?ActSendActivity類代碼如下
package com.example.chapter04;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView;import com.example.chapter04.util.DateUtil;public class ActSendActivity extends AppCompatActivity implements View.OnClickListener {private TextView tv_send; // 聲明一個文本視圖對象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_act_send);// 從布局文件中獲取名叫tv_send的文本視圖tv_send = findViewById(R.id.tv_send);findViewById(R.id.btn_send).setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_send) {// 創建一個意圖對象,準備跳到指定的活動頁面Intent intent = new Intent(this, ActReceiveActivity.class);Bundle bundle = new Bundle(); // 創建一個新包裹// 往包裹存入名叫request_time的字符串bundle.putString("request_time", DateUtil.getNowTime());// 往包裹存入名叫request_content的字符串bundle.putString("request_content", tv_send.getText().toString());intent.putExtras(bundle); // 把快遞包裹塞給意圖startActivity(intent); // 跳轉到意圖指定的活動頁面}} }activity_act_sendXML文件代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_send"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:text="今天的天氣真不錯"android:textColor="#000000"android:textSize="17sp" /><Buttonandroid:id="@+id/btn_send"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="發送以上文字"android:textColor="#000000"android:textSize="17sp" /></LinearLayout>ActReceiveActivity類代碼如下
package com.example.chapter04;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.view.View; import android.widget.TextView;public class ActReceiveActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_act_receive);// 從布局文件中獲取名叫tv_receive的文本視圖TextView tv_receive = findViewById(R.id.tv_receive);findViewById(R.id.btn_receive).setOnClickListener(this);// 從上一個頁面傳來的意圖中獲取快遞包裹Bundle bundle = getIntent().getExtras();// 從包裹中取出名叫request_time的字符串String request_time = bundle.getString("request_time");// 從包裹中取出名叫request_content的字符串String request_content = bundle.getString("request_content");String desc = String.format("收到請求消息:\n請求時間為%s\n請求內容為%s",request_time, request_content);tv_receive.setText(desc); // 把請求消息的詳情顯示在文本視圖上}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_receive) {finish(); // 結束當前的活動頁面}} }activity_act_receiveXML文件代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_receive"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:textColor="#000000"android:textSize="17sp" /><Buttonandroid:id="@+id/btn_receive"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="知道了"android:textColor="#000000"android:textSize="17sp" /></LinearLayout>向上一個Activity返回數據
數據傳遞經常是相互的,上一個頁面不但把請求數據發送到下一個頁面,有時候還要處理下一個頁面的應答數據,所謂應答發生在下一個頁面返回到上一個頁面之際。如果只把請求數據發送到下一個頁面,上一個頁面調用startActivity即可。如果還要處理下一個頁面的應答數據,此時就得分多步處理 效果如下
?
?ActRequestActivity類代碼如下
package com.example.chapter04;import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import com.example.chapter04.util.DateUtil;public class ActRequestActivity extends AppCompatActivity implements View.OnClickListener {private String mRrequest = "你吃飯了嗎?來我家吃吧";private TextView tv_response; // 聲明一個文本視圖對象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_act_request);// 從布局文件中獲取名叫tv_request的文本視圖TextView tv_request = findViewById(R.id.tv_request);tv_request.setText("待發送的消息為:"+mRrequest);// 從布局文件中獲取名叫tv_response的文本視圖tv_response = findViewById(R.id.tv_response);findViewById(R.id.btn_request).setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_request) {// 創建一個意圖對象,準備跳到指定的活動頁面Intent intent = new Intent(this, ActResponseActivity.class);Bundle bundle = new Bundle(); // 創建一個新包裹// 往包裹存入名叫request_time的字符串bundle.putString("request_time", DateUtil.getNowTime());// 往包裹存入名叫request_content的字符串bundle.putString("request_content", mRrequest);intent.putExtras(bundle); // 把快遞包裹塞給意圖// 期望接收下個頁面的返回數據。第二個參數為本次請求代碼startActivityForResult(intent, 0);}}// 從下一個頁面攜帶參數返回當前頁面時觸發。其中requestCode為請求代碼,// resultCode為結果代碼,intent為下一個頁面返回的意圖對象@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent intent) { // 接收返回數據super.onActivityResult(requestCode, resultCode, intent);// 意圖非空,且請求代碼為之前傳的0,結果代碼也為成功if (intent!=null && requestCode==0 && resultCode== Activity.RESULT_OK) {Bundle bundle = intent.getExtras(); // 從返回的意圖中獲取快遞包裹// 從包裹中取出名叫response_time的字符串String response_time = bundle.getString("response_time");// 從包裹中取出名叫response_content的字符串String response_content = bundle.getString("response_content");String desc = String.format("收到返回消息:\n應答時間為:%s\n應答內容為:%s",response_time, response_content);tv_response.setText(desc); // 把返回消息的詳情顯示在文本視圖上}} }activity_act_requestXML文件代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_request"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="5dp"android:paddingTop="5dp"android:textColor="#000000"android:textSize="17sp" /><Buttonandroid:id="@+id/btn_request"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="傳送請求數據"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:id="@+id/tv_response"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="5dp"android:textColor="#000000"android:textSize="17sp" /></LinearLayout>ActResponseActivity類代碼如下
package com.example.chapter04;import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;import com.example.chapter04.util.DateUtil;public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {private String mResponse = "我吃過了,還是你來我家吃";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_act_response);// 從布局文件中獲取名叫tv_request的文本視圖TextView tv_request = findViewById(R.id.tv_request);findViewById(R.id.btn_response).setOnClickListener(this);// 從布局文件中獲取名叫tv_response的文本視圖TextView tv_response = findViewById(R.id.tv_response);tv_response.setText("待返回的消息為:"+mResponse);// 從上一個頁面傳來的意圖中獲取快遞包裹Bundle bundle = getIntent().getExtras();// 從包裹中取出名叫request_time的字符串String request_time = bundle.getString("request_time");// 從包裹中取出名叫request_content的字符串String request_content = bundle.getString("request_content");String desc = String.format("收到請求消息:\n請求時間為:%s\n請求內容為:%s",request_time, request_content);tv_request.setText(desc); // 把請求消息的詳情顯示在文本視圖上}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_response) {Intent intent = new Intent(); // 創建一個新意圖Bundle bundle = new Bundle(); // 創建一個新包裹// 往包裹存入名叫response_time的字符串bundle.putString("response_time", DateUtil.getNowTime());// 往包裹存入名叫response_content的字符串bundle.putString("response_content", mResponse);intent.putExtras(bundle); // 把快遞包裹塞給意圖// 攜帶意圖返回上一個頁面。RESULT_OK表示處理成功setResult(Activity.RESULT_OK, intent);finish(); // 結束當前的活動頁面}} }activity_act_responseXML文件代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/tv_request"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="5dp"android:paddingTop="5dp"android:textColor="#000000"android:textSize="17sp" /><Buttonandroid:id="@+id/btn_response"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="返回應答數據"android:textColor="#000000"android:textSize="17sp" /><TextViewandroid:id="@+id/tv_response"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="5dp"android:textColor="#000000"android:textSize="17sp" /></LinearLayout>創作不易 覺得有幫助請點贊關注收藏
總結
以上是生活随笔為你收集整理的Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java神秘岛_我的世界1.4.2喵喵苍
- 下一篇: Vue前端框架选型论述