安卓广播应用,BroadcastReceiver,详细解说,看了就会

前言:

广播是一个同时通知多个对象的事件通知机制。
在应用甚至手机进行某个动作后,广播接受到是哪一个动作。
接下来我们来编写一个简单的广播应用。
这个应用只需要放上去两个按钮,创建两个BroadcastReceiver类。
我们判断的三个动作分别是,开机,点击按钮一,点击按钮二,然后根据不同的动作,不同的广播进行相应的反应。



开机的动作是系统自带的,它有系统自己的命名,而其它两个动作是我们自己创建的,也是我们自己所命名

过程:

我们先把布局写好

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="50dp"
        android:id="@+id/btn1"></Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"></Button>
</LinearLayout>

然后开始新建广播
在这里插入图片描述
我们先创建一个名为MyReceiver的类。
创建之后它会自动生成这样的代码:
在这里插入图片描述
我们需要把这一行代码删掉




throw new UnsupportedOperationException("Not yet implemented");

然后变成这样:

@Override
    public void onReceive(Context context, Intent intent) {//这个方法是有对象发送广播给它时,它调用这个方法
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        
    }

只要创建了MyReceiver之后,我们在AndroidManifest.xml中,会自动创建一个

<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true"></receiver>

我们想要它接收广播就要为它创建动作,根据动作的不同在MyReceiver接受广播时就会根据相应的动作进行相应的操作,我们这样给它添加动作

<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />//开机动作
                <action android:name="send" />
            </intent-filter>
        </receiver>

我们添加了两个动作
一个是系统自带的开机动作,一个是我们自己自定义的send动作

然后我们转到MyReceiver类中去写代码,如下:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {//接受广播时时,参数里面自带一个intent,里面有所有接受的动作
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        if(intent.getAction()== "android.intent.action.BOOT_COMPLETED")//如果为开机动作
        {
            Log.d("开机","开机成功!!!");
        }
        else if (intent.getAction()=="send")//如果是我们自定义的send动作
        {
            Log.d("二号广播","succeeded");
        }


    }
}

因为开机的动作是系统自带的,手机开机的时候,系统会自动把动作传给MyReceiver,然后就会开始执行onReceive这个方法,但是我们的send动作如何传给MyReceiver呢?我们在MainActivity里面点击按钮2去动作给它。代码:

btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("send");//为intent装上动作reasureReceive
                intent.setComponent(new ComponentName("com.example.broadcast","com.example.broadcast.MyReceiver"));
                //上面这行第一个参数是我们的程序包,第二个参数是广播,广播在程序包下面一个目录,所以前面就是第一个参数的值,意思是让第一个程序包的这个类开始应用
                sendBroadcast(intent);//发送广播
            }
        });

程序包可以在这上面看:
在这里插入图片描述
另一个按钮的点击和我们这个一模一样,大家可以自己尝试。

总代码:

MainActivity.java:
package com.example.broadcast;

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;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1=(Button)findViewById(R.id.btn1);
        Button btn2=(Button)findViewById(R.id.btn2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("reasureReceive");//为intent装上动作reasureReceive
                intent.setComponent(new ComponentName("com.example.broadcast","com.example.broadcast.EnterReceiver"));
                sendBroadcast(intent);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("send");//为intent装上动作reasureReceive
                intent.setComponent(new ComponentName("com.example.broadcast","com.example.broadcast.MyReceiver"));
                sendBroadcast(intent);
            }
        });

    }
}

MyReceiver.java:

package com.example.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        if(intent.getAction()== "android.intent.action.BOOT_COMPLETED")
        {
            Log.d("开机","开机成功!!!");
        }
        else if (intent.getAction()=="send")
        {
            Log.d("二号广播","succeeded");
        }


    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章