Skip to content
Open
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ First, add `flutter_appavailability` as a [dependency in your pubspec.yaml file]
- `checkAvailability(String uri)`
- `getInstalledApps()` (only for **Android**)
- `isAppEnabled(String uri)` (only for **Android**)
- `launchApp(String uri)`
- `launchApp(String uri, String activity)`

See the [docs](https://pub.dartlang.org/documentation/flutter_appavailability/latest/).

Expand Down Expand Up @@ -132,6 +132,13 @@ class _MyAppState extends State<MyApp> {

```

Example starting specific activity on Android. (this is useful if you want to start a specific activity of the app or the app doesn't have a launch intent and is not visible in a launcher)
```dart

AppAvailability.launchApp('com.example.helloworld', 'com.example.helloworld.settings.main.SettingsActivity');

```

Android:
![screenshot_1536780581](https://user-images.githubusercontent.com/5956938/45448682-48c49e80-b6d3-11e8-8e56-5972b017e233.png)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.ApplicationInfo;
Expand All @@ -40,6 +41,7 @@ public static void registerWith(Registrar registrar) {
@Override
public void onMethodCall(MethodCall call, Result result) {
String uriSchema;
String activityName;
switch (call.method) {
case "checkAvailability":
uriSchema = call.argument("uri").toString();
Expand All @@ -54,7 +56,8 @@ public void onMethodCall(MethodCall call, Result result) {
break;
case "launchApp":
uriSchema = call.argument("uri").toString();
this.launchApp(uriSchema , result);
activityName = call.argument("activity").toString();
this.launchApp(uriSchema, activityName, result);
break;
default:
result.notImplemented();
Expand Down Expand Up @@ -127,19 +130,54 @@ private void isAppEnabled(String packageName, Result result) {
}

@TargetApi(Build.VERSION_CODES.CUPCAKE)
private void launchApp(String packageName, Result result) {
PackageInfo info = getAppPackageInfo(packageName);
private void launchApp(String packageName, String activityName, Result result) {

if(info != null) {
Intent launchIntent = registrar.context().getPackageManager().getLaunchIntentForPackage(packageName);
if (launchIntent != null) {
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
registrar.context().startActivity(launchIntent);
//Start specific activity of the app
if(activityName != null){
try {
launchActivity(packageName, activityName);
result.success(null);
return;
} catch (Exception e) {
e.printStackTrace();
}
} else {
PackageInfo info = getAppPackageInfo(packageName);

if(info != null) {
Intent launchIntent = registrar.context().getPackageManager().getLaunchIntentForPackage(packageName);
if (launchIntent != null) {
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
registrar.context().startActivity(launchIntent);
result.success(null);
return;
}

//this will try to launch any available activity for the app if getLaunchIntentForPackage returns null, useful for launching apps without Launcher intent
if(info.activities != null){
for (ActivityInfo activityInfo:
info.activities) {
try {
launchActivity(packageName, activityInfo.name);
result.success(null);
break;
} catch (Exception e) {
e.printStackTrace();
}
}
return;
}
}
}

result.error("", "App not found " + packageName, null);
}

@TargetApi(Build.VERSION_CODES.CUPCAKE)
private void launchActivity(String packageName, String activityName) {
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setClassName(packageName, activityName);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
registrar.context().startActivity(launchIntent);
}
}
5 changes: 3 additions & 2 deletions lib/flutter_appavailability.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ class AppAvailability {
return await _channel.invokeMethod("isAppEnabled", args);
}

/// Launch an app with the given [uri] scheme if it exists.
/// Launch an app with the given [uri] scheme if it exists or if [activity] name is provided [android only].
///
/// If the app app isn't found, then a [PlatformException] is thrown.
static Future<void> launchApp(String uri) async {
static Future<void> launchApp(String uri, String activity) async {
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('uri', () => uri);
if (Platform.isAndroid) {
args.putIfAbsent('activity', () => activity);
await _channel.invokeMethod("launchApp", args);
}
else if (Platform.isIOS) {
Expand Down