gpt4 book ai didi

dart - 我们如何从另一个应用程序启动的应用程序中获得响应?

转载 作者:行者123 更新时间:2023-12-03 04:16:58 25 4
gpt4 key购买 nike

请有人可以帮助我如何从我的应用程序启动的应用程序中获取数据。

我实际上正在尝试将 Google 的 Tez 集成到我的 flutter 应用程序中。

我已成功启动该应用程序以向请求的 UPI 付款。但是,我想得到 Tez 对我的应用程序的响应。

我已经使用 url_launcher 在我的 flutter 应用程序中启动 Tez 应用程序。
不幸的是,我无法听到 Tez 应用程序的任何回复。
我尝试使用 StreamSubscription 但无济于事。

我已经发布了代码以供引用。

代码:

import 'dart:async';

import 'dart:io';

import 'package:flutter/material.dart';

import 'package:url_launcher/url_launcher.dart';

void main() {
runApp(new MaterialApp(
title: 'UPI Demo',
home: new UpiDemo(),
));
}

class UpiDemo extends StatefulWidget {
@override
_UpiDemoState createState() => new _UpiDemoState();
}

class _UpiDemoState extends State<UpiDemo> {
String payeeAddress;
String payeeName;
String transactionNote;
String amount;
String currencyUnit;
HttpClientRequest request;
HttpClientResponse response;
List data = new List();
Uri uri;

StreamSubscription<Map<String, Object>> _paymentSubscription;
Stream<Object> _payment;
Stream<Object> _currentPayment;

@override
void initState() {
super.initState();
payeeAddress = "mobilenumber@upi";
payeeName = "payeename";
transactionNote = "Test for Deeplinking";
amount = "1";
currencyUnit = "INR";

uri = Uri.parse("upi://pay?pa=" +
payeeAddress +
"&pn=" +
payeeName +
"&tn=" +
transactionNote +
"&am=" +
amount +
"&cu=" +
currencyUnit);
}

@override
void dispose() {
super.dispose();
request.close();
}
Future launchTez()async {
try {
if (await canLaunch(uri.toString())) {

_paymentSubscription = await launch(uri.toString()).asStream().listen((var result){
setState((){
_currentPayment = result;
print(_currentPayment);
});
});
setState((){
_currentPayment = _payment;
});

} else {
throw 'Could not launch tez ';
}
} catch (exception) {
print(exception);
}
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('UPI Demo'),
),
body: new Center(
child: new RaisedButton(
onPressed: (() {
launchTez();
}),
child: new Text('Click to Pay Rs.1.0/-'),
),
),
);
}
}

启动应用程序后,我想听取响应,就像来自 github here 的这个示例一样

当 Tez 应用程序启动时, flutter 应用程序停止并在 Tez 上完成交易时, flutter 应用程序恢复。

请有人告诉我我该怎么做。 android 示例实际上是在听响应,但我无法在 flutter 应用程序中做到这一点。

最佳答案

您可以只实现一个平台 channel 来启动 Intent并等待结果。

例子:

安卓代码 -

package com.yourcompany.example;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.util.Log;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "upi/tez";

private MethodChannel.Result callResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("launchUpi")) {
Uri uri = Uri.parse(call.argument("url").toString());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivityForResult(intent,1);
Log.d("Call", "onMethodCall: launchUpi");
callResult = result;
} else {
result.notImplemented();
}
}
});
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Log.d("Result","Data");
if(data!=null && callResult!=null) {
String res = data.getStringExtra("response");
String search = "SUCCESS";
if (res.toLowerCase().contains(search.toLowerCase())) {
callResult.success("Success");
} else {
callResult.success("Failed");
}
}else{
Log.d("Result","Data = null (User canceled)");
callResult.success("User Cancelled");
}
super.onActivityResult(requestCode,resultCode,data);
}
}

Dart 代码 -
void main() {
runApp(new MaterialApp(
title: "Upi Demo",
home: new UpiDemo(),
));
}

class UpiDemo extends StatefulWidget {
@override
_UpiDemoState createState() => new _UpiDemoState();
}

class _UpiDemoState extends State<UpiDemo> {
static const platform = const MethodChannel('upi/tez');
String payeeAddress;
String payeeName;
String transactionNote;
String amount;
String currencyUnit;
Uri uri;

String paymentResponse;

StreamSubscription<Map<String, Object>> _paymentSubscription;
Stream<Object> _payment;
Stream<Object> _currentPayment;

@override
void initState() {
super.initState();
payeeAddress = "mobilenumber@upi";
payeeName = "payeename";
transactionNote = "Test for Deeplinking";
amount = "1";
currencyUnit = "INR";

uri = Uri.parse("upi://pay?pa=" +
payeeAddress +
"&pn=" +
payeeName +
"&tn=" +
transactionNote +
"&am=" +
amount +
"&cu=" +
currencyUnit);
}

@override
void dispose() {
super.dispose();
}

Future launchTez()async {
try {
final String result = await platform.invokeMethod('launchUpi',<String,dynamic>{"url":uri.toString()});
debugPrint(result);
} on PlatformException catch (e) {
debugPrint(e.toString());
}
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('UPI Demo'),
),
body: new Center(
child: new RaisedButton(
onPressed: (() {
launchTez();
}),
child: new Text('Click to Pay Rs.1.0/-'),
),
),
);
}
}

希望有帮助!

关于dart - 我们如何从另一个应用程序启动的应用程序中获得响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48097332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com