【问题标题】:How read JPendingIntent in Delphi?如何在 Delphi 中阅读 JPendingIntent?
【发布时间】:2016-01-20 11:36:07
【问题描述】:

如何在这段代码中读取deliveryIntent

uses
  Androidapi.JNI.JavaTypes, Androidapi.JNI.Telephony;

    procedure TForm1.Button1Click(Sender: TObject);
var
  smsTo: JString;
  smsManager: JSmsManager;
begin
  smsManager := TJSmsManager.JavaClass.getDefault;
  smsTo := StringToJString(ENum.text);
  PendIntent1:=TJPendingIntent.JavaClass.getBroadcast();
  PendIntent2:=TJPendingIntent.JavaClass.getBroadcast();
  smsManager.sendTextMessage(smsTo, nil, StringToJString(MSms.lines.text),PendIntent1,PendIntent2);
end;

因为我知道 SMS 是否消失了?

谢谢

【问题讨论】:

    标签: android delphi sms sendmessage


    【解决方案1】:

    如果您阅读SmsManager.sendTextMessage() documentation,您应该提供自己的Intent 对象(在Delphi 中为JIntent),这些对象包装在PendingIntent 对象中(在Delphi 中为JPendingIntent)。

    因此,您将创建两个分配有所需操作字符串的 Intent 对象,然后使用 PendingIntent.getBroadcast() 方法(在 Delphi 中为 TJPendingIntent.JavaClass.getBroadcast())将它们包装在两个 PendingIntent 对象中,然后分别在sentIntentdeliveryIntent 参数中将它们传递给sendTextMessage()

    短信发送/失败后,sentIntent 参数内的Intent 将被广播。如果短信成功发送到收件人,则deliveryIntent参数内的Intent将被广播。

    要处理这些Intent 对象,您需要在您的应用程序中创建并注册broadcastReceiver。当您收到广播的Intent 时,您可以检索其action 值(参见Intent.getAction() 方法)并采取相应措施。

    不涉及 String 转换,除非您想在 Delphi String 和 Android JString 之间进行转换。

    【讨论】:

    • 按照你的说法,我改了代码。纠正了吗?如何显示广播?发送短信看不到这样的结果。
    • 您必须使用要捕获的意图创建待处理的意图,就像雷米说的那样,您需要使用广播接收器fmxexpress.com/…
    • @A.K:再仔细阅读我的回答。您必须首先创建 2 个 Intent 对象,THEN 创建 2 个 PendingIntent 对象来包装这些 Intent 对象,THENPendingIntent 对象传递给 @987654352 @ 并使用 broadcastReceiver 接收 Intent 对象。您没有创建 Intent 对象。
    【解决方案2】:

    这是我在 Delphi 10.1 Berlin 上的做法:

    1. 基于Java代码示例,我取了herehere
    2. 这里是德尔福广播接收器的完整代码。使用此代码,您可以捕获意图和 ResultCode (getResultCode)。 Here 是一个如何实现它的简单示例。

    单位

    unit BroadcastReceiver;
    
    interface
    
    {$IFDEF ANDROID}
    uses
      Androidapi.JNI.Embarcadero, Androidapi.JNI.GraphicsContentViewText, Androidapi.helpers,
      Androidapi.JNIBridge, FMX.Helpers.Android, Androidapi.JNI.JavaTypes,
      System.Classes, System.SysUtils;
    
    type
      TBroadcastReceiver = class;
    
      TListener = class(TJavaLocal, JFMXBroadcastReceiverListener)
      private
        fOwner: TBroadcastReceiver;
        fReceiver: JFMXBroadcastReceiver;
      public
        constructor Create(aOwner: TBroadcastReceiver);
        destructor Destroy; override;
        procedure onReceive(context: JContext; intent: JIntent); cdecl;
      end;
    
    
      TOnReceive = procedure (aContext: JContext; aIntent: JIntent; aResultCode: integer) of object;
    
      TBroadcastReceiver = class
      private
        fListener : TListener;
        fRegistered: boolean;
        fOnReceive: TOnReceive;
      public
        constructor Create(aOnReceiveProc: TOnReceive);
        destructor Destroy; override;
        procedure AddActions(const Args: array of JString);
        procedure SendBroadcast(const aValue: string);
      end;
    {$ENDIF}
    
    implementation
    
    {$IFDEF ANDROID}
    
    { TBroadcastReceiver }
    
    constructor TBroadcastReceiver.Create(aOnReceiveProc: TOnReceive);
    begin
      inherited Create;
      fListener := TListener.Create(Self);
      fOnReceive := aOnReceiveProc;
    end;
    
    destructor TBroadcastReceiver.Destroy;
    begin
      fListener.Free;
      inherited;
    end;
    
    procedure TBroadcastReceiver.AddActions(const Args: array of JString);
    var
      vFilter: JIntentFilter;
      i: Integer;
    begin
      if fRegistered then
        TAndroidHelper.context.getApplicationContext.UnregisterReceiver(fListener.fReceiver);
    
      vFilter := TJIntentFilter.JavaClass.init;
      for i := 0 to High(Args) do
        vFilter.addAction(Args[i]);
    
      TAndroidHelper.context.getApplicationContext.registerReceiver(fListener.fReceiver, vFilter);
      fRegistered := true;
    end;
    
    procedure TBroadcastReceiver.SendBroadcast(const aValue: string);
    var
      Inx: JIntent;
    begin
      Inx := TJIntent.Create;
      Inx.setAction(StringToJString(aValue));
      TAndroidHelper.Context.sendBroadcast(Inx);
    end;
    
    constructor TListener.Create(aOwner: TBroadcastReceiver);
    begin
      inherited Create;
      fOwner := aOwner;
      fReceiver := TJFMXBroadcastReceiver.JavaClass.init(Self);
    end;
    
    destructor TListener.Destroy;
    begin
      TAndroidHelper.context.getApplicationContext.unregisterReceiver(fReceiver);
      inherited;
    end;
    
    // usually android call it from "UI thread" - it's not main Delphi thread
    procedure TListener.onReceive(context: JContext; intent: JIntent);
    begin
      if Assigned(fOwner.fOnReceive) then
        fOwner.fOnReceive(Context, Intent, fReceiver.getResultCode);
    end;
    
    {$ENDIF}
    
    end.
    

    然后

      fBroadcast := TBroadcastReceiver.Create(OnReceiveBroadcast);
      fBroadcast.AddActions([StringToJString(SENT_ACTION)]);
    

    SENT_ACTION - 可以是任何字符串,例如'SENT' 或 123 - 这是过滤器的 ID。

    然后我发送它:

    procedure TAndroidSMS.SendSMS(const aText, aPhone: string);
    var
      Intent: JIntent;
      PendingIntent: JPendingIntent;
      vSmsManager: JSmsManager;
    begin
      Result := seNotUpdated;
      Intent := TJIntent.Create;
      Intent.setAction(StringToJString(SENT_ACTION));
     // Intent.putExtra( StringToJString(EXTRA_PHONE_PARAM), StringTojString(aPhone));
    
      PendingIntent := TJPendingIntent.JavaClass.getBroadcast(TAndroidHelper.Context, 0, Intent, 0);
    
      vSmsManager := TJSmsManager.JavaClass.getDefault;
      vSmsManager.sendTextMessage( StringToJString(aPhone), nil, StringToJString(aText), PendingIntent, nil);
    end;
    

    并使用广播接收器接收:

    { usually android call it from "UI thread" - it's not main Delphi thread}
    procedure TAndroidSMS.OnReceiveBroadcast(aContext: JContext; aIntent: JIntent; aResultCode: integer);
    begin
     // vAction := JStringToString(aIntent.getAction);
     // vNum := JStringToString(aIntent.getStringExtra( StringToJString(EXTRA_PHONE_PARAM) ) );
    
       // now change value, another thread is cheking periodically for this flag
        {Intel, Arm, write to a single byte, or to a properly aligned 2- or 4-byte value will always be an atomic write.}
      if aResultCode = TJActivity.JavaClass.RESULT_OK then
        Result := seSent
      else if aResultCode = TJSmsManager.JavaClass.RESULT_ERROR_RADIO_OFF then
        Result := seRadioOff
      else if aResultCode = TJSmsManager.JavaClass.RESULT_ERROR_GENERIC_FAILURE then
        Result := seGenericFail
      else if aResultCode = TJSmsManager.JavaClass.RESULT_ERROR_NO_SERVICE then
        Result := seNoService
      else if aResultCode = TJSmsManager.JavaClass.RESULT_ERROR_NULL_PDU then
        Result := seNullPDU
      else Result := seUnknown;
     end;
    

    【讨论】:

    • 像魅力一样工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多