英文:
Delphi x Android Intents
问题
我需要帮助处理我的应用程序。该应用程序在Sunmi设备上运行,我想要集成支付(另一个应用程序)。
我的发送意图的代码是正确的。但是我需要接收一个意图响应。
清单:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="pay-response"
android:scheme="scheme_return" />
</intent-filter>
发送意图:
uri := 'payment-app://pay?return_scheme=scheme_return&amount=' + valorTotal + '&transaction_type=debit';
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW, TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(uri))));
SharedActivity.startActivity(Intent);
在我的 "uri" 中,我有一个 return_scheme 参数。如果该参数的值与清单中的 android:scheme="scheme_return" 相同,支付将在支付应用程序中完成,当我的应用程序返回时,该应用程序会崩溃(冻结)。
如果我将 return_scheme 参数更改为任何其他值,则支付完成后,我的应用程序会正确打开。
我认为应用程序正在等待一个意图响应。但是如何获取这个答案?
--------编辑---------
我的接收意图的代码:
procedure TForm1.FormCreate(Sender: TObject);
var
AppEventService: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, AppEventService) then
AppEventService.SetApplicationEventHandler(HandleAppEvent);
MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_VIEW);
TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, HandleActivityMessage);
end;
procedure TForm1.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
if M is TMessageReceivedNotification then
HandleIntentAction(TMessageReceivedNotification(M).Value);
end;
function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
var
StartupIntent: JIntent;
begin
Result := False;
if AAppEvent = TApplicationEvent.BecameActive then
begin
StartupIntent := MainActivity.getIntent;
if StartupIntent <> nil then
HandleIntentAction(StartupIntent);
end;
end;
function TForm1.HandleIntentAction(const Data: JIntent): Boolean;
var
Extras: JBundle;
begin
Result := False;
if Data <> nil then
begin
Memo1.ClearContent;
Extras := Data.getExtras;
if Extras <> nil then
Memo1.Text := JStringToString(Extras.getString(TJIntent.JavaClass.EXTRA_TEXT));
Invalidate;
end;
end;
支付完成后,我在 HandleIntentAction
中接收到意图,但始终为 nil
。
SDK参考(使用Kotlin):https://sdkandroid.stone.com.br/reference/pagamento-deeplink
英文:
I need help with my app. This app run in Sunmi Device and I would like a payment integration (another app).
My code for send intent its ok. But I need to receive an intent response.
Manifest:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="pay-response"
android:scheme="scheme_return" />
</intent-filter>
Send intent:
uri :='payment-app://pay?return_scheme=scheme_return&amount=' + valorTotal + '&transaction_type=debit';
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW, TJnet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(uri))));
SharedActivity.startActivity(Intent);
In my "uri" I have a return_scheme parameter. If the value of the parameter is the same as the manifest android:scheme="scheme_return", the payment is finished in the payment app, when my app return, the app is crashed (frozen).
If I change return_scheme parameter of any other value, when finished payment, my app open correctly.
I think the app is waiting on an intent response.
But how to get this answer?
--------EDIT---------
My code for receive intent:
procedure TForm1.FormCreate(Sender: TObject);
var
AppEventService: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, AppEventService) then
AppEventService.SetApplicationEventHandler(HandleAppEvent);
MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_VIEW);
TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, HandleActivityMessage);
end;
procedure TForm1.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
if M is TMessageReceivedNotification then
HandleIntentAction(TMessageReceivedNotification(M).Value);
end;
function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
var
StartupIntent: JIntent;
begin
Result := False;
if AAppEvent = TApplicationEvent.BecameActive then
begin
StartupIntent := MainActivity.getIntent;
if StartupIntent <> nil then
HandleIntentAction(StartupIntent);
end;
end;
function TForm1.HandleIntentAction(const Data: JIntent): Boolean;
var
Extras: JBundle;
begin
Result := False;
if Data <> nil then
begin
Memo1.ClearContent;
Extras := Data.getExtras;
if Extras <> nil then
Memo1.Text := JStringToString(Extras.getString(TJIntent.JavaClass.EXTRA_TEXT));
Invalidate;
end;
end;
After payment is finished, i received intent in HandleIntentAction
, but always is nil
.
SDK Reference (in Kotlin): https://sdkandroid.stone.com.br/reference/pagamento-deeplink
答案1
得分: 1
首先,考虑以下步骤:
- 当您启动付款活动时,请使用
startActivityForResult
而不是startActivity
:
int PAYMENT_REQUEST_CODE = 123;
String uri = "payment-app://pay?return_scheme=scheme_return&amount=" + valorTotal + "&transaction_type=debit";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivityForResult(intent, PAYMENT_REQUEST_CODE); // <-- 这里
- 如果可能的话,请确保付款应用正确地返回结果。它应该使用
setResult
来设置结果并调用finish
来关闭其活动:
// 在处理付款逻辑的付款应用的活动中
Intent resultIntent = new Intent();
resultIntent.putExtra("status", "success"); // 如果需要,您可以添加任何额外的数据
setResult(RESULT_OK, resultIntent);
finish();
通过使用startActivityForResult
并实现onActivityResult
,您的应用程序将能够适当地处理来自付款应用的响应。
希望这有所帮助。
问候,
英文:
First of all, take into consideration the following steps:
- When you start the payment activity, use startActivityForResult instead of startActivity:
int PAYMENT_REQUEST_CODE = 123;
String uri = "payment-app://pay?return_scheme=scheme_return&amount=" + valorTotal + "&transaction_type=debit";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivityForResult(intent, PAYMENT_REQUEST_CODE); <--- Aqui
- If possible, make sure that the payment app sends back the result correctly. It should set the result using setResult and call finish to close its activity:
// In the payment app's activity that handles the payment logic
Intent resultIntent = new Intent();
resultIntent.putExtra("status", "success"); // You can add any extra data if needed
setResult(RESULT_OK, resultIntent);
finish();
By using startActivityForResult and implementing onActivityResult, your app will be able to handle the response from the payment app appropriately.
I hope this helps.
Regards,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论