简体   繁体   English

Delphi 10.3.3, Android App AutoStart on Boot (Android 8.x, 10.x)

[英]Delphi 10.3.3 , Android App AutoStart on Boot ( Android 8.x , 10.x )

I created a java file with following content:我创建了一个 java 文件,其内容如下:

package com.embarcadero.XLRBoot;
     
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
     
public class startup extends BroadcastReceiver
{
     
    @Override
        public void onReceive(Context context, Intent intent) 
        {
               Intent sintent = new Intent();
           sintent.setClassName(context, "com.embarcadero.firemonkey.FMXNativeActivity");
               sintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity(sintent);
        }
     
}

I have created a JAR file from this like this:我创建了一个 JAR 文件,如下所示:

C:\Program Files (x86)\Java\jdk1.7.0_71\bin>jar cf com-embarcadero-XLRBoot.jar "d:\Delphi XE 10\XLR Spider\Boot Receiver\com-embarcadero-XLRBoot.java"

My AndroidManifest.template looks like this:我的 AndroidManifest.template 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="%package%"
        android:versionCode="%versionCode%"
        android:versionName="%versionName%"
        android:installLocation="%installLocation%">

    <uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" />
    <%uses-permission%>
    <uses-feature android:glEsVersion="0x00020000" android:required="True"/>
    <application android:persistent="%persistent%" 
        android:restoreAnyVersion="%restoreAnyVersion%" 
        android:label="%label%" 
        android:debuggable="%debuggable%" 
        android:largeHeap="%largeHeap%"
        android:icon="%icon%"
        android:theme="%theme%"
        android:hardwareAccelerated="%hardwareAccelerated%"
        android:resizeableActivity="false">

        <%provider%>
        <%application-meta-data%>
        <%uses-libraries%>
        <%services%>
        <!-- Our activity is a subclass of the built-in NativeActivity framework class.
             This will take care of integrating with our NDK code. -->
        <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity"
                android:label="%activityLabel%"
                android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
                android:launchMode="singleTask">
            <!-- Tell NativeActivity the name of our .so -->
            <meta-data android:name="android.app.lib_name"
                android:value="%libNameValue%" />
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> 
        </activity>
        <receiver android:name="com.embarcadero.firemonkey.notifications.FMXNotificationAlarm" />
        <receiver android:name="com.embarcadero.XLRBoot"
                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>
        <!-- Registration is complete -->    
        <%activity%>
        <%receivers%>
    </application>
</manifest>
<!-- END_INCLUDE(manifest) -->

I have set following Permissions in Delphi: [x] Receive boot completed [x] Reorder tasks我在 Delphi 中设置了以下权限:[x] 接收启动已完成 [x] 重新排序任务

I compile my App, Run it, stop it, reboot device.我编译我的应用程序,运行它,停止它,重新启动设备。 I get an error that it stopped.我收到一个错误,它停止了。

In the logs I see following entry:在日志中,我看到以下条目:

04-20 12:38:49.087  2769  2769 E AndroidRuntime: FATAL EXCEPTION: main
04-20 12:38:49.087  2769  2769 E AndroidRuntime: Process: com.embarcadero.XLRBoot, PID: 2769
04-20 12:38:49.087  2769  2769 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate receiver com.embarcadero.XLRBoot: java.lang.ClassNotFoundException: Didn't find class "com.embarcadero.XLRBoot" on path: DexPathList[[zip file "/data/app/com.embarcadero.XLRBoot-OHpYv4loPMPls2q6DuLOLA==/base.apk"],nativeLibraryDirectories=[/data/app/com.embarcadero.XLRBoot-OHpYv4loPMPls2q6DuLOLA==/lib/arm, /data/app/com.embarcadero.XLRBoot-OHpYv4loPMPls2q6DuLOLA==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib, /system/vendor/lib/hw]]
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3174)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at android.app.ActivityThread.-wrap17(Unknown Source:0)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1675)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:106)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:164)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:6518)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
04-20 12:38:49.087  2769  2769 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.embarcadero.XLRBoot" on path: DexPathList[[zip file "/data/app/com.embarcadero.XLRBoot-OHpYv4loPMPls2q6DuLOLA==/base.apk"],nativeLibraryDirectories=[/data/app/com.embarcadero.XLRBoot-OHpYv4loPMPls2q6DuLOLA==/lib/arm, /data/app/com.embarcadero.XLRBoot-OHpYv4loPMPls2q6DuLOLA==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib, /system/vendor/lib/hw]]
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3169)
04-20 12:38:49.087  2769  2769 E AndroidRuntime:    ... 8 more

