如何在Delphi中使用后台服务自动运行应用程序? [英] How to autorun application with background service in Delphi?

查看:206
本文介绍了如何在Delphi中使用后台服务自动运行应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Delphi 10 Seattle中创建一个具有后台服务的android应用程序,该应用程序将在启动android设备后自动启动?



我找到了一个解决方案( 启动后自动启动Delphi XE5 Android应用),但这是针对Delphi XE5版本的,并且没有自动启动后台服务的选项。



您有没有人尝试解决此问题?如果要与我们分享您的解决方案?



已更新:



我不知道怎么了。


  1. 添加BroadcastReceiver以使用


  2. 注册BroadcastReceiver

     过程TForm1.CreateBroadcastReceiver; 
    如果没有分配则开始
    (BroadcastReceiver)然后
    开始
    BroadcastReceiver:= TCSBroadcastReceiver.Create(nil);
    BroadcastReceiver.OnReceive:= BroadcastReceiverOnReceive;
    BroadcastReceiver.RegisterReceive;
    BroadcastReceiver.Add(’android.intent.action.BOOT_COMPLETED');
    结尾;
    结尾;


  3. 设置BroadcastReceiver OnReceive

     过程TForm1.BroadcastReceiverOnReceive(csContext:JContext; csIntent:JIntent); 
    var
    Inx:JIntent;如果JStringToString(csIntent.getAction).Contains(’android.intent.action.BOOT_COMPLETED’),则
    开始
    ,然后
    开始
    Inx:= TJIntent.Create;
    Inx.setClassName(csContext,StringToJString(’com.embarcadero.firemonkey.FMXNativeActivity’));
    Inx.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
    TAndroidHelper.Context.startActivity(Inx);
    结尾;
    结尾;


  4. 更新 AndroidManifest.xml




 < receiver android:name = com.embarcadero.rtl.notifications.NotificationAlarm /> 
< receiver android:name = com.embarcadero.ProjectBCTA
android:permission = android.permission.RECEIVE_BOOT_COMPLETED>
<意图过滤器>
< action android:name = android.intent.action.BOOT_COMPLETED />
< category android:name = android.intent.category.DEFAULT />
< / intent-filter>
< / receiver>




  1. 启用 RECEIVE_BOOT_COMPLETED 权限


  2. 启动应用程序。


  3. 已停止应用程序。


  4. 重新启动计算机。


  5. 重新启动后,系统显示错误消息应用程序停止


我在做什么错误。什么是错误?

解决方案

我成功解决了该问题,在此过程中自动启动了应用程序上周使用Delphi Tokyo 10.2在工作。我目前正在考虑启动后台服务,而不是启动后启动应用程序,因为启动设备每次启动时启动屏幕和应用程序都会弹出。 (我在研究如何仅启动服务时偶然发现了您的问题,我将在下个周末尝试使用建议的 startService Java方法的变体。经历了痛苦之后,我可以减轻您的痛苦。)



基本上,对于较新的编译器,您不再需要Danny Wind文章中的修改 Classes.dex文件的步骤。我在下面描述的内容适用于东京,如果可以相同的方式,也可能适用于西雅图,我认为可以。



您不能使用Delphi代码进行注册广播接收器。这必须使用Java来完成,并且已编译的类位于JAR中。 Danny Wind的Java包将继续发挥作用,但前提是您要在清单文件中包含问题的第4步,并在项目的Android权限中包含问题的第5步。



顺便说一句,您不应编辑 AndroidManifest.xml文件,而应编辑 AndroidManifest.template.xml文件。



如果您了解如何添加用于后台服务的JAR,您可以通过相同的方法,通过转到 Android目标平台下的 Libraries文件夹,然后右键单击并选择添加,为 BOOT_COMPLETED添加包含BroadcastReceiver的JAR。 。在这种情况下,您无需添加后台服务JAR(应该已经在其中了),而是添加包含BroadcastReceiver的JAR。



为了将Java编译为类,然后将其包含在JAR中,我使用以下批处理文件,该文件基于Danny Wind的文章以及Delphi运行的命令来编译其为后台服务生成的Java(您可以在输出选项卡中看到)消息窗口的顶部):

  @ECHO OFF 
