Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ First install the package via [pub.dev](https://pub.dev/packages/flutter_broadca
```dart
BroadcastReceiver receiver = BroadcastReceiver(
names: <String>["de.kevlatus.broadcast"],
listenToBroadcastsFromOtherApps: false,
);
receiver.messages.listen(print);
receiver.start();
Expand Down
56 changes: 29 additions & 27 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
group 'de.kevlatus.flutter_broadcasts'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.5.20'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.4.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
plugins {
id 'com.android.library'
id 'kotlin-android'
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
group = 'de.kevlatus.flutter_broadcasts'
version = '1.0-SNAPSHOT'

android {
compileSdkVersion 31
namespace 'de.kevlatus.flutter_broadcasts'
compileSdk = 34

defaultConfig {
minSdk = 24
targetSdk = 34
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 24

buildFeatures {
buildConfig = true
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}

repositories {
google()
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.22"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.util.Log
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand All @@ -17,6 +18,7 @@ import java.io.Serializable
class CustomBroadcastReceiver(
val id: Int,
private val names: List<String>,
private val listenToBroadcastsFromOtherApps: Boolean,
private val listener: (Any) -> Unit
) : BroadcastReceiver() {
companion object {
Expand Down Expand Up @@ -46,7 +48,17 @@ class CustomBroadcastReceiver(
}

fun start(context: Context) {
context.registerReceiver(this, intentFilter)
if (Build.VERSION.SDK_INT >= 33) {
val receiverFlags = if (listenToBroadcastsFromOtherApps) {
Context.RECEIVER_EXPORTED
} else {
Context.RECEIVER_NOT_EXPORTED
}
context.registerReceiver(this, intentFilter, receiverFlags)
} else {
context.registerReceiver(this, intentFilter)
}

Log.d(TAG, "starting to listen for broadcasts: " + names.joinToString(";"))
}

Expand Down Expand Up @@ -95,15 +107,18 @@ class MethodCallHandlerImpl(
private fun withReceiverArgs(
call: MethodCall,
result: Result,
func: (id: Int, names: List<String>) -> Unit
func: (id: Int, names: List<String>, listenToBroadcastsFromOtherApps: Boolean) -> Unit
) {
val id = call.argument<Int>("id")
?: return result.error("1", "no receiver id provided", null)

val names = call.argument<List<String>>("names")
?: return result.error("1", "no names provided", null)

func(id, names)
val listenToBroadcastsFromOtherApps = call.argument<Boolean>("listenToBroadcastsFromOtherApps")
?: return result.error("1", "listenToBroadcastsFromOtherApps is not provided", null)

func(id, names, listenToBroadcastsFromOtherApps)
}

private fun withBroadcastArgs(
Expand All @@ -118,16 +133,16 @@ class MethodCallHandlerImpl(
}

private fun onStartReceiver(call: MethodCall, result: Result) {
withReceiverArgs(call, result) { id, names ->
broadcastManager.startReceiver(CustomBroadcastReceiver(id, names) { broadcast ->
withReceiverArgs(call, result) { id, names, listenToBroadcastsFromOtherApps ->
broadcastManager.startReceiver(CustomBroadcastReceiver(id, names, listenToBroadcastsFromOtherApps) { broadcast ->
channel?.invokeMethod("receiveBroadcast", broadcast)
})
result.success(null)
}
}

private fun onStopReceiver(call: MethodCall, result: Result) {
withReceiverArgs(call, result) { id, _ ->
withReceiverArgs(call, result) { id, _, _ ->
broadcastManager.stopReceiver(id)
result.success(null)
}
Expand Down
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "de.kevlatus.flutter_broadcasts_example"
minSdkVersion 24
targetSdkVersion 33
compileSdkVersion 31
targetSdkVersion 34
compileSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
3 changes: 2 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
android:windowSoftInputMode="adjustResize"
android:exported="true">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
13 changes: 12 additions & 1 deletion lib/src/receiver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ class BroadcastReceiver {
/// See [BroadcastMessage.name] for more details.
final List<String> names;

/// Allow receiver to listen to broadcasts from other apps
///
/// Android specific. Requires SDK 33+
final bool listenToBroadcastsFromOtherApps;

StreamSubscription? _subscription;

/// Creates a new [BroadcastReceiver], which subscribes to the given [names].
///
/// At least one name needs to be provided.
BroadcastReceiver({required this.names})
BroadcastReceiver({
required this.names,
this.listenToBroadcastsFromOtherApps = true,
})
: assert(names.length > 0),
_id = ++_index;

Expand Down Expand Up @@ -63,6 +71,7 @@ class BroadcastReceiver {
Map<String, dynamic> toMap() => <String, dynamic>{
'id': _id,
'names': names,
'listenToBroadcastsFromOtherApps': listenToBroadcastsFromOtherApps,
};

@override
Expand All @@ -74,6 +83,7 @@ class BroadcastReceiver {
int get hashCode =>
_id.hashCode ^
names.hashCode ^
listenToBroadcastsFromOtherApps.hashCode ^
_subscription.hashCode ^
_messages.hashCode;

Expand All @@ -83,6 +93,7 @@ class BroadcastReceiver {
other is BroadcastReceiver &&
other._id == _id &&
other.names == names &&
other.listenToBroadcastsFromOtherApps == listenToBroadcastsFromOtherApps &&
other._messages == _messages &&
other._subscription == _subscription;
}
Expand Down