Skip to content

Commit ea72b63

Browse files
feat(android_intent_plus): adds sendService method (#3410)
Co-authored-by: Miguel Beltran <[email protected]>
1 parent 37f533c commit ea72b63

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/IntentSender.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ void send(Intent intent) {
6363
}
6464
}
6565

66+
/** Creates an intent and launches it as a Service. */
67+
void sendService(Intent intent) {
68+
if (applicationContext == null) {
69+
Log.wtf(TAG, "Trying to send an intent before the applicationContext was initialized.");
70+
return;
71+
}
72+
73+
Log.v(TAG, "Sending service intent " + intent);
74+
75+
if (activity != null) {
76+
activity.startService(intent);
77+
} else {
78+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
79+
applicationContext.startService(intent);
80+
}
81+
}
82+
6683
/**
6784
* Like with {@code send}, creates and launches an intent with the given params, but wraps the
6885
* {@code Intent} with {@code Intent.createChooser}.

packages/android_intent_plus/android/src/main/java/dev/fluttercommunity/plus/androidintent/MethodCallHandlerImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
119119
} else if ("sendBroadcast".equalsIgnoreCase(call.method)) {
120120
sender.sendBroadcast(intent);
121121
result.success(null);
122+
} else if ("sendService".equalsIgnoreCase(call.method)) {
123+
sender.sendService(intent);
124+
result.success(null);
122125
} else if ("canResolveActivity".equalsIgnoreCase(call.method)) {
123126
result.success(sender.canResolveActivity(intent));
124127
} else if ("getResolvedActivity".equalsIgnoreCase(call.method)) {

packages/android_intent_plus/example/integration_test/android_intent_plus_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ void main() {
8585
await intent.launchChooser('title');
8686
}, skip: !Platform.isAndroid);
8787

88+
testWidgets('sendService should not throw', (WidgetTester tester) async {
89+
const intent = AndroidIntent(
90+
action: 'com.example.service',
91+
);
92+
await intent.sendService();
93+
}, skip: !Platform.isAndroid);
94+
8895
testWidgets('SendBroadcast should not throw', (WidgetTester tester) async {
8996
const intent = AndroidIntent(
9097
action: 'com.example.broadcast',

packages/android_intent_plus/lib/android_intent.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ class AndroidIntent {
179179
);
180180
}
181181

182+
/// Starts intent as service.
183+
///
184+
/// This works only on Android platforms.
185+
Future<void> sendService() async {
186+
if (!_platform.isAndroid) {
187+
return;
188+
}
189+
190+
await _channel.invokeMethod<void>(
191+
'sendService',
192+
_buildArguments(),
193+
);
194+
}
195+
182196
/// Sends intent as broadcast.
183197
///
184198
/// This works only on Android platforms.

packages/android_intent_plus/test/android_intent_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ void main() {
236236
});
237237
});
238238

239+
group('sendService', () {
240+
test('start a service', () async {
241+
androidIntent = AndroidIntent.private(
242+
action: 'com.example.service',
243+
channel: mockChannel,
244+
platform: FakePlatform(operatingSystem: 'android'),
245+
);
246+
await androidIntent.sendService();
247+
verify(mockChannel.invokeMethod<void>('sendService', <String, Object>{
248+
'action': 'com.example.service',
249+
}));
250+
});
251+
});
252+
239253
group('sendBroadcast', () {
240254
test('send a broadcast', () async {
241255
androidIntent = AndroidIntent.private(

0 commit comments

Comments
 (0)