rem Android类路径JAR文件
SET ANDROID_JAR = C:\Users\Public\Documents\Embarcadero\Studio\19.0\PlatformSDKs\android-sdk-windows\platforms\android-26\android.jar
rem设置参数
SET SOURCE_FILE =%1
SET JAR_FILE =%2
IF x%2 == x SET JAR_FILE =%SOURCE_FILE%
SET CONFIGURATION =%3
IF x%3 == x SET CONFIGURATION = Debug
rem确保存在用于Java归档文件的目录以保存Java类文件
MKDIR JavaClasses\%JAR_FILE%2> NUL
rem指示操作
ECHO。
ECHO将%SOURCE_FILE%.java编译为%SOURCE_FILE%.class并存储在%CONFIGURATION%\%JAR_FILE%.jar ...
ECHO中。
rem编译Java类文件
%JAVASDK%\javac -Xlint:deprecation -classpath%ANDROID_JAR%-bootclasspath%ANDROID_JAR%-d JavaClasses\%JAR_FILE%-target 1.6 -g -source 1.6%SOURCE_FILE%.java
rem创建包含类文件
的Java存档文件%JAVASDK%\jar cf%CONFIGURATION%\%JAR_FILE%.jar -C .\JavaClasses\% JAR_FILE%com
rem清理
SET ANDROID_JAR =
SET SOURCE_FILE =
SET JAR_FILE =
SET CONFIGURATION =

批处理文件具有3个参数:


  1. Java文件名,不带任何扩展名。

  2. JAR文件名,不带任何扩展名。

  3. 配置名(即调试或发布)

如果只有一个参数,则JAR文件名将与Java文件具有相同的名称,并且配置将设置为调试。



如果只有两个参数,则配置将设置为调试。



注意,批处理-



该批处理文件在调用 javac时使用的参数与Danny Wind的文章中使用的参数略有不同。



这些是我从Delphi IDE的使用中获得的(如输出选项卡所示)。



您将需要更改设置环境变量 ANDROID_JAR的行,以使



您还需要将名为 JAVASDK的环境变量设置为Java SDK的 bin的路径。夹。在我的机器上是 C:\Program Files\Java\jdk1.8.0_60\bin。我通过系统属性/高级/环境变量进行了设置。



编译和存档后,包含BroadcastReceiver的JAR将位于调试或发布中



在您的项目中添加了包含BroadcastReceiver的JAR之后,较新版本的Delphi将自动链接该JAR文件



将APK部署到设备上,至少运行一次应用程序,然后重新启动设备。启动完成后,您应该会看到应用启动。


Is there a possibility to create an android application (with background service) in Delphi 10 Seattle that will automatically start after boot android device?

I found one solution (Auto start Delphi XE5 Android App after boot), but it is for the version Delphi XE5 and there is no option to automatically start the background service.

Any of you tried to solve this problem? If so sharing with us your solution?

UPDATED:

I do not know what's wrong.

  1. Add BroadcastReceiver to uses

  2. Registration BroadcastReceiver

    procedure TForm1.CreateBroadcastReceiver;
    begin
      if not Assigned(BroadcastReceiver) then
        begin
          BroadcastReceiver:= TCSBroadcastReceiver.Create(nil);
          BroadcastReceiver.OnReceive:= BroadcastReceiverOnReceive;
          BroadcastReceiver.RegisterReceive;
          BroadcastReceiver.Add('android.intent.action.BOOT_COMPLETED');
        end;
    end;
    

  3. Setting BroadcastReceiver OnReceive

    procedure TForm1.BroadcastReceiverOnReceive(csContext: JContext; csIntent: JIntent);
    var
      Inx: JIntent;
    begin
      if JStringToString(csIntent.getAction).Contains('android.intent.action.BOOT_COMPLETED') then
        begin
          Inx := TJIntent.Create;
          Inx.setClassName(csContext, StringToJString('com.embarcadero.firemonkey.FMXNativeActivity'));
          Inx.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
          TAndroidHelper.Context.startActivity(Inx);
        end;
    end;
    

  4. Update AndroidManifest.xml

<receiver android:name="com.embarcadero.rtl.notifications.NotificationAlarm" />
<receiver android:name="com.embarcadero.ProjectBCTA"
  android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

  1. Enabled the RECEIVE_BOOT_COMPLETED permission

  2. Launched the application.

  3. Stopped the application.

  4. Rebooted the machine.

  5. After rebooting the system shows an error message Application stopped.

What am I doing wrong. What is error?

解决方案

