软件编程
位置:首页>> 软件编程>> Android编程>> Android入门之实现手工发送一个BroadCast

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

作者:TGITCIC  发布时间:2023-08-10 13:54:16 

标签:Android,发送,BroadCast

简介

上一篇我们讲了简单的动态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>

它长下面这个样子。

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

然后我们来看这个按钮的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);
           }
       });
   }
}

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

运行效果

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

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

来源:https://blog.csdn.net/lifetragedy/article/details/128305595

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com