Android实验:广播实验

目录

广播

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

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

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

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

实验目的

1、 了解使用Intent进行组件通信的原理;

2、 了解Intent过滤器的原理和匹配机制;

3、 掌握发送和接收广播的方法

实验内容

任务1、普通广播;

任务2、系统广播;

任务3、有序广播;

实验要求

1、练习使用静态方法和动态方法注册广播接收器

2、练习发送广播消息的方法;

项目结构

代码实现

ActionReceiver.java

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

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

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

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

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

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

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>

结果展示



暂且如此吧。

相关推荐
北辰浮光20 分钟前
[spring]XML配置文件标签
xml·spring
m0_548514773 小时前
2024.12.10——攻防世界Web_php_include
android·前端·php
凤邪摩羯3 小时前
Android-性能优化-03-启动优化-启动耗时
android
凤邪摩羯3 小时前
Android-性能优化-02-内存优化-LeakCanary原理解析
android
喀什酱豆腐4 小时前
Handle
android
m0_748232925 小时前
Android Https和WebView
android·网络协议·https
m0_748251726 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
m0_748254668 小时前
go官方日志库带色彩格式化
android·开发语言·golang
zhangphil8 小时前
Android使用PorterDuffXfermode模式PorterDuff.Mode.SRC_OUT橡皮擦实现“刮刮乐”效果,Kotlin(2)
android·kotlin
爱学测试的李木子8 小时前
从0到1搭建 Android 自动化 python+appium 环境
android·软件测试·python·测试工具·自动化