Android学习笔记之自定义广播的发送和编写

本文介绍了如何在Android应用中实现自定义广播的功能。包括广播发送者的设置与广播接收者的注册,通过实例展示了如何使用Intent来发送自定义广播,并在接收端进行响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Android学习笔记

参考《第一行代码 第二版》

关于广播:即系统之间相互传递消息

自定义广播即自定义消息类型。

一 创建广播接收者,接收广播。

在XML文件中注册广播接收者,注明自定义广播类型

二 使用Intent类创建实例调用构造方法初始化对象并传入广播类型(自定义)。

即 Intent i=new Intent(“c”);=A;

调用sendBroadcast()=B;方法并将初始化的对象传入即     B(A);

以上便完成了自定义广播的发送。

下面附上一个练习也是《第一行代码》书上的对应练习。

java部分代码

package com.example.jackson.receiver_3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button= (Button) findViewById(R.id.Bu_1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("e");
                 //添加自定义广播类型
                sendBroadcast(intent);
                 //发送自定义广播
                Toast.makeText(MainActivity.this,"自定义广播已发送",Toast.LENGTH_SHORT).show();
            }
        });

    }
}

下面这UI部分的xml代码和界面截图;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.jackson.receiver_3.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Bu_1"
        android:text="发送自定义广播"
       />
</RelativeLayout>

我们创建了广播发送的工具,自然需要一个广播接收者。在这里我们使用静态注册广播接收者。

下面附上java代码和XML配置文件

​//----------------------------使用静态注册广播接收者
package com.example.jackson.receiver_3;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "自定义广播1已收到", Toast.LENGTH_SHORT).show();
    }
}

​//-----------------------------以下是mxl配置文件的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jackson.receiver_3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                   <!--添加需要接收的广播类型,在这里我们可以自定义内容-->
                <action android:name="e"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

在这里我们需要注意的是,我们在xml配置文件中添加的自定义接收广播的内容,应和在发送自定义广播时添加的广播类型,完全相同,这句话有点绕口。

简言之就是发送的信号要和接收的信号相同,这样才能接收成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值