Android 8.0 changes static registration to dynamic registration (custom standard broadcast)

Keywords: Programming Android Java xml encoding

Layout file

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

I. static registration (the following code can run in Android 7.0 and below)
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){
            //The receiver listening to the broadcast of com.example.broadcasttest.my'broadcast will receive a message
                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,"I got the radio",Toast.LENGTH_SHORT).show();
    }
}

Android manifest. XML (register here -- so it's called static registration)

     <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter><action android:name="com.example.broadcasttest.MY_BROADCAST"/></intent-filter>
        </receiver>

Rendering: (no response when clicking)
Reason: Android 8.0's failure in receiving static registered broadcasts in the Android manifest.xml file is due to the official optimization of power consumption and a way to avoid APP abuse of broadcasts. Except for a few broadcasts that still support static registration (such as power on broadcast), the rest will fail.

2. Solution: change to dynamic registration (this is recommended for Android 8.0 and later)
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();
        //The receiver listening broadcast type is defined here, and the corresponding broadcast is added here
        intentFilter.addAction("com.example.broadcasttest.MY_BROADCAST");
        //Instantiate receiver
        myBroadcastReceiver=new MyBroadcastReceiver();
        //Register event, assign the listening type to the corresponding broadcast receiver -- so this is called dynamic registration
        registerReceiver(myBroadcastReceiver,intentFilter);

        Button button=(Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //This is a custom standard broadcast - all receivers listening to com.example.broadcasttest.my'broadcast receive messages
                Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        //Unregister Event
        unregisterReceiver(myBroadcastReceiver);
    }

}

MyBroadcastReceiver.java (unchanged)

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) {
        //The broadcast receiver only defines the response to the monitored broadcast, and does not define the listening type (registered dynamically in MainActivity.java)
        Toast.makeText(context,"I got the radio",Toast.LENGTH_SHORT).show();
    }
}

Design sketch:

Core idea

  • Move the part registered in Android manifest.xml to MainActivity.java for registration (remember to destroy the registration)

Posted by noodle on Wed, 11 Dec 2019 07:29:42 -0800