我们如何从另一个应用程序启动的应用程序获得响应?
How can we get response from an app which was launched by another app?
有人可以帮助我如何从我的应用程序启动的应用程序中获取数据。
我实际上正在尝试将 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 应用程序中执行此操作。
您可以只实现一个启动 Intent
并等待结果的平台通道。
示例:
Android代码-
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/-'),
),
),
);
}
}
希望对您有所帮助!
有人可以帮助我如何从我的应用程序启动的应用程序中获取数据。
我实际上正在尝试将 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 应用程序中执行此操作。
您可以只实现一个启动 Intent
并等待结果的平台通道。
示例:
Android代码-
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/-'),
),
),
);
}
}
希望对您有所帮助!