Skip to content
Merged
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 @@ -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)) {
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 {
);
}

/// 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.
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('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(
Expand Down