Skip to content

Commit

Permalink
add sendService
Browse files Browse the repository at this point in the history
  • Loading branch information
KGDavidson committed Dec 23, 2024
1 parent 7c8894b commit 18cc707
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 14 additions & 0 deletions packages/android_intent_plus/lib/android_intent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ class AndroidIntent {
);
}

/// Sends intent as broadcast.
///
/// This works only on Android platforms.
Future<void> sendService() async {
if (!_platform.isAndroid) {
return;
}

await _channel.invokeMethod<void>(
'sendService',
_buildArguments(),
);
}

/// Sends intent as broadcast.
///
/// This works only on Android platforms.
Expand Down
14 changes: 14 additions & 0 deletions packages/android_intent_plus/test/android_intent_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>('sendService', <String, Object>{
'action': 'com.example.service',
}));
});
});

group('sendBroadcast', () {
test('send a broadcast', () async {
androidIntent = AndroidIntent.private(
Expand Down

0 comments on commit 18cc707

Please sign in to comment.