I obviously missed something, but what?我显然错过了一些东西,但是什么?

Thank you for the help.感谢您的帮助。

Regards Robert问候罗伯特

UPDATE 4更新 4

I removed the previous updates because I was a bit off.我删除了以前的更新,因为我有点偏离。 So this is how it seems like I need to properly create the files.所以这就是我需要正确创建文件的方式。

Generate Class File生成 Class 文件

javac -Xlint:all -classpath "c:\Users\Public\Documents\Embarcadero\Studio\20.0\PlatformSDKs\android-sdk-windows\platforms\android-26\android.jar" "d:\Delphi XE 10\XLR Spider\Boot Receiver\0.2\startup\com\embarcadero\XLRBoot\startup.java" -d "d:\Delphi XE 10\XLR Spider\Boot Receiver\0.2\startup"

Generate JAR file:生成 JAR 文件:

jar cf "d:\Delphi XE 10\XLR Spider\Boot Receiver\0.2\startup\com\embarcadero\XLRBoot\startup.jar" -C "d:\Delphi XE 10\XLR Spider\Boot Receiver\0.2\startup\com\embarcadero\XLRBoot" startup.class

Delphi gives a EXEC(1) Exec Error. Delphi 给出 EXEC(1) 执行错误。

Step 1:步骤1:

create src\com\XLR folder in your App Directory在您的 App Directory 中创建 src\com\XLR 文件夹

there create the file BootReceiver.java with the following content:在那里创建具有以下内容的文件 BootReceiver.java:

package com.XLR;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;

public class BootReceiver extends BroadcastReceiver
{

        @Override
        public void onReceive(Context context, Intent intent) 
        {
           Intent launchintent = new Intent();
           launchintent.setClassName(context, "com.embarcadero.firemonkey.FMXNativeActivity");           
           launchintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           context.startActivity(launchintent);  
        }

}

Step 2:第2步:

Under your App Directory create build.bat with following content:在您的 App Directory 下创建 build.bat 并包含以下内容:

@echo off
echo.
echo Compiles your Java code into classes.dex
echo Verified to work in Delphi 10.3.3
echo.
echo Place this batch in a java folder below your project (project\java)
echo Place the source in project\java\src\com\dannywind\delphi
echo If your source file location or name is different, please modify it below.
echo.

setlocal

set ANDROID_JAR="C:\Users\Public\Documents\Embarcadero\Studio\20.0\PlatformSDKs\android-sdk-windows\platforms\android-26\android.jar"
set DX_LIB="C:\Users\Public\Documents\Embarcadero\Studio\20.0\PlatformSDKs\android-sdk-windows\build-tools\28.0.2\lib"
set EMBO_DEX="D:\PRG\20.0\lib\android\debug\classes.dex"
set PROJ_DIR=%CD%
set VERBOSE=0
set JAVASDK="C:\Program Files (x86)\Java\jdk1.7.0_71\bin"
set DX_BAT="C:\Users\Public\Documents\Embarcadero\Studio\20.0\PlatformSDKs\android-sdk-windows\build-tools\28.0.2\dx.bat"

echo.
echo Compiling the Java source files
echo.
pause
mkdir output 2> nul
mkdir output\classes 2> nul
if x%VERBOSE% == x1 SET VERBOSE_FLAG=-verbose
%JAVASDK%\javac %VERBOSE_FLAG% -Xlint:all -classpath %ANDROID_JAR% -d output\classes -source 1.6 -target 1.6 src\com\XLR\BootReceiver.java

echo.
echo Creating jar containing the new classes
echo.
pause
mkdir output\jar 2> nul
if x%VERBOSE% == x1 SET VERBOSE_FLAG=v
%JAVASDK%\jar c%VERBOSE_FLAG%f output\jar\XLRBoot.jar -C output\classes com


