Embarcadero and Delphi on Android: Using AlarmManager

98 views Asked by At

On Embarcadero 11 I'm trying to run some check twice a day, please help I'm exhausted and losing my mind. I'm using \22.0\Samples\Object Pascal\Multi-Device Samples\Device Sensors and Services\AndroidNotificationServiceDemo\ as the base for my project. Just like in the demo, I'm starting the Android Service from GUi app by TLocalServiceConnection.StartService('NotificationService');

In the AndroidServiceStartCommand of the service I call my ScheduleTwiceDailyEvent which is currently:

procedure TNotificationServiceDM.ScheduleTwiceDailyEvent;
var
  AlarmManager: JAlarmManager;
  PendingIntent: JPendingIntent;
  Calendar: JCalendar;
  Intent: JIntent;
begin
  // Get the AlarmManager service
  AlarmManager := TJAlarmManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.ALARM_SERVICE));

  // Create an Intent to be broadcasted
  Intent := TJIntent.Create;
  Intent.setAction(StringToJString('com.embarcadero.firemonkey.ACTION_SCHEDULED_EVENT'));
  PendingIntent := TJPendingIntent.JavaClass.getBroadcast(TAndroidHelper.Context, 0, Intent, 0);

  // Schedule the alarm to repeat twice a day
  Calendar := TJCalendar.JavaClass.getInstance;
  Calendar.add(TJCalendar.JavaClass.HOUR_OF_DAY, 20);
  Calendar.add(TJCalendar.JavaClass.MINUTE, 3);
  AlarmManager.setRepeating(TJAlarmManager.JavaClass.RTC_WAKEUP, Calendar.getTimeInMillis, TJAlarmManager.JavaClass.INTERVAL_FIFTEEN_MINUTES, PendingIntent);
end;

(it's much more than twice per day for the testing period). The manifest file for the service (in the \Android64\Debug generated from my modified template) looks currently like this:

<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.embarcadero.NotificationService"
    android:versionCode="1"
    android:versionName="1.0.0"
    android:installLocation="auto">
    <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="30" />
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>
    <queries>

    </queries>
    <application
        android:persistent="False"
        android:restoreAnyVersion="False"
        android:label="NotificationService"
        android:debuggable="true"
        android:largeHeap="False"
        android:icon="@drawable/ic_launcher"
        android:theme="@android:style/Theme.NoTitleBar"
        android:hardwareAccelerated="true"
        android:resizeableActivity="false"
        android:requestLegacyExternalStorage="true">




        <!-- 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="NotificationService"
            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="NotificationService" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.embarcadero.rtl.notifications.NotificationAlarm" />

<receiver android:enabled="true" android:exported="false" android:label="MyAlarmReceiver" android:name="com.embarcadero.firemonkey.MyAlarmReceiver">
  <intent-filter>
    <action android:name="com.embarcadero.firemonkey.ACTION_SCHEDULED_EVENT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</receiver>


    </application>
</manifest>
<!-- END_INCLUDE(manifest) -->

The way I understand it, the AndroidServiceStartCommand should be called 15 minutes, but nothing happens. I have no idea how to even check if AlarmManager has this entry or how to debug this or anything.

What I don't understand is that if I run an app such as "APK Editor" on my phone, the manifest file it returns me for my app doesn't even include the line I added in the template which is properly included in the xml in the \Android64\Debug.

On the top of that I don't understand many things when it comes to Android development with Embarcadero and would appreciate any kind of course suggestion which goes beyond creating a simple example app.

I also tried to avoid the AlarmManager by created a new thread in the Android service and using "sleep" inside it. But it still blocks the gui app, I don't understand why.

0

There are 0 answers