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
3 changes: 2 additions & 1 deletion packages/android_intent_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## pre-release
## 0.4.2

- Add launchChooser method, which uses Intent.createChooser internally.
- Add sendBroadcast method, which uses context.sendBroadcast() internally.

## 0.4.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public void launchChooser(Intent intent, String title) {
send(Intent.createChooser(intent, title));
}

/** Creates an intent and sends it as Broadcast. */
public void sendBroadcast(Intent intent) {
if (applicationContext == null) {
Log.wtf(TAG, "Trying to send broadcast before the applicationContext was initialized.");
return;
}

Log.v(TAG, "Sending broadcast " + intent);
applicationContext.sendBroadcast(intent);
}

/**
* Verifies the given intent and returns whether the application context class can resolve it.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
String title = call.argument("chooserTitle");
sender.launchChooser(intent, title);
result.success(null);
} else if ("sendBroadcast".equalsIgnoreCase(call.method)) {
sender.sendBroadcast(intent);
result.success(null);
} else if ("canResolveActivity".equalsIgnoreCase(call.method)) {
result.success(sender.canResolveActivity(intent));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ flutter {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.plugins.androidintentexample">

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<!--
The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
android:label="android_intent_example"
android:name="io.flutter.app.FlutterApplication">
android:label="android_intent_example">
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:name=".EmbeddingV1Activity"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
</activity>

<activity android:name="io.flutter.embedding.android.FlutterActivity"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:windowSoftInputMode="adjustResize"></activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="flutterEmbedding" android:value="2"/>

<meta-data
android:name="flutterEmbedding"
android:value="2" />

<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcast"></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.flutter.plugins.androidintentexample;

import android.content.IntentFilter;
import android.os.Bundle;
import dev.fluttercommunity.plus.androidintent.AndroidIntentPlugin;
import io.flutter.app.FlutterActivity;

public class MainActivity extends FlutterActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

AndroidIntentPlugin.registerWith(
registrarFor("dev.fluttercommunity.plus.androidintent.AndroidIntentPlugin"));

IntentFilter filter = new IntentFilter("com.example.broadcast");
MyBroadcastReceiver receiver = new MyBroadcastReceiver();
registerReceiver(receiver, filter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.flutter.plugins.androidintentexample;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MyBroadcastReceiver", "Got Intent: " + intent.toString());
Toast.makeText(context, "Broadcast Received!", Toast.LENGTH_LONG).show();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<resources></resources>
15 changes: 12 additions & 3 deletions packages/android_intent_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ class MyHomePage extends StatelessWidget {
onPressed: _createAlarm,
),
RaisedButton(
child: const Text(
'Tap here to launch Intent with Chooser',
),
child: const Text('Tap here to launch Intent with Chooser'),
onPressed: _openChooser,
),
RaisedButton(
child: const Text('Tap here to send Intent as broadcast'),
onPressed: _sendBroadcast,
),
RaisedButton(
child: const Text('Tap here to test explicit intents.'),
onPressed: () => _openExplicitIntentsView(context),
Expand All @@ -96,6 +98,13 @@ class MyHomePage extends StatelessWidget {
);
intent.launchChooser('Chose an app');
}

void _sendBroadcast() {
final intent = const AndroidIntent(
action: 'com.example.broadcast',
);
intent.sendBroadcast();
}
}

/// Launches intents to specific Android activities.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() {
(Widget widget) =>
widget is Text && widget.data.startsWith('Tap here'),
),
findsNWidgets(3),
findsNWidgets(4),
);
} else {
expect(
Expand Down Expand Up @@ -58,6 +58,13 @@ void main() {
await intent.launchChooser('title');
}, skip: !Platform.isAndroid);

testWidgets('#sendBroadcast should not throw', (WidgetTester tester) async {
final intent = const AndroidIntent(
action: 'com.example.broadcast',
);
await intent.sendBroadcast();
}, skip: !Platform.isAndroid);

testWidgets('#canResolveActivity returns true when example Activity is found',
(WidgetTester tester) async {
final intent = AndroidIntent(
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 @@ -154,6 +154,20 @@ class AndroidIntent {
);
}

/// Sends intent as broadcast.
///
/// This works only on Android platforms.
Future<void> sendBroadcast() async {
if (!_platform.isAndroid) {
return;
}

await _channel.invokeMethod<void>(
'sendBroadcast',
_buildArguments(),
);
}

/// Check whether the intent can be resolved to an activity.
///
/// This works only on Android platforms.
Expand Down
2 changes: 1 addition & 1 deletion packages/android_intent_plus/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: android_intent_plus
description: Flutter plugin for launching Android Intents. Not supported on iOS.
version: 0.4.1
version: 0.4.2
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/

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 @@ -155,6 +155,20 @@ void main() {
}));
});
});

group('sendBroadcast', () {
test('send a broadcast', () async {
androidIntent = AndroidIntent.private(
action: 'com.example.broadcast',
channel: mockChannel,
platform: FakePlatform(operatingSystem: 'android'),
);
await androidIntent.sendBroadcast();
verify(mockChannel.invokeMethod<void>('sendBroadcast', <String, Object>{
'action': 'com.example.broadcast',
}));
});
});
});

group('convertFlags ', () {
Expand Down