Delphi XE8のFiremoneky AndroidアプリケーションでIntentを受信するには

Delphi XE8のFiremoneky AndroidアプリケーションでIntentを受信する方法です。

Intentを送信するアプリケーション

最初に文字列をIntentで送信するサンプルプログラムを作成します。
このサンプルプログラムで送信した文字列を受信するアプリケーションを作成します。

フォームにTButtonコンポーネントを一つ配置します。

uses
  Androidapi.JNI.GraphicsContentViewText, // JIntent
  Androidapi.Helpers; // StringToJString

procedure TForm1.Button1Click(Sender: TObject);
var
  sendIntent: JIntent;
begin
  sendIntent := TJIntent.Create;
  sendIntent.setAction(TJIntent.JavaClass.ACTION_SEND);
  sendIntent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString('テスト'));
  sendIntent.setType(StringToJString('text/plain'));
  SharedActivity.startActivity(sendIntent);
end;

Intentを受信するアプリケーション

次にIntentを受信するアプリケーションを作成します。

新規にアプリケーションを作成し、TMemoコンポーネントを配置します。

Intentを受信するアプリケーションのAndroidManifest.template.xmlを編集します。
AndroidManifest.template.xmlはプロジェクトをビルドすると生成されます。

AndroidManifest.template.xmlをエディタで開き、次の行を追加します。

        <intent-filter>  
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.SEND" /> <=追加
            <category android:name="android.intent.category.DEFAULT" /> <=追加
            <data android:mimeType="text/*" /> <=追加
        </intent-filter> 

アプリケーションがアクティブになったときのイベントを取得できるようにします。

type
  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
    …
    function HandleAppEvent(AAppEvent: TApplicationEvent;
      AContext: TObject): Boolean;
  end;

procedure TForm2.FormCreate(Sender: TObject);
var
  AService: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService
    (IFMXApplicationEventService, IInterface(AService)) then
    AService.SetApplicationEventHandler(HandleAppEvent);
end;

function TForm2.HandleAppEvent(AAppEvent: TApplicationEvent;
  AContext: TObject): Boolean;
begin
  if AAppEvent = TApplicationEvent.BecameActive then
  begin
    //アプリケーションがアクティブになったときの処理
  end;
end;

Intentを受信する処理を記述します。

uses
  Androidapi.JNI.GraphicsContentViewText, // JIntent
  Androidapi.JNI.Os, // JBundle
  Androidapi.JNI.JavaTypes, // JString
  Androidapi.Helpers; // SharedActivity JStringToString

function TForm2.HandleAppEvent(AAppEvent: TApplicationEvent;
  AContext: TObject): Boolean;
var
  Intent: JIntent;
  Extras: JBundle;
  S: JString;
begin
  Result := False;

  if AAppEvent = TApplicationEvent.BecameActive then
  begin
    Intent := SharedActivity.getIntent;
    if Intent <> nil then
    begin
      if TJIntent.JavaClass.ACTION_SEND.equals(Intent.getAction) then
      begin
        Extras := Intent.getExtras;
        if Extras <> nil then
        begin
          S := Extras.getString(TJIntent.JavaClass.EXTRA_TEXT);
          if S <> nil then
          begin
            // 受信した文字列をMemo1に表示
            Memo1.Text := JStringToString(S);
          end;
        end;
      end;
    end;
  end;
end;

Intent送信側アプリケーションから送信すると、

intent-1

受信側アプリケーションで受信できました。

intent-2

2015年10月19日 追記:
アプリケーションが待機中の時にインテントを受信できていませんでした。
正しい方法は「Delphi 10 SeattleのAndroidアプリケーションでIntentの送受信をするには」をご覧ください。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください