Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab9f213

Browse files
akindonemehmetf
authored andcommitted
Fix: url_launcher can't launch for Android (flutter-team-archive#947)
Always require activity instead of using application context
1 parent 5f9affc commit ab9f213

4 files changed

Lines changed: 46 additions & 11 deletions

File tree

packages/url_launcher/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.0.3
2+
3+
* Fixed launch url fail for Android: `launch` now assert activity not null and using activity to startActivity.
4+
* Fixed `WebViewActivity has leaked IntentReceiver` for Android.
5+
16
## 4.0.2
27

38
* Added `closeWebView` function to programmatically close the current WebView.

packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/UrlLauncherPlugin.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,31 @@ private UrlLauncherPlugin(Registrar registrar) {
3939

4040
@Override
4141
public void onMethodCall(MethodCall call, Result result) {
42-
Context context = mRegistrar.context();
4342
String url = call.argument("url");
4443
if (call.method.equals("canLaunch")) {
4544
canLaunch(url, result);
4645
} else if (call.method.equals("launch")) {
4746
Intent launchIntent;
4847
boolean useWebView = call.argument("useWebView");
4948
boolean enableJavaScript = call.argument("enableJavaScript");
49+
Activity activity = mRegistrar.activity();
50+
if (activity == null) {
51+
result.error("NO_ACTIVITY", "Launching a URL requires a foreground activity.", null);
52+
return;
53+
}
5054
if (useWebView) {
51-
launchIntent = new Intent(context, WebViewActivity.class);
55+
launchIntent = new Intent(activity, WebViewActivity.class);
5256
launchIntent.putExtra("url", url);
5357
launchIntent.putExtra("enableJavaScript", enableJavaScript);
5458
} else {
5559
launchIntent = new Intent(Intent.ACTION_VIEW);
5660
launchIntent.setData(Uri.parse(url));
5761
}
58-
if (mRegistrar.activity() == null) {
59-
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
60-
}
61-
context.startActivity(launchIntent);
62+
activity.startActivity(launchIntent);
6263
result.success(null);
6364
} else if (call.method.equals("closeWebView")) {
6465
Intent intent = new Intent("close");
65-
context.sendBroadcast(intent);
66+
mRegistrar.context().sendBroadcast(intent);
6667
result.success(null);
6768
} else {
6869
result.notImplemented();
@@ -85,6 +86,7 @@ private void canLaunch(String url, Result result) {
8586
/* Launches WebView activity */
8687
public static class WebViewActivity extends Activity {
8788
private WebView webview;
89+
private BroadcastReceiver broadcastReceiver;
8890

8991
@Override
9092
public void onCreate(Bundle savedInstanceState) {
@@ -110,17 +112,23 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
110112
});
111113

112114
// Set broadcast receiver to handle calls to close the web view
113-
BroadcastReceiver broadcast_receiver =
115+
broadcastReceiver =
114116
new BroadcastReceiver() {
115117
@Override
116118
public void onReceive(Context arg0, Intent intent) {
117119
String action = intent.getAction();
118-
if (action.equals("close")) {
120+
if ("close".equals(action)) {
119121
finish();
120122
}
121123
}
122124
};
123-
registerReceiver(broadcast_receiver, new IntentFilter("close"));
125+
registerReceiver(broadcastReceiver, new IntentFilter("close"));
126+
}
127+
128+
@Override
129+
protected void onDestroy() {
130+
super.onDestroy();
131+
unregisterReceiver(broadcastReceiver);
124132
}
125133

126134
@Override

packages/url_launcher/example/lib/main.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class MyHomePage extends StatefulWidget {
3434

3535
class _MyHomePageState extends State<MyHomePage> {
3636
Future<void> _launched;
37+
String _phone = '';
3738

3839
Future<void> _launchInBrowser(String url) async {
3940
if (await canLaunch(url)) {
@@ -72,6 +73,14 @@ class _MyHomePageState extends State<MyHomePage> {
7273
}
7374
}
7475

76+
Future<void> _makePhoneCall(String url) async {
77+
if (await canLaunch(url)) {
78+
await launch(url);
79+
} else {
80+
throw 'Could not launch $url';
81+
}
82+
}
83+
7584
@override
7685
Widget build(BuildContext context) {
7786
const String toLaunch = 'https://flutter.io';
@@ -83,6 +92,19 @@ class _MyHomePageState extends State<MyHomePage> {
8392
child: Column(
8493
mainAxisAlignment: MainAxisAlignment.center,
8594
children: <Widget>[
95+
Padding(
96+
padding: const EdgeInsets.all(16.0),
97+
child: TextField(
98+
onChanged: (String text) => _phone = text,
99+
decoration: const InputDecoration(
100+
hintText: 'Input the phone number to launch')),
101+
),
102+
RaisedButton(
103+
onPressed: () => setState(() {
104+
_launched = _makePhoneCall('tel:$_phone');
105+
}),
106+
child: const Text('Make phone call'),
107+
),
86108
const Padding(
87109
padding: EdgeInsets.all(16.0),
88110
child: Text(toLaunch),

packages/url_launcher/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL on Android and iOS. Supports
33
web, phone, SMS, and email schemes.
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher
6-
version: 4.0.2
6+
version: 4.0.3
77

88
flutter:
99
plugin:

0 commit comments

Comments
 (0)