-->

signed

QiShunwang

“诚信为本、客户至上”

Android基础篇 静态注册 and 动态注册 BroadcastReceiver系列(一)

2021/6/3 17:32:14   来源:

一、静态注册 【仅在安卓7.0及其以下能运行】

1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lenovo.broadcasttest.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send Broadcast"/>
</RelativeLayout>

2、静态注册
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button=(Button) findViewById(R.id.button);
        //这里是指发送一条信息,让广播接收,如果不发,静态广播在任意处接收了意向的ACTION都会响应,这里可以设置成缓存的信息,开启服务后,读取缓存,接收执行静态广播的响应
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
            //监听有com.example.broadcasttest.MY_BROADCAST这条广播的接收器会收到消息
                Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
                sendBroadcast(intent);
            }          
        });
    }
}

3、(广播)MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
    public MyBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
    	//执行某些操作,但不要进行耗时操作
        Toast.makeText(context,"欢迎关注彭老希",Toast.LENGTH_SHORT).show();
    }
}

4、AndroidManifest.xml 注册 --> 静态注册的表现
     <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
            <-- 自定义name -->
            <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
            </intent-filter>
        </receiver>

二、动态注册

public class MainActivity extends AppCompatActivity {

	//自定义广播的变量
    private MyBroadcastReceiver myBroadcastReceiver;
    //Intent意向过滤
    private IntentFilter intentFilter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intentFilter = new IntentFilter();
        //这里定义接受器监听广播的类型,这里添加相应的广播
        intentFilter.addAction("com.example.broadcasttest.MY_BROADCAST");
        //实例化接收器
        myBroadcastReceiver=new MyBroadcastReceiver();
        //动态注册 :注册事件,将监听类型赋给对应的广播接收器
        registerReceiver(myBroadcastReceiver,intentFilter);

        Button button=(Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //这是一条自定义标准广播-----所有监听com.example.broadcasttest.MY_BROADCAST这条广播的接收器都会收到消息
                Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        //注销事件
        unregisterReceiver(myBroadcastReceiver);
    }

}

(广播)MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
    public MyBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        //广播接收器仅仅定义了对监听到的广播的反应,没有定义监听的类型(在MainActivity.java中动态注册)
        Toast.makeText(context,"欢迎关注彭老希",Toast.LENGTH_SHORT).show();
    }
}