Android8.0中将静态注册改为动态注册(自定义标准广播)

布局文件


<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>

一、静态注册(以下代码在安卓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);
            }          
        });
    }
}

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();
    }
}

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>

效果图:(点击没有任何反应Android8.0中将静态注册改为动态注册(自定义标准广播)_第1张图片
原因: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);
    }

}

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();
    }
}

效果图:
Android8.0中将静态注册改为动态注册(自定义标准广播)_第2张图片

核心思想

  • 将在AndroidManifest.xml中注册的部分移到MainActivity.java中进行注册(记得销毁注册)

你可能感兴趣的:(Android)