Поиск сообщества
Показаны результаты для тегов 'broadcast receiver'.
Найдено: 1 результат
-
Как создать простейший Android Broadcast Receiver. Создайте класс, для приема Intent уведомлений: uses Androidapi.JNIBridge, Androidapi.JNI.Embarcadero, Androidapi.JNI.GraphicsContentViewText; type TMyReceiver = class(TJavaLocal, JFMXBroadcastReceiverListener) public constructor Create; procedure onReceive(context: JContext; intent: JIntent); cdecl; end; uses Androidapi.Helpers, Androidapi.JNI.JavaTypes; { TMyReceiver } constructor TMyReceiver.Create; begin inherited; end; procedure TMyReceiver.onReceive(context: JContext; intent: JIntent); begin Log.d('Broadcast Received = ' + JStringToString(intent.getAction)); end; Регистрируем тип уведомлений и приемник в событиях формы: type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { private } FMyListener: TMyReceiver; FBroadcastReceiver: JFMXBroadcastReceiver; public { public } end; procedure TForm1.FormCreate(Sender: TObject); var Filter: JIntentFilter; begin FMyListener := TMyReceiver.Create; FBroadcastReceiver := TJFMXBroadcastReceiver.JavaClass.init(FMyListener); Filter := TJIntentFilter.JavaClass.init; Filter.addAction(TJIntent.JavaClass.ACTION_SCREEN_OFF); Filter.addAction(TJIntent.JavaClass.ACTION_SCREEN_ON); TAndroidHelper.context.getApplicationContext.registerReceiver (FBroadcastReceiver, Filter); end; procedure TForm1.FormDestroy(Sender: TObject); begin TAndroidHelper.context.getApplicationContext.unregisterReceiver (FBroadcastReceiver); end; Все. Теперь когда вы запустите программу и выключите\включите экран, лог покажет : FMX: BroadcastSample: Broadcast Received = android.intent.action.SCREEN_OFF FMX: BroadcastSample: Broadcast Received = android.intent.action.SCREEN_ON Takashi Yamamoto