網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Intent 是一種消息傳播機(jī)制,用于組件之間數(shù)據(jù)交換和發(fā)送廣播消息。通過(guò)本次實(shí)驗(yàn)了解 Android 系統(tǒng)的組件通信原理,掌握利用 Intent 啟動(dòng)其他組件的方法,以及利用 Intent 獲取信息和發(fā)送廣播消息的方法。
1、實(shí)現(xiàn)具有“登錄”按鈕的主界面,輸入用戶名、密碼,點(diǎn)擊登錄按鈕后,經(jīng)過(guò)判斷進(jìn)入一個(gè)廣播Activity(需要傳遞主界面的用戶名)
2、在廣播Activity中,輸入信息,點(diǎn)擊發(fā)送廣播按鈕發(fā)送廣播,并且在廣播接收器中接收廣播并顯示。
activity.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <GridLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:useDefaultMargins="true" ? ? ? ? android:columnCount="4"> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_columnSpan="1" ? ? ? ? ? ? android:layout_gravity="right" ? ? ? ? ? ? android:text="用戶名"/> ? ? ? ? ? <EditText ? ? ? ? ? ? ? ? android:ems="16" ? ? ? ? ? ? ? ? android:layout_columnSpan="3" ? ? ? ? ? ? ? ? android:id="@+id/user"/> ? ? ? ? <TextView ? ? ? ? ? ? android:layout_columnSpan="1" ? ? ? ? ? ? android:layout_gravity="right" ? ? ? ? ? ? android:layout_column="0" ? ? ? ? ? ? android:text="密碼"/> ? ? ? ? ? <EditText ? ? ? ? ? ? android:ems="16" ? ? ? ? ? ? android:layout_columnSpan="3" ? ? ? ? ? ? android:id="@+id/password"/> ? ? ? ? <Button ? ? ? ? ? ? android:text="登錄" ? ? ? ? ? ? android:id="@+id/signin" ? ? ? ? ? ? android:layout_column="1" ? ? ? ? ? ? android:layout_gravity="fill_horizontal"/> ? ? ? ? <Button ? ? ? ? ? ? android:text="退出" ? ? ? ? ? ? android:id="@+id/signout" ? ? ? ? ? ? android:layout_column="2" ? ? ? ? ? ? android:layout_gravity="fill_horizontal"/> ? ? ? </GridLayout> ? ? </androidx.constraintlayout.widget.ConstraintLayout>
activity_my_brocast_reveicer.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MySendBrocastReceiver"> ? ? <GridLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:useDefaultMargins="true" ? ? ? ? android:columnCount="4"> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_columnSpan="1" ? ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:layout_gravity="left" ? ? ? ? ? ? ? ? android:text="歡迎你" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:layout_width="match_parent"/> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:layout_gravity="left" ? ? ? ? ? ? ? ? android:id="@+id/name" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:layout_width="match_parent"/> ? ? ? ? </LinearLayout> ? ? ? ? <EditText ? ? ? ? ? ? android:ems="16" ? ? ? ? ? ? android:layout_column="0" ? ? ? ? ? ? android:layout_columnSpan="3" ? ? ? ? ? ? android:id="@+id/text"/> ? ? ? ? <Button ? ? ? ? ? ? android:text="發(fā)送廣播" ? ? ? ? ? ? android:id="@+id/send" ? ? ? ? ? ? android:layout_column="0" ? ? ? ? ? ? android:layout_gravity="fill_horizontal"/> ? ? ? </GridLayout> </androidx.constraintlayout.widget.ConstraintLayout>
MyReceiver.java
package com.example.intendbrocastreceiver;
?
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
?
public class MyReceiver extends BroadcastReceiver {
?
? ? @Override
? ? public void onReceive(Context context, Intent intent) {
? ? ? ? // TODO: This method is called when the BroadcastReceiver is receiving
? ? ? ? // an Intent broadcast.
? ? ? ? String name=intent.getStringExtra("name");
? ? ? ? Toast.makeText(context,name,Toast.LENGTH_LONG).show();
? ? }
}
MySendBrocastReceiver.java
package com.example.intendbrocastreceiver;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
?
import org.w3c.dom.Text;
?
public class MySendBrocastReceiver extends AppCompatActivity {
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_my_send_brocast_receiver);
?
? ? ? ? TextView text=(TextView)findViewById(R.id.name);//文本框?qū)ο?
? ? ? ? //獲取用戶名
? ? ? ? Intent getuser=getIntent();
? ? ? ? String s=getuser.getStringExtra("user");
? ? ? ? text.setText(s);
? ? ? ? //動(dòng)態(tài)注冊(cè)廣播
? ? ? ? MyReceiver myreceicer=new MyReceiver();
? ? ? ? IntentFilter intentfilter=new IntentFilter();
? ? ? ? intentfilter.addAction("com.example.intentdbrocastreceiver.send");
? ? ? ? registerReceiver(myreceicer,intentfilter);
?
? ? ? ? Button but_send=(Button)findViewById(R.id.send);
? ? ? ? but_send.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? EditText text=(EditText)findViewById(R.id.text);
? ? ? ? ? ? ? ? String te=text.getText().toString();
?
? ? ? ? ? ? ? ? Intent intent=new Intent();
? ? ? ? ? ? ? ? intent.setAction("com.example.intentdbrocastreceiver.send");
? ? ? ? ? ? ? ? intent.putExtra("name",te);//傳遞
? ? ? ? ? ? ? ? sendBroadcast(intent);//發(fā)送廣播
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
MainActivity.java
package com.example.intendbrocastreceiver;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
?
public class MainActivity extends AppCompatActivity {
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button signin=(Button)findViewById(R.id.signin);
? ? ? ? signin.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? EditText users=(EditText)findViewById(R.id.user);
? ? ? ? ? ? ? ? EditText passwords=(EditText)findViewById(R.id.password);
? ? ? ? ? ? ? ? //用戶輸入的用戶名密碼
? ? ? ? ? ? ? ? String user=users.getText().toString();
? ? ? ? ? ? ? ? String password=passwords.getText().toString();
? ? ? ? ? ? ? ? //系統(tǒng)內(nèi)包含的用戶名密碼
? ? ? ? ? ? ? ? String myuser="123";
? ? ? ? ? ? ? ? String mypassword="666";
? ? ? ? ? ? ? ? if(user.equals(myuser)&&password.equals(mypassword)){
? ? ? ? ? ? ? ? ? ? Intent login=new Intent();
? ? ? ? ? ? ? ? ? ? login.setAction("android.intent.action.sendbrocast");
? ? ? ? ? ? ? ? ? ? login.putExtra("user",user);//傳遞用戶名
? ? ? ? ? ? ? ? ? ? startActivity(login);
?
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"用戶名不存在或密碼錯(cuò)誤",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? Button out=(Button)findViewById(R.id.signout);
? ? ? ? out.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
原文鏈接:https://blog.csdn.net/qq_57171795/article/details/125195248
相關(guān)推薦
- 2022-11-05 關(guān)于Linux下動(dòng)態(tài)查看實(shí)時(shí)日志的命令_linux shell
- 2022-06-25 CentOS?8?安裝調(diào)試KVM的詳細(xì)步驟_Kvm
- 2022-12-14 正則表達(dá)式匹配0-10的正整數(shù)以及使用細(xì)節(jié)_正則表達(dá)式
- 2023-07-28 async await 寫法
- 2022-06-02 python套接字socket通信_(tái)python
- 2023-01-26 Android?源碼淺析RecyclerView?Adapter_Android
- 2022-11-04 ASP.NET?MVC使用jQuery的Load方法加載靜態(tài)頁(yè)面及注意事項(xiàng)_實(shí)用技巧
- 2022-07-23 詳解NumPy中的線性關(guān)系與數(shù)據(jù)修剪壓縮_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支