Skip to content

How to Use with Broadcast Listener

Fabian edited this page Mar 3, 2026 · 4 revisions

This enables you to use Buttons to choose actions without having to enter your app.

Side Note: For something more simple but requires opening app checkout callback with buttons

Point to needed files

# buildozer.spec
android.add_src = src
p4a.hook = p4a/hook.py
requirements = python3, kivy, pyjnius, android-notify>=1.60.6

Insert receiver to xml through p4a.hook

  • Change package variable with values in your buildozer.spec -> "package.domain.package.name"

  • Using actions ACTION_RESUME, ACTION_PAUSE and ACTION_STOP, Note Actions can be named anything.

# p4a/hook.py
from pathlib import Path
from pythonforandroid.toolchain import ToolchainCL

def after_apk_build(toolchain: ToolchainCL):
    manifest_file = Path(toolchain._dist.dist_dir) / "src" / "main" / "AndroidManifest.xml"
    old_manifest = manifest_file.read_text(encoding="utf-8")

    # Your custom receiver XML
    package = "org.wally.waller"
    receiver_name = "CarouselReceiver"
    receiver_xml = f'''
    <receiver android:name="{package}.{receiver_name}"
              android:enabled="true"
              android:exported="false">
        <intent-filter>
            <action android:name="ACTION_RESUME" />
            <action android:name="ACTION_PAUSE" />
            <action android:name="ACTION_STOP" />
        </intent-filter>
    </receiver>
    '''

    # Insert before the closing </application>
    new_manifest = old_manifest.replace('</application>', f'{receiver_xml}\n</application>')

    manifest_file.write_text(new_manifest, encoding="utf-8")

    if old_manifest != new_manifest:
        print("Receiver added successfully")
    else:
        print("Failed to add receiver")

Talk to listener from py file

from android_notify import Notification
notification = Notification(title="Reciver Notification")
notification.addButton(text="Resume", receiver_name="CarouselReceiver", action="ACTION_RESUME")
notification.addButton(text="Pause", receiver_name="CarouselReceiver", action="ACTION_PAUSE")
notification.addButton(text="Stop", receiver_name="CarouselReceiver", action="ACTION_STOP")

Broadcast Listener

  • Change first line with values in your buildozer.spec -> "package.domain.package.name"
  • This will show a toast when buttons are clicked
// src/CarouselReceiver.java
package org.wally.waller;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class CarouselReceiver extends BroadcastReceiver {

    private static final String TAG = "CarouselReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Toast.makeText(context, "Action received: " + action, Toast.LENGTH_LONG).show();
        Log.d(TAG, "Received action: " + action);
        
    }
}

Working Sample App: wallpaper carousel app