Skip to content

dreiklangdev/Breadcast

Repository files navigation

Base: Download Processor: Download Annotation: Download

bread Breadcast

Small Broadcast Receiver Library for Android

... simplifies listening to broadcasts by hiding what would be boilerplate code.

  • One for All: Single BroadcastReceiver instance for every action
  • Clean look: Generates the code behind the curtains
  • Thread-Modes: Multithreaded callback via annotation option
  • Simple: Broadcast listening possible in one statement

Before:

class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
            case Intent.ACTION_SCREEN_OFF:
                ...
            case Intent.ACTION_SCREEN_ON:
                ...
        }
    }
}
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
context.registerBroadcast(new MyReceiver(), filter);

With Breadcast you can choose between 2 ways:

1. Breadcast Standalone (Base package)

new Breadcaster(context)
    .action(Intent.ACTION_SCREEN_OFF, (context, intent) -> ... )
    .action(Intent.ACTION_SCREEN_ON, (context, intent) -> ... )
    .register();

2. Breadcast Annotation

Breadcast.init(context);
class MyClass { // extends ...
    MyClass() {
        Breadcast.instance().register(this); // required for non-static methods
    }
    
    @Receive(action = Intent.ACTION_SCREEN_OFF)
    void onScreenOff() { ... }
    
    @Receive(action = Intent.ACTION_SCREEN_ON)
    void onScreenOn() { ... }
}

More examples

@Receive(action = {Intent.ACTION_SCREEN_ON, Intent.ACTION_SCREEN_OFF})
onScreenChange(Context context, Intent intent) { ... } // multiple
	
@Receive(action = "custom1", threadMode = ThreadModus.ASYNC)
onCustomAsync() { ... } // asynchronous

@Receive(action = Intent.ACTION_SHUTDOWN)
static withoutRegister() { ... } // static - called regardless of object registration	

To define actions via manifest (implicit/static), use ManifestBreadcast

<receiver android:name="io.dreiklang.breadcast.base.statics.ManifestBreadcast">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
@Receive(action = Intent.ACTION_BOOT_COMPLETED)
static onBoot() { ... } // must be static

Important: You must init() Breadcast nevertheless!

Notes

Download

Gradle

dependencies {
    // required
    implementation 'io.dreiklang:breadcast-base:1.1.0'
    
    // if you use the annotation
    implementation 'io.dreiklang:breadcast-annotation:1.1.0'
    annotationProcessor 'io.dreiklang:breadcast-processor:1.1.0'
}

Please open an issue case if you find one. It's merely 1-2 minutes against literally tons of neurons roasted for debunking them. :)

Thank you and have fun!

License

Copyright 2018 Nhu Huy Le

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Logo icon by Icons8