安卓组件通信
2023年11月6日通讯方式1 使用 startActivity
组件1逻辑层,发送数据
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 |
package com.example.myapplication; import com.example.myapplication.util.DateUtil; import androidx.appcompat.app.AppCompatActivity; import android.widget.TextView; import android.content.Intent; import android.os.Bundle; import android.view.View; public class ActSendActivity05 extends AppCompatActivity implements View.OnClickListener { private TextView tv_send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_send05); tv_send = findViewById(R.id.tv_send); findViewById(R.id.btn_send).setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(this, ActReceiveActivity05.class); // 创建一个新包裹 Bundle bundle = new Bundle(); bundle.putString("request_time", DateUtil.getNowTime()); bundle.putString("request_content", tv_send.getText().toString()); intent.putExtras(bundle); startActivity(intent); } } |
组件1视图层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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/tv_send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="今天的天气真不错" /> <Button android:id="@+id/btn_send" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="发送以上文字" /> </LinearLayout> |
组件2接收数据逻辑层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class ActReceiveActivity05 extends AppCompatActivity { private TextView tv_receive; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_receive05); tv_receive = findViewById(R.id.tv_receive); // 从上一个页面传来的意图中获取快递包裹 Bundle bundle = getIntent().getExtras(); String request_time = bundle.getString("request_time"); String request_content = bundle.getString("request_content"); String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s", request_time, request_content); tv_receive.setText(desc); } } |
组件2接收数据视图层
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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/tv_receive" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> |
通讯方式2
组件通讯方式2逻辑层
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.example.myapplication; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.example.myapplication.util.DateUtil; public class ActRequestActivity06 extends AppCompatActivity implements View.OnClickListener { private static final String mRequest = "你睡了吗?来我家睡吧"; private ActivityResultLauncher<Intent> register; private TextView tv_response; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_request06); TextView tv_request = findViewById(R.id.tv_request); tv_request.setText("待发送的消息为:" + mRequest); tv_response = findViewById(R.id.tv_response); findViewById(R.id.btn_request).setOnClickListener(this); register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { if (result != null) { Intent intent = result.getData(); if (intent != null && result.getResultCode() == Activity.RESULT_OK) { Bundle bundle = intent.getExtras(); String response_time = bundle.getString("response_time"); String response_content = bundle.getString("response_content "); String desc = String.format("收到返回消息:\n应答时间为%s\n应答内容为%s", response_time, response_content); // 把返回消息的详情显示在文本视图上 tv_response.setText(desc); } } }); } @Override public void onClick(View v) { Intent intent = new Intent(this, ActResponseActivity06.class); // 创建一个新包裹 Bundle bundle = new Bundle(); bundle.putString("request_time", DateUtil.getNowTime()); bundle.putString("request_content", mRequest); intent.putExtras(bundle); register.launch(intent); } } |
视图层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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/tv_request" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="传送请求数据" /> <TextView android:id="@+id/tv_response" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> |
接收逻辑层
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 42 43 44 45 46 47 48 |
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.example.myapplication.util.DateUtil; public class ActResponseActivity06 extends AppCompatActivity implements View.OnClickListener { private static final String mReponse = "我还没睡,我爸妈不在家。"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act_response06); TextView tv_request = findViewById(R.id.tv_request); // 从上一个页面传来的意图中获取快递包裹 Bundle bundle = getIntent().getExtras(); String request_time = bundle.getString("request_time"); String request_content = bundle.getString("request_content"); String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s", request_time, request_content); // 把请求消息的详情显示在文本视图上 tv_request.setText(desc); findViewById(R.id.btn_response).setOnClickListener(this); TextView tv_response = findViewById(R.id.tv_response); tv_response.setText("待返回的消息为:" + mReponse); } @Override public void onClick(View v) { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("response_time", DateUtil.getNowTime()); bundle.putString("response_content", mReponse); intent.putExtras(bundle); // 携带意图返回上一个页面。RESULT_OK表示处理成功 setResult(Activity.RESULT_OK, intent); // 结束当前的活动页面 finish(); } } |
接收视图层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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/tv_request" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_response" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="返回应答数据" /> <TextView android:id="@+id/tv_response" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> |