Skip to content
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

feat(android_intent_plus): adds sendService method #3410

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
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
Loading