Created
August 5, 2024 02:23
-
-
Save RepComm/4e5c7bd5c01ed583456ea51f6f8c9369 to your computer and use it in GitHub Desktop.
Android broadcast listener/receiver, but it's not annoying to use and smaller scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
/**Intent Broadcast receiver shit is too verbose, fixed it for ya*/ | |
public class EZAction { | |
public static void Fire(Context ctx, String action) { | |
Intent intent = new Intent(action); | |
ctx.sendBroadcast(intent); | |
} | |
public static void FireData(Context ctx, String action, String data) { | |
Intent intent = new Intent(action); | |
intent.putExtra("data", data); | |
ctx.sendBroadcast(intent); | |
} | |
public interface EZActionListener { | |
/**data may be null*/ | |
public void onAction(String action, String data); | |
} | |
/**Listen to actions using a broadcast receiver, but with less boilerplate*/ | |
public static BroadcastReceiver Listen(EZActionListener l, String[] actions, Context ctx) { | |
BroadcastReceiver result = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
String action = intent.getAction(); | |
if (action == null) return; | |
String data = intent.getStringExtra("data"); | |
l.onAction(action, data); | |
} | |
}; | |
IntentFilter f = new IntentFilter(); | |
for (String a : actions) { | |
f.addAction(a); | |
} | |
ctx.registerReceiver(result, f); | |
return result; | |
} | |
/**Unregisters broadcast receiver | |
* If br is null, does nothing*/ | |
public static void Deafen(BroadcastReceiver br, Context ctx) { | |
if (br == null) { | |
return; | |
} | |
ctx.unregisterReceiver(br); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment