Android实验:广播实验

广播

Android 应用与 Android 系统和其他 Android 应用之间可以相互收发广播消息,这与发布-订阅设计模式相似。这些广播会在所关注的事件发生时发送。举例来说,Android 系统会在发生各种系统事件时发送广播,例如系统启动或设备开始充电时。再比如,应用可以发送自定义广播来通知其他应用它们可能感兴趣的事件(例如,一些新数据已下载)。

应用可以注册接收特定的广播。广播发出后,系统会自动将广播传送给同意接收这种广播的应用。

一般来说,广播可作为跨应用和普通用户流之外的消息传递系统。但是,您必须小心,不要滥用在后台响应广播和运行作业的机会,因为这会导致系统变慢

关于详细的广播介绍什么的就不在这里叙述了,贴一个笔者认为写的详尽且通俗的文章
linkhttp://t.csdnimg.cn/wRnOF感兴趣可以自行了解

实验目的

1、 了解使用Intent进行组件通信的原理;
2、 了解Intent过滤器的原理和匹配机制;
3、 掌握发送和接收广播的方法

实验内容

任务1、普通广播;
任务2、系统广播;
任务3、有序广播;

实验要求

1、练习使用静态方法和动态方法注册广播接收器
2、练习发送广播消息的方法;

项目结构

在这里插入图片描述

代码实现

ActionReceiver.java

import android.app.Activity;
//import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.exp5.R;

public class ActionReceiver extends Activity {
    protected static final String ACTION =
            "com.eoeAndroid.broadcastReceiver.RECEIVER_ACTION";

    private MyReceiver myReceiver;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actionlayout);
    }
    public void sendAct(View view){
        Intent intent=new Intent();       //实例化Intent
        intent.setAction(ACTION);      //设置Intent的action属性
        intent.putExtra("info","动态方法");
        sendBroadcast(intent);
    }
    public void register(View view){
        myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION);
        registerReceiver(myReceiver, filter);
    }
    public void unregister(View view){
        unregisterReceiver(myReceiver);
    }
}

mainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.exp5.R;

public class mainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
    public void staticSys(View view){
        Intent intent = new Intent(mainActivity.this, StaticReceiver.class);
        startActivity(intent);
    }
    public void actionSys(View view){
        Intent intent = new Intent(mainActivity.this, ActionReceiver.class);
        startActivity(intent);
    }

MyOrderReceiverOne.java

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

public class MyOrderReceiverOne extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("我收到有序的啦");
    }
}

MyOrderReceiverTwo.java

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

public class MyOrderReceiverTwo extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("我收到有序的啦");
    }
}

MyOrderReceiverThree.java

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

public class MyOrderReceiverThree extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("我收到有序的啦");
    }
}

MyReceiver.java

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

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast t = Toast.makeText(context,"广播方式:"+intent.getStringExtra("info"), Toast.LENGTH_SHORT);
        t.setGravity(Gravity.TOP,0,40);
        t.show();
    }
}

StaticReceiver.java

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.example.exp5.R;

public class StaticReceiver extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.staticlayout);


    }
    public void send(View view){
        Intent intent = new Intent();
        //intent.setAction("com.eoeAndroid.broadcastReceiver.RECEIVER_ACTION");
        intent.putExtra("info","静态方法");
        intent.setComponent(new ComponentName(getPackageName(),"D:/Android/exp/exp5/app/src/main/java/com/example/exp5/ui/theme/MyReceiver.java"));
        sendBroadcast(intent);
    }
}

actionlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.theme.ActionReceiver">

    <Button
        android:id = "@+id/actBroadcast"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text = "发送Broadcast"
        android:onClick="sendAct"
        />

    <Button
        android:id = "@+id/registerReceiver"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text = "创建"
        android:onClick="register"
        />

    <Button
        android:id = "@+id/unregisterReceiver"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text = "注销"
        android:onClick="unregister"
        />

</LinearLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.theme.mainActivity">

    <Button
        android:id = "@+id/staticButton"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text = "静态方法"
        android:onClick="staticSys"
        />

    <Button
        android:id = "@+id/actionButton"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text = "动态方法"
        android:onClick="actionSys"
        />

    <Button
        android:id = "@+id/orderButton"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text = "有序广播"
        android:onClick="orderSys"
        />

</LinearLayout>

staticlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.theme.StaticReceiver">

    <Button
        android:id = "@+id/btnBroadcast"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text = "发送Broadcast"
        android:onClick="send"
        />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Exp5"
        tools:targetApi="31">
        <activity
            android:name=".ui.theme.mainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.Exp5">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.exp5.ui.theme.StaticReceiver" />
        <activity android:name="com.example.exp5.ui.theme.ActionReceiver" />

        <receiver android:name="com.example.exp5.ui.theme.MyOrderReceiverOne" />
        <receiver android:name="com.example.exp5.ui.theme.MyReceiver" />
        <receiver android:name="com.example.exp5.ui.theme.MyOrderReceiverTwo" />
        <receiver android:name="com.example.exp5.ui.theme.MyOrderReceiverThree" />
    </application>

</manifest>

结果展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

暂且如此吧。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,Android Studio 的消息广播实验可以通过以下步骤完成: 1. 创建一个新的项目 在 Android Studio 中创建一个新的项目,并在应用程序的主活动中添加以下代码: ``` public class MainActivity extends AppCompatActivity { private Button broadcastButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); broadcastButton = findViewById(R.id.broadcast_button); broadcastButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.broadcast.MY_ACTION"); sendBroadcast(intent); } }); } } ``` 在此代码段中,我们创建了一个按钮,当用户单击按钮时,会发送一个名为“com.example.broadcast.MY_ACTION”的广播。 2. 创建一个广播接收器 在应用程序中创建一个新的广播接收器,以便我们可以接收到发送的广播。为此,请创建一个新的 Java 类,并在其中添加以下代码: ``` public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Broadcast received!", Toast.LENGTH_SHORT).show(); } } ``` 在此代码段中,我们创建了一个新的广播接收器,并在接收到广播时显示一个短暂的 Toast 消息。 3. 注册广播接收器 在 AndroidManifest.xml 文件中,添加以下内容以注册广播接收器: ``` <receiver android:name=".MyBroadcastReceiver" android:exported="true"> <intent-filter> <action android:name="com.example.broadcast.MY_ACTION" /> </intent-filter> </receiver> ``` 在此代码段中,我们将 MyBroadcastReceiver 类注册为接收名为“com.example.broadcast.MY_ACTION”的广播。 4. 运行应用程序 现在,您可以运行应用程序并单击“发送广播”按钮。您应该能够看到一个短暂的 Toast 消息,指示广播已成功接收。 这就是 Android Studio 中消息广播实验的基本步骤。您可以根据需要进行修改和扩展,以满足您的特定需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值