Delphi x Android Intents

huangapple go评论77阅读模式
英文:

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:

&lt;intent-filter&gt;
    &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
    &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
    &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
    &lt;data
        android:host=&quot;pay-response&quot;
        android:scheme=&quot;scheme_return&quot; /&gt;
&lt;/intent-filter&gt;

Send intent:

uri :=&#39;payment-app://pay?return_scheme=scheme_return&amp;amount=&#39; + valorTotal + &#39;&amp;transaction_type=debit&#39;;

  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 &lt;&gt; nil then
      HandleIntentAction(StartupIntent);
  end;
end;

function TForm1.HandleIntentAction(const Data: JIntent): Boolean;
var
  Extras: JBundle;
begin
  Result := False;
  if Data &lt;&gt; nil then
  begin
    Memo1.ClearContent;
    Extras := Data.getExtras;
    if Extras &lt;&gt; 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

首先,考虑以下步骤:

  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); // <-- 这里
  1. 如果可能的话,请确保付款应用正确地返回结果。它应该使用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:

  1. When you start the payment activity, use startActivityForResult instead of startActivity:
int PAYMENT_REQUEST_CODE = 123;
String uri = &quot;payment-app://pay?return_scheme=scheme_return&amp;amount=&quot; + valorTotal + &quot;&amp;transaction_type=debit&quot;;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivityForResult(intent, PAYMENT_REQUEST_CODE); &lt;--- Aqui
  1. 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&#39;s activity that handles the payment logic
Intent resultIntent = new Intent();
resultIntent.putExtra(&quot;status&quot;, &quot;success&quot;); // 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,

huangapple
  • 本文由 发表于 2023年7月13日 21:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定