I successfully worked through the problem to auto-launch an app during last week at work, using Delphi Tokyo 10.2. I'm currently looking into starting the background service, instead of the app after boot-up as the splash-screen and application currently "pop-up" each time the device starts up. (I stumbled on your question while looking into how to start the service only, and I'll try a variation of the suggested "startService" Java approach this coming weekend. Having gone through the pain, I can alleviate yours.)

Basically for the newer compilers, you no longer need the steps in Danny Wind's article that modify the "Classes.dex" file. What I describe below works for Tokyo, and will probably work for Seattle if it works the same way, which I believe it does.

You cannot use the Delphi code to register the BroadcastReceiver. That has to be done in Java, with the compiled class being in a JAR. Danny Wind's Java package will continue to do exactly the trick, on condition that you include step 4 of your question in the manifest file and step 5 of your question in the project's Android permissions.

BTW, you should not be editing the "AndroidManifest.xml" file, but rather the "AndroidManifest.template.xml" file.

If you understand how to add the JAR for the background service, you follow the same approach to add the JAR containing your BroadcastReceiver for "BOOT_COMPLETED", by going to the "Libraries" folder under the "Android" target platform and right-mouse-clicking and choosing "Add". Instead of adding your background-service JAR (which should already be there), in this case you add your JAR containing the BroadcastReceiver.

In order to compile the Java into a class, and then include that in a JAR, I use the following batch-file, which is based on Danny Wind's article and the command that Delphi runs to compile the Java it generates for the background service (which you can see in the Output tab of the Messages window):

@ECHO OFF
rem Path to Android classes JAR-file
SET ANDROID_JAR="C:\Users\Public\Documents\Embarcadero\Studio\19.0\PlatformSDKs\android-sdk-windows\platforms\android-26\android.jar"
rem Setup parameters
SET SOURCE_FILE=%1
SET JAR_FILE=%2
IF x%2 == x SET JAR_FILE=%SOURCE_FILE%
SET CONFIGURATION=%3
IF x%3 == x SET CONFIGURATION=Debug
rem Ensure directory to hold Java Class files exists for Java Archive
MKDIR JavaClasses\%JAR_FILE% 2> NUL
rem Indicate actions
ECHO .
ECHO Compiling %SOURCE_FILE%.java to %SOURCE_FILE%.class and storing in %CONFIGURATION%\%JAR_FILE%.jar...
ECHO .
rem Compile Java Class file
"%JAVASDK%\javac" -Xlint:deprecation -classpath %ANDROID_JAR% -bootclasspath %ANDROID_JAR% -d JavaClasses\%JAR_FILE% -target 1.6 -g -source 1.6 %SOURCE_FILE%.java
rem Create Java Archive file containing class file
"%JAVASDK%\jar" cf %CONFIGURATION%\%JAR_FILE%.jar -C .\JavaClasses\%JAR_FILE% com
rem Clean-up
SET ANDROID_JAR=""
SET SOURCE_FILE=""
SET JAR_FILE=""
SET CONFIGURATION=""

The batch file takes 3 parameters:

  1. Name of Java file, minus any extension.
  2. Name of JAR file, minus any extension.
  3. Name of configuration (i.e. "Debug" or "Release")

If there is only one parameter, the JAR filename will have the same name as Java file and configuration will be set to "Debug".

If there are only two parameters, configuration will be set to "Debug".

Note, the batch-file doesn't do any error checking regarding parameters right now.

The batch file uses slightly different parameters when calling "javac" than that in Danny Wind's article. These I took from what the Delphi IDE uses (as seen in the Output tab).

You will need to change the line that sets environment variable "ANDROID_JAR" to have the correct path to your compiler's "android.jar" file.

You will also need to set an environment variable named "JAVASDK" to the path of your Java SDK's "bin" folder. On my machine that is "C:\Program Files\Java\jdk1.8.0_60\bin". This I have set via "System Properties / Advanced / Environment Variables".

After compilation and archiving, the JAR containing the BroadcastReceiver will be in the "Debug" or "Release" folder depending on which configuration you've opted for.

Having added the JAR containing the BroadcastReceiver to your project, the newer versions of Delphi will then automatically link the JAR file into "Classes.dex" and ultimately the APK for your app.

Deploy the APK to your device, run the app at least once, and then restart your device. You should see your app startup once the boot has completed.

这篇关于如何在Delphi中使用后台服务自动运行应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