Here you can check other app installed in your ANDROID device using below flutter code without packages.
Certainly! Here's the Kotlin version of the Android code:
kotlin:
import android.content.pm.PackageManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val CHANNEL = "app_availability"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "isAppInstalled") {
val packageName = call.arguments.toString()
val isInstalled = isAppInstalled(packageName)
result.success(isInstalled)
} else {
result.notImplemented()
}
}
}
private fun isAppInstalled(packageName: String): Boolean {
return try {
packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
}
In this Kotlin code:
- We define the `MainActivity` class that extends `FlutterActivity`.
- We create a method channel named `app_availability`.
- In the `configureFlutterEngine` method, we set the method call handler to handle method calls from Flutter.
- In the `isAppInstalled` function, we use Kotlin's `try-catch` block to catch the `NameNotFoundException` if the package is not found, and return `false` accordingly. Otherwise, we return `true`.
- `packageManager` is directly accessible without importing anything because it's available in the Android context.
How to Use Dart Code Using above KOtlin Code
import 'package:flutter/services.dart';
final MethodChannel _channel = MethodChannel('app_availability');
Future<bool> isAppInstalled(String packageName) async {
try {
final bool result = await _channel.invokeMethod('isAppInstalled', packageName);
return result;
} on PlatformException catch (e) {
print("Failed to check app availability: '${e.message}'.");
return false;
}
}
You can Use above method to retrive data from kotlin laungage for get App availability in my Android device using flutter
Flutter screen call isAppInstalled() method using Elevated button
In this method, you would need the app's package name/your.bundle.id (e.g., com.example.yourapp
).
Dart:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('App Availability Checker'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
bool isInstalled = await isAppInstalled("your.bundle.id"); // Replace "your.bundle.id" with the bundle ID of the app you want to check
if (isInstalled) {
print("App is installed on the device");
} else {
print("App is not installed on the device");
}
},
child: Text('Check App Availability'),
),
),
),
);
}
Future<bool> isAppInstalled(String bundleId) async {
try {
return await MethodChannel('app_availability').invokeMethod('isAppInstalled', bundleId);
} catch (e) {
print("Error checking app availability: $e");
return false;
}
}
}
With Packages
To check if an app is available or installed on a device (specifically iOS) using Flutter, you can use the url_launcher
or device_apps
package.
Notes:
- Custom URL Scheme: Some apps have custom URL schemes (like
whatsapp://
,fb://
, etc.). If you know the URL scheme of the app you want to check, this can be a simpler solution. - App Package Name: For Android, using package names is more difficult due to restrictions, and it's not as direct as it is on Android. The
device_apps
package is more useful on Android, but for android, you may rely onurl_launcher
or check App Store availability using their Play Store links.
Thank You