android studio广播机制使用详解

 更新时间:2022年08月11日 15:49:30   作者:冰山丶一角  
这篇文章主要为大家详细介绍了android studio广播机制的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Intent 是一种消息传播机制,用于组件之间数据交换和发送广播消息。通过本次实验了解 Android 系统的组件通信原理,掌握利用 Intent 启动其他组件的方法,以及利用 Intent 获取信息和发送广播消息的方法。

1、实现具有“登录”按钮的主界面,输入用户名、密码,点击登录按钮后,经过判断进入一个广播Activity(需要传递主界面的用户名)

2、在广播Activity中,输入信息,点击发送广播按钮发送广播,并且在广播接收器中接收广播并显示。

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="发送广播"
            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);//文本框对象
        //获取用户名
        Intent getuser=getIntent();
        String s=getuser.getStringExtra("user");
        text.setText(s);
        //动态注册广播
        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);//发送广播
            }
        });
    }
}

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();
                //系统内包含的用户名密码
                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,"用户名不存在或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
        Button out=(Button)findViewById(R.id.signout);
        out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android开发之获取SD卡及手机ROM容量的方法

    Android开发之获取SD卡及手机ROM容量的方法

    这篇文章主要介绍了Android开发之获取SD卡及手机ROM容量的方法,结合实例形式分析了Android针对SD卡的读取及属性操作相关技巧,需要的朋友可以参考下
    2016-04-04
  • Android中的OpenGL使用配置详解

    Android中的OpenGL使用配置详解

    这篇文章主要为大家介绍了Android中的OpenGL使用配置详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android ViewPager无限循环实现底部小圆点动态滑动

    Android ViewPager无限循环实现底部小圆点动态滑动

    这篇文章主要为大家详细介绍了Android ViewPager无限循环实现底部小圆点动态滑动的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Andriod Service与Thread的区别介绍

    Andriod Service与Thread的区别介绍

    我们要明确Service是运行在主线程的,不能有耗时操作,这样,在Service中处理耗时操作的时候,我们依然需要使用线程来处理,既然在Service里也要创建一个子线程,那为什么不直接在Activity里创建呢,下面通过本文给大家介绍Andriod Service与Thread的区别,一起看看吧
    2017-04-04
  • Android自定义扇形倒计时实例代码

    Android自定义扇形倒计时实例代码

    最近工作中需要做一个倒计时,是那种一个圆,慢慢的被吃掉的动画倒计时,由于自己是android小白,效果还不是多满意,先给大家分享实例代码,仅供大家参考
    2017-03-03
  • Android编程实现简单流量管理功能实例

    Android编程实现简单流量管理功能实例

    这篇文章主要介绍了Android编程实现简单流量管理功能的方法,结合实例形式分析了Android实现流量监控所涉及的功能模块与布局技巧,需要的朋友可以参考下
    2016-02-02
  • Android搜索框通用版

    Android搜索框通用版

    这篇文章主要为大家详细介绍了Android搜索框通用版的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android应用的Material设计的布局兼容性的一些要点总结

    Android应用的Material设计的布局兼容性的一些要点总结

    这篇文章主要介绍了Android应用的Material设计的布局兼容性的一些要点总结,文中还给了一个RecyclerView布局管理的例子,需要的朋友可以参考下
    2016-04-04
  • Android Activity 横竖屏切换的生命周期

    Android Activity 横竖屏切换的生命周期

    这篇文章主要介绍了Android Activity 横竖屏切换的生命周期的相关资料,需要的朋友可以参考下
    2016-04-04
  • 详解Android StrictMode严格模式的使用方法

    详解Android StrictMode严格模式的使用方法

    这篇文章主要介绍了Android StrictMode严格模式的使用方法,需要的朋友可以参考下
    2018-01-01

最新评论