In My App I have one Payment Which i show available payment related application show how i can show this application in flutter.
Future<void> _getPaymentApps() async {
try {
final List<dynamic> apps = await platform.invokeMethod('getPaymentApps');
setState(() {
_paymentApps = List<String>.from(apps);
});
} on PlatformException catch (e) {
print("Failed to get payment apps: '${e.message}'.");
}
}
I use this code for method channel but it not work
And Create a chanel in Native code
private static final String CHANNEL = "com.example.payment_gateways/paymentApps";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("getPaymentApps")) {
List<String> paymentApps = getPaymentApps();
result.success(paymentApps);
} else {
result.notImplemented();
}
}
}
);
}
private List<String> getPaymentApps() {
List<String> paymentApps = new ArrayList<>();
Intent intent = new Intent(Intent.ACTION_VIEW);
PackageManager pm = getPackageManager();
List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(intent, PackageManager.MATCH_ALL);
for (ResolveInfo resolveInfo : resolveInfoList) {
paymentApps.add(resolveInfo.activityInfo.packageName);
}
return paymentApps;
}
First : add this permission in AndroidManifest.xml
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
Second : add this in your MainActivity.kt
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 = "com.example.payment_gateways/paymentApps"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
CHANNEL
).setMethodCallHandler { call, result ->
if (call.method == "getPaymentApps") {
result.success(getPaymentApps())
} else {
result.notImplemented()
}
}
}
private fun getPaymentApps(): List<String> {
val paymentApps = listOf(
"com.google.android.apps.nbu.paisa.user", // this is for Google Pay
"com.phonepe.app", //this is for PhonePe
"net.one97.paytm", // this is for Paytm
// You can add other payment app package names
)
val installedPaymentApps = mutableListOf<String>()
val packageManager = packageManager
val packages = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES)
for (app in paymentApps) {
try {
packageManager.getPackageInfo(app, PackageManager.GET_ACTIVITIES)
installedPaymentApps.add(app)
} catch (e: PackageManager.NameNotFoundException) {
// App is not installed
}
}
return installedPaymentApps
}
}
Third : Create a dart file and add this
class PaymentAppChecker {
static const platform = MethodChannel('com.example.payment_gateways/paymentApps');
static Future<List<String>> getPaymentApps() async {
try {
final List<String>? installedApps = await platform.invokeListMethod<String>('getPaymentApps');
return installedApps ?? [];
} on PlatformException catch (e) {
print("Failed to get installed payment apps: '${e.message}'.");
return [];
}
}
}
That's it you can use getPaymentApps for getting the payment related app.