Android8.0中将静态注册改为动态注册(自定义标准广播)
布局文件
<?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>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
一、静态注册(以下代码在安卓7.0及其以下能运行)
MainActivity.java
package com.example.lenovo.broadcasttest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
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 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);
            }          
        });
    }
}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
MyBroadcastReceiver.java
package com.example.lenovo.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
    public MyBroadcastReceiver() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"我收到广播",Toast.LENGTH_SHORT).show();
    }
}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
AndroidManifest.xml(在这里面进行注册----所以叫做静态注册)
     <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter><action android:name="com.example.broadcasttest.MY_BROADCAST"/></intent-filter>
        </receiver>
    1
    2
    3
    4
    5
    6
效果图:(点击没有任何反应)在这里插入图片描述
原因:Android8.0在AndroidManifest.xml文件中静态注册广播接收失效是由于官方对耗电量的优化,避免APP滥用广播的一种处理方式。除了少部分的广播仍支持静态注册(如开机广播),其余的都会出现失效的情况。
二、解决方案:改成动态注册(在Android8.0以及以后,都建议采用这种注册方式)
MainActivity.java
package com.example.lenovo.broadcasttest;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    private MyBroadcastReceiver myBroadcastReceiver;
    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);
    }
}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
MyBroadcastReceiver.java(没变)
package com.example.lenovo.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
    public MyBroadcastReceiver() {
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        //广播接收器仅仅定义了对监听到的广播的反应,没有定义监听的类型(在MainActivity.java中动态注册)
        Toast.makeText(context,"我收到广播",Toast.LENGTH_SHORT).show();
    }
}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
效果图:
在这里插入图片描述
核心思想
    将在AndroidManifest.xml中注册的部分移到MainActivity.java中进行注册(记得销毁注册)
--------------------- 
作者:青春如我 
来源:CSDN 
原文:https://blog.csdn.net/qq_38338069/article/details/83656711 
版权声明:本文为博主原创文章,转载请附上博文链接!
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号