安卓自定义广播

1.以动态注册方式使用自定义广播。

在这里插入图片描述

MainActivity

public class MainActivity extends AppCompatActivity {
    private TextView tv;  //用于显示接收到的广播信息
    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle mybundle =  intent.getExtras();
            String str1 = mybundle.getString("data1");
            String str2 = mybundle.getString("data2");
            Toast.makeText(context, str1+" " +str2,Toast.LENGTH_LONG).show();
            tv.setText(str1+" " +str2);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //使用意图过滤器类IntentFilter动态注册广播接收者
        IntentFilter myintentfliter = new IntentFilter();
        myintentfliter.addAction("com.example.broadcast.MY_BROADCAST");
        registerReceiver(myReceiver, myintentfliter);

        tv = findViewById(R.id.tv);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent myintent = new Intent("com.example.broadcast.MY_BROADCAST");
                Bundle  bundle = new Bundle();
                bundle.putString("data1", "自定义广播与接收案例");
                bundle.putString("data2", "李亚琦666");
                myintent.putExtras(bundle);  //捆绑数据
                sendBroadcast(myintent);   //发送广播
                try {
                    Thread.sleep(1000);  //休眠一下,模拟广播可能存在的延迟
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

主布局

    <TextView
        android:id="@+id/tv"
        android:layout_width="338dp"
        android:layout_height="40dp"
        android:layout_marginTop="32dp"
        android:text=""
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.493"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv" />

2.以静态注册方式实现的自定义广播及接收。

在这里插入图片描述

MainActivity

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
                intent.setPackage("com.example.ex622");
                Bundle bundle = new Bundle();
                bundle.putString("datal", "666");
                bundle.putString("data2", "张粤~...Ov0...");
                intent.putExtras(bundle);
                sendBroadcast(intent);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

MyReceiver

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context,Intent intent){
        Bundle mybundle = intent.getExtras();
        String str1 = mybundle.getString("data1");
        String str2 = mybundle.getString("data2");
        Toast.makeText(context,str1+" " +str2,Toast.LENGTH_LONG).show();
    }
}

主布局

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

清单文件

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <receiver
            android:name="com.example.test5_a.MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcast.MY_BROADCAST"/>
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值