I can't seem to find a recent tutorial on this and the ones from 2017 and earlier don't appear to work. But if I do the broadcast receiver registration in Java it will work fine. What am I doing wrong here?
If I was to add the below to MainActivity::onCreate
it works fine whether or not the manifest has the <receiver>
entry:
registerReceiver(
new DemoReceiver(),
new IntentFilter("com.example.receiverdemo.CUSTOM_INTENT")
);
Basic example:
MainActivity.java:
package com.example.receiverdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.TestButton)
.setOnClickListener(view ->
sendBroadcast(new Intent("com.example.receiverdemo.CUSTOM_INTENT"))
);
}
}
DemoReceiver.java
package com.example.receiverdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class DemoReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("FOO", "com.example.receiverdemo.DemoReceiver::onReceive called.");
Toast.makeText(context, "Intent Received!", Toast.LENGTH_SHORT).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.receiverdemo">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ReceiverDemo"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.receiverdemo.DemoReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.receiverdemo.CUSTOM_INTENT"/>
</intent-filter>
</receiver>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/TestButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:text="TEST BROADCAST RECEIVER" />
</FrameLayout>
There is a restriction above Android O.
Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.