Skip to content

Commit 18cc707

Browse files
committed
add sendService
1 parent 7c8894b commit 18cc707

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

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

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

66+
67+
/** Creates an intent and launches it as a Service. */
68+
void sendService(Intent intent) {
69+
if (applicationContext == null) {
70+
Log.wtf(TAG, "Trying to send an intent before the applicationContext was initialized.");
71+
return;
72+
}
73+
74+
Log.v(TAG, "Sending service intent " + intent);
75+
76+
if (activity != null) {
77+
activity.startService(intent);
78+
} else {
79+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
80+
applicationContext.startService(intent);
81+
}
82+
}
83+
6684
/**
6785
* Like with {@code send}, creates and launches an intent with the given params, but wraps the
6886
* {@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
@@ -114,6 +114,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
114114

115115
sender.launchChooser(intent, title);
116116
result.success(null);
117+
} else if ("sendService".equalsIgnoreCase(call.method)) {
118+
sender.sendService(intent);
119+
result.success(null);
117120
} else if ("sendBroadcast".equalsIgnoreCase(call.method)) {
118121
sender.sendBroadcast(intent);
119122
result.success(null);

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+
/// Sends intent as broadcast.
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('send a broadcast', () 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)