本文整理自网络,侵删。
如何使用Delphi 10 Seattle的Android应用程序接收Intent。
我以前编写的 “ 使用Firephionek Android应用程序接收Delphi XE8的Intent ”中没有处理Activity.OnNewIntent的问题。
发送字符串到另一个应用程序创建一个新的多设备应用程序并将TButton和TMemo放置在窗体上。
intent01
按下按钮后,输入的字符串将发送到另一个应用程序。
描述按下Button1时的事件。
创建一个JIntent并设置要发送的数据。
Intent := TJIntent.Create; Intent.setType(StringToJString('text/plain')); Intent.setAction(TJIntent.JavaClass.ACTION_SEND); Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(AText));使用Android API PackageManager类的queryIntentActivities方法,确认是否存在可以处理该意图的应用程序。如果有可以处理的应用程序,请发送意图。
if MainActivity.getPackageManager.queryIntentActivities(Intent, TJPackageManager.JavaClass.MATCH_DEFAULT_ONLY).size > 0 then MainActivity.startActivity(Intent) else ShowMessage('Receiver not found');源代码如下所示:
uses Androidapi.JNI.GraphicsContentViewText, // JIntent Androidapi.Helpers, // StringToJString FMX.Platform.Android; // MainActivity
procedure TForm1.Button1Click(Sender: TObject);var AText: string; Intent: JIntent;begin AText := Memo1.Text;
Intent := TJIntent.Create; Intent.setType(StringToJString('text/plain')); Intent.setAction(TJIntent.JavaClass.ACTION_SEND); Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(AText));
if MainActivity.getPackageManager.queryIntentActivities(Intent, TJPackageManager.JavaClass.MATCH_DEFAULT_ONLY).size > 0 then MainActivity.startActivity(Intent) else ShowMessage('Receiver not found');end;intent02
intent03
接收发送的字符串创建一个新的多设备应用程序并将TMemo放置在表单上。
intent04
编辑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" /> </intent-filter> //↓追加 <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> 现在您可以收到意向书。
接收字符串从意图接收字符串。
描述表单的OnCreate事件。
设置应用程序的事件处理程序(HandleAppEvent)以在应用程序变为活动状态时获取事件。
procedure TForm2.FormCreate(Sender: TObject);var AppEventService: IFMXApplicationEventService;begin if TPlatformServices.Current.SupportsPlatformService (IFMXApplicationEventService, AppEventService) then AppEventService.SetApplicationEventHandler(HandleAppEvent);end;将HandleAppEvent方法添加到窗体。
type TForm2 = class(TForm) private function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;当应用程序处于活动状态时,如果有意图,请调用HandleIntentAction方法。
function TForm2.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;将HandleIntentAction方法添加到窗体。
type TForm2 = class(TForm) private function HandleIntentAction(const Data: JIntent): Boolean;检索从意图发送的字符串。
function TForm2.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;当应用程序处于关闭状态时,它可以正常工作,但是在应用程序等待时,无法接收意图。
当正在等待的应用程序收到意图时,将调用活动的OnNewIntent。 允许在调用OnNewIntent时处理意图。
将以下代码添加到窗体的OnCreate事件。在MainActivity的registerIntentAction方法中接收TJIntent.JavaClass.ACTION_SEND。结果,当调用Activity的OnNewIntent时,如果有TJIntent.JavaClass.ACTION_SEND的意图,则会有一个通知。使用HandleActivityMessage方法接收通知。
procedure TForm2.FormCreate(Sender: TObject);var AppEventService: IFMXApplicationEventService;begin if TPlatformServices.Current.SupportsPlatformService (IFMXApplicationEventService, AppEventService) then AppEventService.SetApplicationEventHandler(HandleAppEvent);
//次のコ?`ドを追加 MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_SEND); TMessageManager.DefaultManager.SubscribeToMessage (TMessageReceivedNotification, HandleActivityMessage);end;将HandleActivityMessage方法添加到窗体。
type TForm2 = class(TForm) private procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);如果在此处调用HandleIntentAction方法,则在应用程序处于活动状态时,它将被HandleAppEvent方法覆盖。因此,当您收到该意图时,请更新MainActivity意图。*这里的处理没有得到充分验证。可能有问题。
procedure TForm2.HandleActivityMessage(const Sender: TObject; const M: TMessage);var Intent: JIntent;begin if M is TMessageReceivedNotification then begin Intent := TMessageReceivedNotification(M).Value; if Intent <> nil then MainActivity.setIntent(Intent); end;end;
相关阅读 >>
Delphi 32位程序 out of memory 内存不足时解决办法之一
更多相关阅读请进入《Delphi》频道 >>