diff --git a/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java b/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java index ef2bd7a549..e2c22a36d7 100644 --- a/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java +++ b/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java @@ -63,6 +63,24 @@ void send(Intent intent) { } } + + /** Creates an intent and launches it as a Service. */ + void sendService(Intent intent) { + if (applicationContext == null) { + Log.wtf(TAG, "Trying to send an intent before the applicationContext was initialized."); + return; + } + + Log.v(TAG, "Sending service intent " + intent); + + if (activity != null) { + activity.startService(intent); + } else { + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + applicationContext.startService(intent); + } + } + /** * Like with {@code send}, creates and launches an intent with the given params, but wraps the * {@code Intent} with {@code Intent.createChooser}. diff --git a/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java b/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java index 0cd0a0765f..73c5c65558 100644 --- a/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java +++ b/packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java @@ -114,6 +114,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { sender.launchChooser(intent, title); result.success(null); + } else if ("sendService".equalsIgnoreCase(call.method)) { + sender.sendService(intent); + result.success(null); } else if ("sendBroadcast".equalsIgnoreCase(call.method)) { sender.sendBroadcast(intent); result.success(null); diff --git a/packages/android_intent_plus/example/integration_test/android_intent_plus_test.dart b/packages/android_intent_plus/example/integration_test/android_intent_plus_test.dart index 5a81d9e914..341cac544a 100644 --- a/packages/android_intent_plus/example/integration_test/android_intent_plus_test.dart +++ b/packages/android_intent_plus/example/integration_test/android_intent_plus_test.dart @@ -85,6 +85,13 @@ void main() { await intent.launchChooser('title'); }, skip: !Platform.isAndroid); + testWidgets('sendService should not throw', (WidgetTester tester) async { + const intent = AndroidIntent( + action: 'com.example.service', + ); + await intent.sendService(); + }, skip: !Platform.isAndroid); + testWidgets('SendBroadcast should not throw', (WidgetTester tester) async { const intent = AndroidIntent( action: 'com.example.broadcast', diff --git a/packages/android_intent_plus/lib/android_intent.dart b/packages/android_intent_plus/lib/android_intent.dart index 42494eb4aa..b190440ef7 100644 --- a/packages/android_intent_plus/lib/android_intent.dart +++ b/packages/android_intent_plus/lib/android_intent.dart @@ -179,6 +179,20 @@ class AndroidIntent { ); } + /// Sends intent as broadcast. + /// + /// This works only on Android platforms. + Future sendService() async { + if (!_platform.isAndroid) { + return; + } + + await _channel.invokeMethod( + 'sendService', + _buildArguments(), + ); + } + /// Sends intent as broadcast. /// /// This works only on Android platforms. diff --git a/packages/android_intent_plus/test/android_intent_test.dart b/packages/android_intent_plus/test/android_intent_test.dart index 1d39129f2e..120344b8ad 100644 --- a/packages/android_intent_plus/test/android_intent_test.dart +++ b/packages/android_intent_plus/test/android_intent_test.dart @@ -236,6 +236,20 @@ void main() { }); }); + group('sendService', () { + test('send a broadcast', () async { + androidIntent = AndroidIntent.private( + action: 'com.example.service', + channel: mockChannel, + platform: FakePlatform(operatingSystem: 'android'), + ); + await androidIntent.sendService(); + verify(mockChannel.invokeMethod('sendService', { + 'action': 'com.example.service', + })); + }); + }); + group('sendBroadcast', () { test('send a broadcast', () async { androidIntent = AndroidIntent.private(