Skip to content

feat(android_intent_plus): adds sendService method #3410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 3, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -63,6 +63,23 @@ 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}.
Original file line number Diff line number Diff line change
@@ -119,6 +119,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
} else if ("sendBroadcast".equalsIgnoreCase(call.method)) {
sender.sendBroadcast(intent);
result.success(null);
} else if ("sendService".equalsIgnoreCase(call.method)) {
sender.sendService(intent);
result.success(null);
} else if ("canResolveActivity".equalsIgnoreCase(call.method)) {
result.success(sender.canResolveActivity(intent));
} else if ("getResolvedActivity".equalsIgnoreCase(call.method)) {
Original file line number Diff line number Diff line change
@@ -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',
14 changes: 14 additions & 0 deletions packages/android_intent_plus/lib/android_intent.dart
Original file line number Diff line number Diff line change
@@ -179,6 +179,20 @@ class AndroidIntent {
);
}

/// Starts intent as service.
///
/// 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.
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
@@ -236,6 +236,20 @@ void main() {
});
});

group('sendService', () {
test('start a service', () 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(