Android入门之实现手工发送一个BroadCast

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android入门之实现手工发送一个BroadCast

TGITCIC   2022-12-14 我要评论

简介

上一篇我们讲了简单的动态BroadCast,今天我们通过手工来发送一条BroadCast进一步来了解BroadCast。

在上一篇里我们使用BroadCast监听网络状态,今天我们要完成的是自己发一条自自己的消息来触发BroadCast Receiver。

设计

为了让Receiver收听到我们自己发送的自定义消息,我们需要在Receiver注册在AndroidManifest.xml文件中多添加一点东西,它长成下面这个样:

        <receiver
            android:name=".SimpleBroadCastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="org.mk.demo.broadcast.SimpleBroadCast"/>
            </intent-filter>
 
        </receiver>

来看Receiver类。

SimpleBroadCastReceiver

package org.mk.android.demo.demo;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
 
public class SimpleBroadCastReceiver extends BroadcastReceiver {
    private final String ACTION_BOOT = "org.mk.demo.broadcast.SimpleBroadCast";
    private final String TAG = "SendBroadCast";
 
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        if (intent.getAction().equals(ACTION_BOOT)) {
            Log.i(TAG, "receive: " + ACTION_BOOT);
            Toast.makeText(context, "receive: " + ACTION_BOOT, Toast.LENGTH_LONG).show();
        }
    }
}

看这边,这边我们使用了onReceive方法内传入的Intent里的getAction来判断,这条消息是不是来自于我们自定义的BroadCast。

这个BroadCast来自于我们的Activity里的button的onclick动作。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <Button
        android:id="@+id/buttonSendBroadCast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送一条BroadCast信息"/>
</LinearLayout>

它长下面这个样子。

然后我们来看这个按钮的onClick事件吧。

MainActivity.java

package org.mk.android.demo.demo;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity {
 
    private Button buttonSendBroadCast;
    private final String TAG = "SendBroadCast";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonSendBroadCast = (Button) findViewById(R.id.buttonSendBroadCast);
        buttonSendBroadCast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG,"start send broadcast");
                Intent bIntent=new Intent("org.mk.demo.broadcast.SimpleBroadCast");
                bIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
                sendBroadcast(bIntent);
            }
        });
    }
}

从代码里可以看到,我们传送了一个消息为“org.mk.demo.broadcast.SimpleBroadCast”。这让它可以触发我们的SimpleBroadCastReceiver里的onReceive方法。

运行效果

在点击该按钮时,我们的APP收到了自定义的BroadCast并显示了以下这条Toast在屏幕上。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们