:Exit

endlocal

Step 3:第 3 步:

now run build.bat and it will create the class and jar files in the Output Directory in your App Directory.现在运行 build.bat,它将在您的应用程序目录的 Output 目录中创建 class 和 jar 文件。

Step 4:第4步:

Alter your AndroidManifest.template like this像这样更改您的 AndroidManifest.template

<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="%package%"
        android:versionCode="%versionCode%"
        android:versionName="%versionName%"
        android:installLocation="%installLocation%">

    <uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="%targetSdkVersion%" />
    <%uses-permission%>
    <uses-feature android:glEsVersion="0x00020000" android:required="True"/>
    <application android:persistent="%persistent%" 
        android:restoreAnyVersion="%restoreAnyVersion%" 
        android:label="%label%" 
        android:debuggable="%debuggable%" 
        android:largeHeap="%largeHeap%"
        android:icon="%icon%"
        android:theme="%theme%"
        android:hardwareAccelerated="%hardwareAccelerated%"
        android:resizeableActivity="false">

        <%provider%>
        <%application-meta-data%>
        <%uses-libraries%>
        <%services%>
        <!-- Our activity is a subclass of the built-in NativeActivity framework class.
             This will take care of integrating with our NDK code. -->
        <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity"
                android:label="%activityLabel%"
                android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
                android:launchMode="singleTask">
            <!-- Tell NativeActivity the name of our .so -->
            <meta-data android:name="android.app.lib_name"
                android:value="%libNameValue%" />
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> 
        </activity>
        <receiver android:name="com.embarcadero.firemonkey.notifications.FMXNotificationAlarm" />
        <receiver android:name="com.XLR.BootReceiver"
                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>
        <!-- Registration is complete -->    
        <%activity%>
        <%receivers%>
    </application>
</manifest>
<!-- END_INCLUDE(manifest) -->

Step 5:第 5 步:

Add the output\jar\XLRBoot.jar file to your Libraries inside Delphi and Run the programm.将 output\jar\XLRBoot.jar 文件添加到 Delphi 内的库中并运行程序。

Step 6:第 6 步:

After Run the Program Starts, Stop it Start it again.运行程序启动后,停止它再次启动它。 Now Reboot it will start.现在重新启动它将启动。 Turn Device Off and Turn On again and it will start.关闭设备并再次打开,它将启动。

Thank you.谢谢你。

SPECIAL NOTE FOR ANDROID 10: ANDROID 10 的特别说明:

You must set the Permissions System Alert window in Delphi.您必须在 Delphi 中设置权限系统警报 window。 And on the Phone under App Info -> Advanced enable the Display over other apps settings.在手机上的 App Info -> Advanced 下启用 Display over other apps 设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Delphi 10.X 或 Delphi 11.X 中安装额外的 Android SDK? - How to install additional Android SDK into Delphi 10.X or Delphi 11.X? 对将第 3 方 android aar 库导入 Delphi 10.x 项目有疑问 - Have a question about importing 3rd party android aar library into Delphi 10.x project 有没有人成功地将您的 ionic cordova Hybrid 应用程序从 android 9.x 版升级到 android Cordova 10.x 版 - Has anybody successfully upgraded your ionic cordova Hybrid app from android version 9.x to android Cordova version 10.x 当 ionic cordova 在 Mac 10.x 上构建 android 时,gradle 失败 - gradle fails when ionic cordova build android on Mac 10.x Cordova(版本 10.x)- Ajax POST 方法不适用于 Android 平台 - Cordova (version 10.x) - Ajax POST method not working for Android platform ButterKnife有时会在Android 8.x上停止工作 - ButterKnife sometimes stops working on Android 8.x 自动启动 android 10 中的应用程序 - Autostart an application in android 10 Delphi Android 10.3.3 上没有返回键的 OnKeyDown 事件 - No OnKeyDown event for Return key on Delphi Android 10.3.3 Delphi 10.3.3 上的目标 Android API 29 - Target Android API 29 on Delphi 10.3.3 如何构建Cordova 8.x cordova.jar以包含在现有的Android应用程序中? - How do I build a Cordova 8.x cordova.jar to include in existing Android app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM